• Status: Solved
• Priority: Medium
• Security: Public
• Views: 138

# Flaoting point equality in if then's

Hi,

I have read some of the many discussions on gloating point problems, perhaps there is a white paper I shoudl know about? Whatis the solution for comparing a floating point variable to a value in an if then? I have defined the value as a typed conatant and that seems to work sometimes. Any insight would be appreciated.

Thanks,

Tom.
0
boardtc
1 Solution

Commented:
Maybe FloatToDecimal is interesting for you.

When comparing floating point values you should concider the significance. That is how many digits behind the . are important.

I your want to know if 'x' is 'equal'  to 4.1234 (significance = 4) the check if 'x' is between 4.12339 and 4.12341.

4.12339 < x < 4.12341

This can become quite complex, if you know what I mean.

Epsylon.
0

Commented:
the prob of sig digits can be complex.  even 2 floats that should have the same value, may not due to rounding errors.  Suggested that you limit the prescision required ex. lo_bound < x < hi_bound or x >= hi_bound.
0

Commented:
edey, are you a parrot?   :o)
0

Commented:
Hi Tom,

Comparing floats in if's cause problem (as epsylon et al say) because of rounding etc, so:

1. Usually you can't directly test equality so do this:

const
epsilon = 0.00001; // put as many zeros in as you require

where you would write:

if a = b then

You now write:

if abs(a - b) < Epsilon then

2. Constant float values are a real trap because they default to EXTENDED. For instance, this is guranteed to fail:

const
c = 1;
var
v : Double

v:= 1;
if c = v then

But this will succeed:

const
c : Double = 1;
var
v : Double

v:= 1;
if c = v then
...

I hope this helps...

Cheers,

Raymond.

0

Commented:
My suggestion:

const
PRECISION = 10000;

...
if Trunc(x*PRECISION) = Trunc(y*PRECISION) then
ShowMessage('Equal')
else
ShowMessage('NOT Equal');

Am I missing smth ?

0

Commented:
Tom?
0

Author Commented:
Raymond, can you answer so I can grade. is there somewhere one can read up on this floating point thing - not necessarily for Delphi? Thanks, Tom.
0

Commented:
Hi Tom,

Here's the comment as an answer. I've never really seen anything explicitly covering the floating point comparison problems, at least in terms of something you can read up on. They are considered a natural consequence of rounding loss of precision (and in my experience have always been mentioned in that context). What usually happens is you get bitten by it and don't do it again (well, at least not very often!)

Comparing floats in if's cause problem (as epsylon et al say) because of rounding etc, so:

1. Usually you can't directly test equality so do this:

const
epsilon = 0.00001; // put as many zeros in as you require

where you would write:

if a = b then

You now write:

if abs(a - b) < Epsilon then

2. Constant float values are a real trap because they default to EXTENDED. For instance, this is guranteed to fail:

const
c = 1;
var
v : Double

v:= 1;
if c = v then

But this will succeed:

const
c : Double = 1;
var
v : Double

v:= 1;
if c = v then
...

I hope this helps...

Cheers,

Raymond.

0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.