Connecting Tech Pros Worldwide Help | Site Map

Re: Help w/ Floats needed!

Somebody
Guest
 
Posts: n/a
#1: Jun 27 '08
That way, in my case off 1/3, I'll be able to fully recover the original
Quote:
value. Ie...:
Quote:
>Not unless your doubles use a base 3, or a power of 3.
Huh? What does this have to do with 3's?

My point was, in the case of a 1/3 ratio (or any ratio with an infinite
repeating decimal), I'll never be able to recalculate the numerator exactly
given the denomenator and ratio.

But by storing the numerator and denomenator as ints instead of a ratio, I
would.


James Kanze
Guest
 
Posts: n/a
#2: Jun 27 '08

re: Re: Help w/ Floats needed!


On 18 mai, 20:05, "Somebody" <someb...@cox.netwrote:
Quote:
Quote:
That way, in my case off 1/3, I'll be able to fully recover the original
value. Ie...:
Not unless your doubles use a base 3, or a power of 3.
Quote:
Huh? What does this have to do with 3's?
The fraction 1/3 will only be exactly representable in a double
if the base of the double is 3 or a multiple of 3.
Quote:
My point was, in the case of a 1/3 ratio (or any ratio with an
infinite repeating decimal), I'll never be able to recalculate
the numerator exactly given the denomenator and ratio.
Quote:
But by storing the numerator and denomenator as ints instead
of a ratio, I would.
That's one solution. If the target value is an int, after
rescaling, it's probable that the double will store the ratio
with enough precision, provided that you round the results
correctly.

--
James Kanze (GABI Software) email:james.kanze@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

Frank Birbacher
Guest
 
Posts: n/a
#3: Jun 27 '08

re: Re: Help w/ Floats needed!


Hi!

Somebody schrieb:
Quote:
My point was, in the case of a 1/3 ratio (or any ratio with an infinite
repeating decimal), I'll never be able to recalculate the numerator exactly
given the denomenator and ratio.
Although your statement is logically correct the reverse it not:

Not every finite decimal (that is no repeating decimal) is exactly
representable as a float/double.

The point is: you must not forget that doubles are 2-based and not
10-based. That's why a number like 0.3 cannot be exactly stored in a
double, because it has an infinitly repeating binary representation. But
0.25 can (one fourth, or 0.01 in binary, or 1e-2 in binary).

You must learn that floating point calculations are not exact. You must
learn there is no operator == in floating point math. For your problem
either use proper rounding or fractional numbers (as you proposed).

Regards,
Frank
Closed Thread