Comparing floats.

Started by
15 comments, last by LilBudyWizer 21 years, 4 months ago
> But I cannot remember the function to get the X value from a
> float.

I think it''s the lowest 23 bits of the float, with the other bits being sign and exponent.

You could examine the appropriate bits to get the exponent, then divide by 2^n (as it''s a binary exponent) to adjust a and b before testing |a = b| < e. But this still has problems if a and/or b can be close to zero, as then the exponent grabbed from the numbers may be to small and so dividing by 2^n may generate too big errors.
John BlackburneProgrammer, The Pitbull Syndicate
Advertisement
>Thus the |a - b| = X e N, hence epsilon is compared to _X_.

How can you be sure that your computer will store |a-b| so that _X_ can be compared to epsilon ?

Let''s say |a-b| = 1.0
then you can have |a-b| = 0.1 * 10^1 ("big") or = 0.00000000001 * 10^10 ("small") .


Tang
quote:Original post by Tangh
>Thus the |a - b| = X e N, hence epsilon is compared to _X_.

How can you be sure that your computer will store |a-b| so that _X_ can be compared to epsilon ?

Let''s say |a-b| = 1.0
then you can have |a-b| = 0.1 * 10^1 ("big") or = 0.00000000001 * 10^10 ("small") .


Tang



Again I forgot something important : the first digit after the decimal separator is not 0, thus 0.1 in is garanteed to be coded as 0.1E1 .


----
David Sporn AKA Sporniket
ok, I see... but I suppose you actually compare the exponent of (a-b) with the smaller of the expo of a and b (instead of comparing X).

Tang
Oh my god !!! the stuff about comparing the X part of |a - b| is just nonsense !!!!!!!

I apologize for writing such ****STUPID**** thing !!

=========================================================
Ok, I restart from the beginning :

let's say that 'a' is the reference, non zero value, then :

abs(a - b) is called "absolute error" and depends of the magnitude of a and b, thus so do epsilon.

abs((a - b)/a) is the "relative error" (relative to a) and it is independant from the magnitude of a and b, thus epsilon too.


[Edit] Ah, and if the reference is zero, well, obviously, we cannot use the relative error, of course.

----
David Sporn AKA Sporniket

[edited by - davidsporn on December 6, 2002 1:31:44 PM]
quote:Original post by Thrump
You could express your (semi-)real numbers as 2 integers.
...


Sorry to be a math-nazi, but you''d have a hard time representing irrational numbers this way (eg. pi, sqrt(2), e, etc.). You could store rational approximations to these numbers, but this hardly seems appropriate given your goal of eliminating the approximation built into the the binary representation of floating-point numbers.

A.P.
>Sorry to be a math-nazi, but you''d have a hard time >representing irrational numbers this way (eg. pi, sqrt(2), e, >etc.). You could store rational approximations to these >numbers, but this hardly seems appropriate given your goal of >eliminating the approximation built into the the binary >representation of floating-point numbers.

of course you cant get the exact value of a rationnal number with a float, but who cares ? the goal wasnt to eliminate the approximations, but only to make sure these approx arent too bad. (and if there were no approx, the whole topic would be pointless)

Tang

This topic is closed to new replies.

Advertisement