Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
Solved

# Flaoting point equality in if then's

Posted on 1999-07-07
Medium Priority
136 Views
Last Modified: 2010-04-06
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
Question by:tomcorcoran
[X]
###### Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

• Help others & share knowledge
• Earn cash & points
• Learn & ask questions
8 Comments

LVL 13

Expert Comment

ID: 1386956
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

LVL 6

Expert Comment

ID: 1386957
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

LVL 13

Expert Comment

ID: 1386958
edey, are you a parrot?   :o)
0

LVL 12

Expert Comment

ID: 1386959
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

LVL 4

Expert Comment

ID: 1386960
My suggestion:

const
PRECISION = 10000;

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

Am I missing smth ?

0

LVL 13

Expert Comment

ID: 1386961
Tom?
0

Author Comment

ID: 1386962
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

LVL 12

Accepted Solution

rwilson032697 earned 200 total points
ID: 1386963
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

## Featured Post

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have toâ€¦
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi databaseâ€¦
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as formâ€¦
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrasâ€¦
###### Suggested Courses
Course of the Month4 days, 10 hours left to enroll

#### 660 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.