473,657 Members | 2,530 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Requirements of floating point comparisons

I'm fuzzy on exactly what I can depend on when doing floating point
comparisons. I know that calculated values can differ, e.g. I'm not
guaranteed that 1. + 2. == 3.

What about comparisons with literals?

float f=3.;
int v = f==3.;

is v guaranteed to be one? What about situations involving conversions
between floats and doubles?

int v = 3.f == 3.;

How about for values calculated from strings?

int v = strtod("3.0", NULL)==3.;

Any other situations I should know about?

Thanks for any advice,
-Peter

--
Pull out a splinter to reply.
Nov 14 '05 #1
10 1559
"Peter Ammon" <ge******@splin termac.com> wrote in message
news:J6******** *********@newss vr25.news.prodi gy.com...
I'm fuzzy on exactly what I can depend on when doing floating point
comparisons. I know that calculated values can differ, e.g. I'm not
guaranteed that 1. + 2. == 3.
Right. Although in this case, you probably *will* get a match.
What about comparisons with literals?
No difference. Why do you think there should be any?
float f=3.;
int v = f==3.;

is v guaranteed to be one?
In this particular case, very likely. In general, no.
What about situations involving conversions
between floats and doubles?
Same thing.
int v = 3.f == 3.;
Is this a typo?
How about for values calculated from strings?

int v = strtod("3.0", NULL)==3.;
The same in a different shade of mauve.
Any other situations I should know about?


You may find that a and b may not compare equal in the following case:

float a = 1.0/10.0, b = 0.1;

Peter
Nov 14 '05 #2
Peter Pichler wrote:

"Peter Ammon" <ge******@splin termac.com> wrote in message
news:J6******** *********@newss vr25.news.prodi gy.com...
I'm fuzzy on exactly what I can depend on when doing floating point
comparisons. I know that calculated values can differ, e.g. I'm not
guaranteed that 1. + 2. == 3.


Right. Although in this case, you probably *will* get a match.
What about comparisons with literals?


No difference. Why do you think there should be any?
float f=3.;
int v = f==3.;

is v guaranteed to be one?


In this particular case, very likely. In general, no.
What about situations involving conversions
between floats and doubles?


Same thing.
int v = 3.f == 3.;


Is this a typo?
How about for values calculated from strings?

int v = strtod("3.0", NULL)==3.;


The same in a different shade of mauve.
Any other situations I should know about?


You may find that a and b may not compare equal in the following case:

float a = 1.0/10.0, b = 0.1;


But, the OP seemed more interested in assignment operations
rather than arithmetic operations.

I would assume that for
double f = 0.1;
that
(f == 0.1)
was true,
regardless of whether 0.1 could be represented exactly as a double.
My reasoning is that even if 0.1 can't be represented exactly,
then whatever it's representation is,
will be the same for both the constant and the object.

--
pete
Nov 14 '05 #3
In article <40***********@ mindspring.com> pf*****@mindspr ing.com writes:
....
I would assume that for
double f = 0.1;
that
(f == 0.1)
was true,
regardless of whether 0.1 could be represented exactly as a double.
My reasoning is that even if 0.1 can't be represented exactly,
then whatever it's representation is,
will be the same for both the constant and the object.


Perhaps. The constant 0.1 can be kept in extended precision.
--
dik t. winter, cwi, kruislaan 413, 1098 sj amsterdam, nederland, +31205924131
home: bovenover 215, 1025 jn amsterdam, nederland; http://www.cwi.nl/~dik/
Nov 14 '05 #4
Dik T. Winter wrote:

In article <40***********@ mindspring.com> pf*****@mindspr ing.com writes:
...
> I would assume that for
> double f = 0.1;
> that
> (f == 0.1)
> was true,
> regardless of whether 0.1 could be represented exactly as a double.
> My reasoning is that even if 0.1 can't be represented exactly,
> then whatever it's representation is,
> will be the same for both the constant and the object.


Perhaps. The constant 0.1 can be kept in extended precision.


If an object of type double has been assigned the value
of a constant of type double, then should the value
of the object compare equal to the value of the constant ?
I think it should, always.

