Floating points

K

kasiyil

Hello,

what is the disadvantage of using floating point numbers in boolean
comparisons. For example,

why using #define FALSE 0.0 will produce error instead of #define FALSE
0?
 
C

Colander

kasiyil schreef:

Hi There,
Hello,

what is the disadvantage of using floating point numbers in boolean
comparisons. For example,

why using #define FALSE 0.0 will produce error instead of #define FALSE
0?


The
#define FALSE 0.0
will not produce an error, it's legal c(++).
It's better style to write:
const double FALSE = 0.0;
but they should work kinda alike.

There are a lot of disadvantages, allmost to many to start, but the
main one is that you are mixing types. Bools are used to express
boolean values, doubles are used to express numbers. The world is far
more simpler if everybody tries to do this!

And did you know that c++ had an build-in constant called 'false'? I'd
use that one if I was you.

Good luck,
colander
 
D

David Harmon

On 1 Sep 2006 05:28:22 -0700 in comp.lang.c++, "kasiyil"
what is the disadvantage of using floating point numbers in boolean
comparisons.

Floating point numbers are complicated things. Booleans are much
simpler. It's better to keep things simple.

Floating point is used where you can tolerate approximate results.
Boolean true and false are exact. Never expect an exact result from
floating point.
 
J

Jim Langston

kasiyil said:
Hello,

what is the disadvantage of using floating point numbers in boolean
comparisons. For example,

why using #define FALSE 0.0 will produce error instead of #define FALSE
0?

They don't produce errors afaik, although you may not always get what you
expect. The main caveat is trying to compair a floating point number to an
exact number.

if ( MyFloat == 12.5 )
//...

MyFloat may appear to be 12.5, but may actually be something like
12.499999999999999 or such.

Other than that, I really don't userstand what you are asking.
 
V

Victor Bazarov

Jim said:
They don't produce errors afaik, although you may not always get what
you expect. The main caveat is trying to compair a floating point
number to an exact number.

if ( MyFloat == 12.5 )
//...

MyFloat may appear to be 12.5, but may actually be something like
12.499999999999999 or such.

Curiously enough, you picked a rather bad example. 12.5 is most likely
represented _precisely_. It's just 25/2. N/2^k (for N fitting in 50 bits
or so and ||k|| below 1023) can be precisely represented. 1/3 cannot, for
example.

V
 
D

David Harmon

On Fri, 1 Sep 2006 16:08:44 -0400 in comp.lang.c++, "Victor Bazarov"
Curiously enough, you picked a rather bad example. 12.5 is most likely
represented _precisely_. It's just 25/2. N/2^k (for N fitting in 50 bits
or so and ||k|| below 1023) can be precisely represented. 1/3 cannot, for
example.

1/3 is not so surprising to people who are used to decimal
arithmetic. 1.10 us surprising.
 
V

Victor Bazarov

David said:
On Fri, 1 Sep 2006 16:08:44 -0400 in comp.lang.c++, "Victor Bazarov"


1/3 is not so surprising to people who are used to decimal
arithmetic. 1.10 us surprising.

1/3 can be represented precisely in ternary, though... <g>

V
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,900
Latest member
Nell636132

Latest Threads

Top