Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Floating points

Reply
Thread Tools

Floating points

 
 
kasiyil
Guest
Posts: n/a
 
      09-01-2006
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?

 
Reply With Quote
 
 
 
 
Colander
Guest
Posts: n/a
 
      09-01-2006

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

 
Reply With Quote
 
 
 
 
David Harmon
Guest
Posts: n/a
 
      09-01-2006
On 1 Sep 2006 05:28:22 -0700 in comp.lang.c++, "kasiyil"
<> wrote,
>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.

 
Reply With Quote
 
Jim Langston
Guest
Posts: n/a
 
      09-01-2006
"kasiyil" <> wrote in message
news: ups.com...
> 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.


 
Reply With Quote
 
Victor Bazarov
Guest
Posts: n/a
 
      09-01-2006
Jim Langston wrote:
> "kasiyil" <> wrote in message
> news: ups.com...
>> 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.


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
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask


 
Reply With Quote
 
David Harmon
Guest
Posts: n/a
 
      09-02-2006
On Fri, 1 Sep 2006 16:08:44 -0400 in comp.lang.c++, "Victor Bazarov"
<> wrote,
>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.

 
Reply With Quote
 
Victor Bazarov
Guest
Posts: n/a
 
      09-02-2006
David Harmon wrote:
> On Fri, 1 Sep 2006 16:08:44 -0400 in comp.lang.c++, "Victor Bazarov"
> <> wrote,
>> 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.


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

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Writing a macro for checking close enough floating points. pereges C Programming 3 07-23-2008 03:37 PM
Writing a macro for checking close enough floating points. pereges C Programming 8 07-22-2008 01:32 PM
Problem with floating points in datagrid Anders K. Jacobsen [DK] ASP .Net 1 04-07-2005 10:45 PM
repr of floating points Noam Raphael Python 1 06-06-2004 07:07 AM
J2ME: Dealing with Floating Points? gilgantic Java 4 04-26-2004 02:18 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57