--
pete
Nov 14 '05 #5
In article <40***********@ mindspring.com> pf*****@mindspr ing.com writes:
Dik T. Winter wrote:

In article <40***********@ mindspring.com> pf*****@mindspr ing.com writes:
...
> I would assume that for
> double f = 0.1;
> that
> (f == 0.1)
> was true,
> regardless of whether 0.1 could be represented exactly as a double.
> My reasoning is that even if 0.1 can't be represented exactly,
> then whatever it's representation is,
> will be the same for both the constant and the object.


Perhaps. The constant 0.1 can be kept in extended precision.


If an object of type double has been assigned the value
of a constant of type double, then should the value
of the object compare equal to the value of the constant ?
I think it should, always.


Why? The assignment to the double can lead to loss of precision when
the constant is held in extended precision (and as far as I know that
is allowed by the standard).
--
dik t. winter, cwi, kruislaan 413, 1098 sj amsterdam, nederland, +31205924131
home: bovenover 215, 1025 jn amsterdam, nederland; http://www.cwi.nl/~dik/
Nov 14 '05 #6
Dik T. Winter wrote:

In article <40***********@ mindspring.com> pf*****@mindspr ing.com writes:
> Dik T. Winter wrote:
> >
> > In article <40***********@ mindspring.com> pf*****@mindspr ing.com writes:
> > ...
> > > I would assume that for
> > > double f = 0.1;
> > > that
> > > (f == 0.1)
> > > was true,
> > > regardless of whether 0.1 could be represented exactly as a double.
> > > My reasoning is that even if 0.1 can't be represented exactly,
> > > then whatever it's representation is,
> > > will be the same for both the constant and the object.
> >
> > Perhaps. The constant 0.1 can be kept in extended precision.

>
> If an object of type double has been assigned the value
> of a constant of type double, then should the value
> of the object compare equal to the value of the constant ?
> I think it should, always.


Why? The assignment to the double can lead to loss of precision when
the constant is held in extended precision (and as far as I know that
is allowed by the standard).


Constants have the same types as objects.
Representations are specified by type.

--
pete
Nov 14 '05 #7
On Mon, 16 Feb 2004 19:12:57 GMT, pete <pf*****@mindsp ring.com> wrote
in comp.lang.c:
Dik T. Winter wrote:

In article <40***********@ mindspring.com> pf*****@mindspr ing.com writes:
> Dik T. Winter wrote:
> >
> > In article <40***********@ mindspring.com> pf*****@mindspr ing.com writes:
> > ...
> > > I would assume that for
> > > double f = 0.1;
> > > that
> > > (f == 0.1)
> > > was true,
> > > regardless of whether 0.1 could be represented exactly as a double.
> > > My reasoning is that even if 0.1 can't be represented exactly,
> > > then whatever it's representation is,
> > > will be the same for both the constant and the object.
> >
> > Perhaps. The constant 0.1 can be kept in extended precision.
>
> If an object of type double has been assigned the value
> of a constant of type double, then should the value
> of the object compare equal to the value of the constant ?
> I think it should, always.


Why? The assignment to the double can lead to loss of precision when
the constant is held in extended precision (and as far as I know that
is allowed by the standard).


Constants have the same types as objects.
Representations are specified by type.


That's all very well in theory, but actually quite rarely observed in
practice. Regardless of what you think comparing a double initialized
from a floating point literal, and an identical floating point literal
at some other time can fail in many cases on many compilers.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.l earn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
Nov 14 '05 #8
pete <pf*****@mindsp ring.com> wrote in message news:<40******* ****@mindspring .com>...
Dik T. Winter wrote:

In article <40***********@ mindspring.com> pf*****@mindspr ing.com writes:
...
> I would assume that for
> double f = 0.1;
> that
> (f == 0.1)
> was true,
> regardless of whether 0.1 could be represented exactly as a double.
> My reasoning is that even if 0.1 can't be represented exactly,
> then whatever it's representation is,
> will be the same for both the constant and the object.


Perhaps. The constant 0.1 can be kept in extended precision.


If an object of type double has been assigned the value
of a constant of type double, then should the value
of the object compare equal to the value of the constant ?
I think it should, always.


