Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Rounding Float in C and Remove those Zeros

Reply
Thread Tools

Rounding Float in C and Remove those Zeros

 
 
Keith Thompson
Guest
Posts: n/a
 
      05-15-2006
"osmium" <(E-Mail Removed)> writes:
> "Joe Wright" writes:
>> osmium wrote:
>>> No. For example, the number 1 can not be represented exactly. Select as
>>> many terms as you wish from the following series:
>>> 1/2, 1/4, 1/8, ....1/2^googolplex
>>>
>>> No way are you going to get a 1 out of that. People who write I/O
>>> libraries are aware of this and create the *illusion* that you can.

>> You misunderstand the floating point system. In IEEE 754 integers up to
>> the width of the mantissa are represented exactly. On my system thats
>> 2**24 for float and 2**53 for double.

>
> You're right. I was referring to the floating point representation I
> learned in school. Which was not THE floating point system, it was only A
> floating point system. Since seeing some of the earlier posts I have looked
> into IEEE 754 and it *is* quite different - and an improvement. Bystanders
> should be aware, however, that there is no assurance that C uses IEEE 754,
> so coders still have to follow the old rules if they want to permit cross
> platform use. That is, don't use == if you want portability. The
> representation I was speaking about is still permissible.


And how does this system you learned in school represent 2.0?

--
Keith Thompson (The_Other_Keith) http://www.velocityreviews.com/forums/(E-Mail Removed) <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
 
Reply With Quote
 
 
 
 
Skarmander
Guest
Posts: n/a
 
      05-15-2006
osmium wrote:
> "Joe Wright" writes:
>
>> osmium wrote:

>
>>> No. For example, the number 1 can not be represented exactly. Select as
>>> many terms as you wish from the following series:
>>> 1/2, 1/4, 1/8, ....1/2^googolplex
>>>
>>> No way are you going to get a 1 out of that. People who write I/O
>>> libraries are aware of this and create the *illusion* that you can.

>> You misunderstand the floating point system. In IEEE 754 integers up to
>> the width of the mantissa are represented exactly. On my system thats
>> 2**24 for float and 2**53 for double.

>
> You're right. I was referring to the floating point representation I
> learned in school. Which was not THE floating point system, it was only A
> floating point system. Since seeing some of the earlier posts I have looked
> into IEEE 754 and it *is* quite different - and an improvement. Bystanders
> should be aware, however, that there is no assurance that C uses IEEE 754,
> so coders still have to follow the old rules if they want to permit cross
> platform use. That is, don't use == if you want portability. The
> representation I was speaking about is still permissible.
>

No, it's not. Although a C implementation is not required to use IEEE 754,
it cannot use a system where 1 is not exactly representable as a
floating-point number. Such a system could not meet the requirements of the
floating-point model described in the standard. (As long as we're talking
C99; I don't know what C89 said.) I verified this with the regulars of
comp.std.c to be sure; the thread is at
http://groups.google.com/group/comp....a116e9c14faab5

The "don't use ==" exhortation applies to all floating-point systems, IEEE
754 included. This is not a portability issue, since it's not even
guaranteed (or rather supposed) to work on one platform. If you're talking
binary representations, then those are indeed not portable, but == has
nothing to do with that.

You're sure you're not confusing all this with fixed-point arithmetic? In
fixed-point it's possible for 1.0 not to be exactly representable. (Standard
C does not define any fixed-point types, but it's not hard to implement them.)

S.
 
Reply With Quote
 
 
 
 
John Bode
Guest
Posts: n/a
 
      05-15-2006

osmium wrote:
> "Vladimir Oka" writes:
>
> > I > think (before looking into the Standard) that all small integers are
> > guaranteed to be. Beware numbers like 1000000000000042.0000, though.

>
> No. For example, the number 1 can not be represented exactly. Select as
> many terms as you wish from the following series:
> 1/2, 1/4, 1/8, ....1/2^googolplex
>


True that the value 1.0 cannot be represented exactly as a sum of the
series 1/2^n (n = -1, -2, -3, ...) with a finite number of terms.
However, since most floating point formats I'm aware of also use an
exponent, it *can* be represented exactly as

0.5 * 2^1

If you're talking about 0.1, now *that's* a problem, even with the
exponent.

 
Reply With Quote
 
Dik T. Winter
Guest
Posts: n/a
 
      05-15-2006
In article <(E-Mail Removed)> Keith Thompson <(E-Mail Removed)> writes:
> "osmium" <(E-Mail Removed)> writes:
> > "Joe Wright" writes:
> >> osmium wrote:
> >>> No. For example, the number 1 can not be represented exactly. Select as
> >>> many terms as you wish from the following series:
> >>> 1/2, 1/4, 1/8, ....1/2^googolplex

....
> > You're right. I was referring to the floating point representation I
> > learned in school.

>
> And how does this system you learned in school represent 2.0?


It looks more like fixed point than floating point to me.
--
dik t. winter, cwi, kruislaan 413, 1098 sj amsterdam, nederland, +31205924131
home: bovenover 215, 1025 jn amsterdam, nederland; http://www.cwi.nl/~dik/
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
float to string to float, with first float == second float Carsten Fuchs C++ 45 10-08-2009 09:47 AM
getting rid of leading zeros in float expotential tom@finland.com Python 1 06-03-2007 04:42 PM
Rounding values after zeros joshb Javascript 10 07-25-2005 04:28 PM
rounding a float/double to nearest 1/10th Shea Martin C++ 7 10-30-2003 04:51 PM
Re: float->byte->float is same with original float image. why float->ubyte->float is different??? bd C Programming 0 07-07-2003 12:09 AM



Advertisments