473,405 Members | 2,310 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,405 software developers and data experts.

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 1539
"Peter Ammon" <ge******@splintermac.com> wrote in message
news:J6*****************@newssvr25.news.prodigy.co m...
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******@splintermac.com> wrote in message
news:J6*****************@newssvr25.news.prodigy.co m...
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*****@mindspring.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*****@mindspring.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*****@mindspring.com writes:
Dik T. Winter wrote:

In article <40***********@mindspring.com> pf*****@mindspring.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*****@mindspring.com writes:
> Dik T. Winter wrote:
> >
> > In article <40***********@mindspring.com> pf*****@mindspring.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*****@mindspring.com> wrote
in comp.lang.c:
Dik T. Winter wrote:

In article <40***********@mindspring.com> pf*****@mindspring.com writes:
> Dik T. Winter wrote:
> >
> > In article <40***********@mindspring.com> pf*****@mindspring.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.learn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
Nov 14 '05 #8
pete <pf*****@mindspring.com> wrote in message news:<40***********@mindspring.com>...
Dik T. Winter wrote:

In article <40***********@mindspring.com> pf*****@mindspring.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*****@mindspring.com> wrote
in comp.lang.c:
Dik T. Winter wrote:

In article <40***********@mindspring.com> pf*****@mindspring.com writes:
> Dik T. Winter wrote:
> >
> > In article <40***********@mindspring.com> pf*****@mindspring.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
On Mon, 16 Feb 2004 08:38:37 -0000, "Peter Pichler" <pi*****@pobox.sk>
wrote:
"Peter Ammon" <ge******@splintermac.com> wrote in message
news:J6*****************@newssvr25.news.prodigy.co m...

<snip>
What about situations involving conversions
between floats and doubles?


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


Is this a typo?

No. 3.f is a float (single) constant; 3. is a double constant. That
illustrates exactly his question "between floats and doubles".

Although it might not be exactly the best illustration, since this
computation can and probably will be done at compile time; plus 3. is
(well) within the range that will be exactly represented, and exactly
computed, on any remotely plausible FP implementation.

Something like
_Bool foo (double x) { return x = 7654321.f; }
.... foo (7654321.) ...
would be a better example.

(concur with the rest)

- David.Thompson1 at worldnet.att.net
Nov 14 '05 #11

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

Similar topics

4
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...
13
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 =...
2
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
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...
4
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...
27
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...
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 );...
33
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
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
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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
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
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,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
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,...
0
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...

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.