Article View: comp.std.c
Article #3778Re: Casting float to int - standard bug?
From: FFarance
Date: Fri, 18 Aug 1995 16:21
Date: Fri, 18 Aug 1995 16:21
44 lines
2483 bytes
2483 bytes
> Path: newsbf01.news.aol.com!newstf01.news.aol.com!uunet!in1.uu.net!news.bluesky. net!news.mathworks.com!newshost.marcam.com!usc!howland.reston.ans.net!news. nic.surfnet.nl!sun4nl!cwi.nl!dik > From: dik@cwi.nl (Dik T. Winter) > > ... > Stringized versions of comparison are much better. But there is again > one caveat; I do not know the exact requirements on the behaviour of > sprintf; are there any? Is it allowable that sprintf displays identical > strings for f-p numbers that are different? (I think it is BTW.) In > that case it is not a proper substitute. > > The portion of C code I saw (using frexp) is appears to be correct for base > 2 and 16. But for other bases there is something uncertain left (the > f-p calculations in there worry me). But when converting from float to > int, using frexp is correct. Since almost all floating point implementations of C's "float", "double", and "long double" use base 2 or 16, the example would avoid floating point most of the time. I guess I could have added case statements for base 3, 4, 5, ... up to say, 256? But what about bases greater that 256 -- use the "log" functions. Even so, you only need to call the function *once* per program -- only marginally longer on machines with oddball bases. Your solution requires floating point operations as you convert the floating point to a string. Furthermore, you need to convert to a string for *each* comparison. Comparing your solution to mine: Mine: Calling "log" one per program (the "log(2)" can be replaced by a constant) and *only* for machines with odd bases. Yours: Calling "sprintf" for each floating and integer conversion and comparison. I don't think your solution offers better performance. -FF ------------------------------------------------------------------- (``I only use AOL for reading netnews.'') Frank Farance, Farance Inc. E-mail: frank@farance.com, Telephone: +1 212 486 4700 ISO JTC1/SC22/WG14 & ANSI X3J11 (C Programming Language) Project Editor
Technical Details
Message-ID:
<412emk$7nb@newsbf02.news.aol.com>Path:
arc1-ca.pugleaf.net!newsbf02.news.aol.com!not-for-mailReferences:
<DDHC6H.vn@cwi.nl>Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!newsbf02.news.aol.com!not-for-mail From: ffarance@aol.com (FFarance) Newsgroups: comp.std.c Subject: Re: Casting float to int - standard bug? Date: 18 Aug 1995 12:21:40 -0400 Organization: America Online, Inc. (1-800-827-6364) Sender: root@newsbf02.news.aol.com Message-ID: <412emk$7nb@newsbf02.news.aol.com> References: <DDHC6H.vn@cwi.nl> Reply-To: ffarance@aol.com (FFarance) NNTP-Posting-Host: newsbf02.mail.aol.com