Look up the thread "Are Floating Point Constants "constant?"
(back in 1998) for more than you want to know about this topic.
While the standard allows some leeway in the value which will be used given a
floating point constant (e.g. 0.1) the question is "must the
choice be consistent". There was no consensus (personally,
I don't think the standard does mandate consistency). In
practice, I would not depend on the implementation to get this
right, even if the requirement was clear.

- William Hughes
Nov 14 '05 #9
Jack Klein wrote:

On Mon, 16 Feb 2004 19:12:57 GMT, pete <pf*****@mindsp ring.com> wrote
in comp.lang.c:
Dik T. Winter wrote:

In article <40***********@ mindspring.com> pf*****@mindspr ing.com writes:
> Dik T. Winter wrote:
> >
> > In article <40***********@ mindspring.com> pf*****@mindspr ing.com writes:
> > ...
> > > I would assume that for
> > > double f = 0.1;
> > > that
> > > (f == 0.1)
> > > was true,
> > > regardless of whether 0.1 could be represented exactly as a double.
> > > My reasoning is that even if 0.1 can't be represented exactly,
> > > then whatever it's representation is,
> > > will be the same for both the constant and the object.
> >
> > Perhaps. The constant 0.1 can be kept in extended precision.
>
> If an object of type double has been assigned the value
> of a constant of type double, then should the value
> of the object compare equal to the value of the constant ?
> I think it should, always.

Why? The assignment to the double can lead to loss of precision when
the constant is held in extended precision (and as far as I know that
is allowed by the standard).


Constants have the same types as objects.
Representations are specified by type.


That's all very well in theory, but actually quite rarely observed in
practice. Regardless of what you think comparing a double initialized
from a floating point literal, and an identical floating point literal
at some other time can fail in many cases on many compilers.


Thank you.
I'll check out the 1998 "Are Floating Point Constants "constant?"
thread, suggested by William Hughes.

--
pete
Nov 14 '05 #10

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

Similar topics

4
3300
by: Dave | last post by:
Hi folks, I am trying to develop a routine that will handle sphere-sphere and sphere-triangle collisions and interactions. My aim is to develop a quake style collision engine where a player can interact with a rich 3D environment. Seem to be 90% of the way there! My problems are related to calculations where the result tends to zero (or another defined limit.) Have loads of cases where this kind of interaction occurs but this one
13
2909
by: Dylan Nicholson | last post by:
I just posted regarding a possible floating point error (not sure where), and have since discovered that: float f = 5.15002; double d = 5.15002; if (float(d) < f) puts("huh 1?"); float f2 = float(d); if (f2 < f) puts("huh 2?");
2
608
by: Sooha Park Lee | last post by:
I just fount out that most of machines provide standard macros handling "floating-point comparison" such as "isgreater()", "isgreaterequal()"... Thx. -S
9
1774
by: David Veeneman | last post by:
I'm working on a project that uses floating-point values (doubles), and I'm being driven crazy by something pretty basic. I understand that it's in the nature of floating-point calculations to produce values like 0.10000000000000003, when what I really want is 0.1. But is there any way to eliminate that digit at the end? I've tried rounding, but that simply moves the digit to the least significant position, such as 0.1000003. Failing...
4
3368
by: rz0 | last post by:
Hi all, This is a question about both C89 and C99 and is based on my partial reading of the standard drafts (one from before C89 but mainly N1124). If appropriate, please give a separate answer for each version of the language. Let's consider the conversion from a given floating real type to a specific integer type.
27
2145
by: G Patel | last post by:
Hello, I'm having trouble with floating point problems. I'm trying to write a function that calculates the distance between two cartesian points (integer coordinates). I have the function working, but it fails on one test case. Here is my function:
32
4083
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 ); } int main() { std::deque<double> pt ;
33
2758
by: dis_is_eagle | last post by:
hi....i have encountered strange problem regarding floating point comparison...the problem is... main() { float a=0.7; if(0.7 a) printf("hi"); else printf("hello");
6
1754
by: kasiyil | last post by:
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?
0
8413
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8842
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8617
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7352
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6176
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5642
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4330
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
1970
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1733
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.