473,396 Members | 1,927 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Floating points

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?

Sep 1 '06 #1
6 1740

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

Sep 1 '06 #2
On 1 Sep 2006 05:28:22 -0700 in comp.lang.c++, "kasiyil"
<si********@gmail.comwrote,
>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.

Sep 1 '06 #3
"kasiyil" <si********@gmail.comwrote in message
news:11*********************@p79g2000cwp.googlegro 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.
Sep 1 '06 #4
Jim Langston wrote:
"kasiyil" <si********@gmail.comwrote in message
news:11*********************@p79g2000cwp.googlegro 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
Sep 1 '06 #5
On Fri, 1 Sep 2006 16:08:44 -0400 in comp.lang.c++, "Victor Bazarov"
<v.********@comAcast.netwrote,
>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.

Sep 2 '06 #6
David Harmon wrote:
On Fri, 1 Sep 2006 16:08:44 -0400 in comp.lang.c++, "Victor Bazarov"
<v.********@comAcast.netwrote,
>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
Sep 2 '06 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: DvGrimm | last post by:
Any help would be very, very much appreciated... I've been searching the net (google) for 4 days now trying to find a php function to convert hex to floating point. I'm converting old...
2
by: gilgantic | last post by:
How do you deal with floating points in J2ME? From my understanding MIDP 1.0 does not allow you to use float or double primitives. I want to be able to perform a calculations which give float or...
3
by: Mark L Pappin | last post by:
<puts on Compiler Vendor hat> I've recently discovered that our compilers don't make any attempt to handle floating point overflow in add/subtract/ multiply/divide, with the result that...
7
by: Vinoth | last post by:
I'm working in an ARM (ARM9) system which does not have Floating point co-processor or Floating point libraries. But it does support long long int (64 bits). Can you provide some link that would...
5
by: Steffen | last post by:
Hi, is it possible to have two fractions, which (mathematically) have the order a/b < c/d (a,b,c,d integers), but when (correctly) converted into floating point representation just have the...
5
by: VISHNU VARDHAN REDDY UNDYALA | last post by:
Hello, Can someone over here help me in truncating a float variable. I mean if PI=3.14159 ...How can I get to read the first two or first three decimal values with out rounding them. Any...
1
by: Keith Kingsley | last post by:
I'm new to C#, so please forgive me if this is elementary. I have a file that consists of, say, one thousand (4-byte) floating points, and I'd like to read it into an array of floats as...
13
by: Bern McCarty | last post by:
I have run an experiment to try to learn some things about floating point performance in managed C++. I am using Visual Studio 2003. I was hoping to get a feel for whether or not it would make...
32
by: ma740988 | last post by:
template <class T> inline bool isEqual( const T& a, const T& b, const T epsilon = std::numeric_limits<T>::epsilon() ) { const T diff = a - b; return ( diff <= epsilon ) && ( diff >= -epsilon );...
9
by: pereges | last post by:
Hi, I'm trying to write a macro for the relative difference function which is used to check the close enough floating point values. Is this correct way to write it ? : #define max(x, y) ((x)...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.