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

Comparing floating point numbers

nw
Hi,

I'd like to compare 2 floating point numbers within a given error. I'd
rather not use a absolute error but one related to the number of
values that can be represented between the two floats. I've been
reading: http://www.cygnus-software.com/paper...ringfloats.htm
where the following function is provided to do this:

bool AlmostEqual2sComplement(float A, float B, int maxUlps) {
// Make sure maxUlps is non-negative and small enough that the
// default NAN won't compare as equal to anything.
assert(maxUlps 0 && maxUlps < 4 * 1024 * 1024);
int aInt = *(int*)&A;
// Make aInt lexicographically ordered as a twos-complement int
if (aInt < 0)
aInt = 0x80000000 - aInt;
// Make bInt lexicographically ordered as a twos-complement int
int bInt = *(int*)&B;
if (bInt < 0)
bInt = 0x80000000 - bInt;
int intDiff = abs(aInt - bInt);
if (intDiff <= maxUlps)
return true;
return false;
}

However, as the article states, this relies on a number of compiler
specific features, such as the size of int (and I guess float). It
also relies on the floats using IEEE representation (I guess all
compilers use this, but is it in the standard?).

So my question is this. Is there a good compiler independent method
for comparing floating point numbers with a relative error?

Apr 30 '07 #1
14 2481
nw wrote:
I'd like to compare 2 floating point numbers within a given error. [...]

However, as the article states, this relies on a number of compiler
specific features, such as the size of int (and I guess float). It
also relies on the floats using IEEE representation (I guess all
compilers use this, but is it in the standard?).
Right.
So my question is this. Is there a good compiler independent method
for comparing floating point numbers with a relative error?
double a, b;
...
if ( fabs(a-b) < myepsilon * max(fabs(a),fabs(b)) )
// they are "equal"

Pick myepsilon as you deem fit. That's your "relative error".

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Apr 30 '07 #2
nw
double a, b;
...
if ( fabs(a-b) < myepsilon * max(fabs(a),fabs(b)) )
// they are "equal"

Pick myepsilon as you deem fit. That's your "relative error".
ok, if I'm reading this correctly this will mean that large numbers
are allowed a bigger distance than smaller ones? This isn't exactly
what I want. The function I previously posted allows me to say "are
these two numbers with the X nearest possible floating point values"
is it possible to do that in a compiler independent way?

Apr 30 '07 #3
nw wrote:
> double a, b;
...
if ( fabs(a-b) < myepsilon * max(fabs(a),fabs(b)) )
// they are "equal"

Pick myepsilon as you deem fit. That's your "relative error".

ok, if I'm reading this correctly this will mean that large numbers
are allowed a bigger distance than smaller ones?
Bigger absolute distance, but the same relative distance.
This isn't exactly
what I want.
But that's what "relative" means.
The function I previously posted allows me to say "are
these two numbers with the X nearest possible floating point values"
Huh? Please re-read the statement inside double quotes and try
expressing it in mathematical notation.
is it possible to do that in a compiler independent way?
As soon as I know what exactly (or relatively) it is you want, I'll
try to help.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Apr 30 '07 #4
In message <f1**********@news.datemas.de>, Victor Bazarov
<v.********@comAcast.netwrites
>nw wrote:
>> double a, b;
...
if ( fabs(a-b) < myepsilon * max(fabs(a),fabs(b)) )
// they are "equal"

Pick myepsilon as you deem fit. That's your "relative error".

ok, if I'm reading this correctly this will mean that large numbers
are allowed a bigger distance than smaller ones?

Bigger absolute distance, but the same relative distance.
>This isn't exactly
what I want.

But that's what "relative" means.
>The function I previously posted allows me to say "are
these two numbers with the X nearest possible floating point values"

Huh? Please re-read the statement inside double quotes and try
expressing it in mathematical notation.
>is it possible to do that in a compiler independent way?

As soon as I know what exactly (or relatively) it is you want, I'll
try to help.
I think you need to read the cited article to find out what this is
about. This isn't really a "relative error", it's asking the question
"is the number of representable floating-point values between given A
and B less than N?"

--
Richard Herring
Apr 30 '07 #5
nw
I think you need to read the cited article to find out what this is
about. This isn't really a "relative error", it's asking the question
"is the number of representable floating-point values between given A
and B less than N?"
Yes what he said. :)

Sorry, the question wasn't defined as clearly as it could have been.
So is there any compiler independent method for this? Or is it perhaps
not even a particularly good idea?

Apr 30 '07 #6
Richard Herring wrote:
[...]
I think you need to read the cited article to find out what this is
about. This isn't really a "relative error", it's asking the question
"is the number of representable floating-point values between given A
and B less than N?"
Right. My fault for not reading the article, and I think we can call
it "relative error" after all. Each of the "next representable FP
number" from a certain value differs from it on a relative basis (the
exponent plays the part of the scaling factor).

To the OP:

It can be done, but only if the platform does have the integral type
large enough to represent the floating point representation, and that
the FP representation and the integral representation share the same
base (2). The information is available through 'std::numeric_limits'
specialisations, take a look.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Apr 30 '07 #7
nw wrote:
>I think you need to read the cited article to find out what this is
about. This isn't really a "relative error", it's asking the question
"is the number of representable floating-point values between given A
and B less than N?"

Yes what he said. :)

Sorry, the question wasn't defined as clearly as it could have been.
So is there any compiler independent method for this? Or is it perhaps
not even a particularly good idea?
Since there is no requirement that FP values are represented in the same
base as integral values, the answer is most likely "no".

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Apr 30 '07 #8
"nw" <ne*@soton.ac.ukwrote in message
news:11**********************@h2g2000hsg.googlegro ups.com...
>I think you need to read the cited article to find out what this is
about. This isn't really a "relative error", it's asking the question
"is the number of representable floating-point values between given A
and B less than N?"

Yes what he said. :)

Sorry, the question wasn't defined as clearly as it could have been.
So is there any compiler independent method for this? Or is it perhaps
not even a particularly good idea?
You need the C99 function nexttoward, which does almost exactly what
you want. It'll be a part of the next C++ Standard, but right now it's
relatively rare. See our Compleat Library, available at our web site.

P.J. Plauger
Dinkumware, Ltd.
http://www.dinkumware.com
Apr 30 '07 #9
nw
You need the C99 function nexttoward, which does almost exactly what
you want. It'll be a part of the next C++ Standard, but right now it's
relatively rare. See our Compleat Library, available at our web site.
Yep that does sound like what I want. I guess I'll just have to test
within absolute limits for now and patiently await the new C standard.

Thanks for your help!

Apr 30 '07 #10
P.J. Plauger wrote:
"nw" <ne*@soton.ac.ukwrote in message
news:11**********************@h2g2000hsg.googlegro ups.com...
>>I think you need to read the cited article to find out what this is
about. This isn't really a "relative error", it's asking the
question "is the number of representable floating-point values
between given A and B less than N?"

Yes what he said. :)

Sorry, the question wasn't defined as clearly as it could have been.
So is there any compiler independent method for this? Or is it
perhaps not even a particularly good idea?

You need the C99 function nexttoward, which does almost exactly what
you want. It'll be a part of the next C++ Standard, but right now it's
relatively rare. See our Compleat Library, available at our web site.
There is also nextafter which is almost the same but maybe slightly more
appropriate since it takes parameters of the same type instead of
having the second parameter always as a long double for reasons
probably only numerical analysts know.

They are also included with recent GNU libc versions that are used on
most Linux systems.

--
Markus

Apr 30 '07 #11
On Apr 30, 3:15 pm, "Victor Bazarov" <v.Abaza...@comAcast.netwrote:
nw wrote:
I'd like to compare 2 floating point numbers within a given error. [...]
However, as the article states, this relies on a number of compiler
specific features, such as the size of int (and I guess float). It
also relies on the floats using IEEE representation (I guess all
compilers use this, but is it in the standard?).
Right.
Which standard. IEEE representation is a standard, but it's not
required in C++, and is far from universal.

--
James Kanze (Gabi Software) email: ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

May 1 '07 #12
James Kanze wrote:
On Apr 30, 3:15 pm, "Victor Bazarov" <v.Abaza...@comAcast.netwrote:
>nw wrote:
>>I'd like to compare 2 floating point numbers within a given error.
[...]
>>However, as the article states, this relies on a number of compiler
specific features, such as the size of int (and I guess float). It
also relies on the floats using IEEE representation (I guess all
compilers use this, but is it in the standard?).
>Right.

Which standard. IEEE representation is a standard, but it's not
required in C++, and is far from universal.
Yes. Are you agreeing or disagreeing with my saying "right"?

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
May 1 '07 #13
On May 1, 3:29 pm, "Victor Bazarov" <v.Abaza...@comAcast.netwrote:
James Kanze wrote:
On Apr 30, 3:15 pm, "Victor Bazarov" <v.Abaza...@comAcast.netwrote:
nw wrote:
I'd like to compare 2 floating point numbers within a given error.
[...]
>However, as the article states, this relies on a number of compiler
specific features, such as the size of int (and I guess float). It
also relies on the floats using IEEE representation (I guess all
compilers use this, but is it in the standard?).
Right.
Which standard. IEEE representation is a standard, but it's not
required in C++, and is far from universal.
Yes. Are you agreeing or disagreeing with my saying "right"?
It depends on what you meant by "right". The way I read it was:
>I guess all compilers use IEEE representation, but is it in
the standard?
Right [meaning yes, it is in the standard].
However, I don't find "right" very idiomatic for responding to
this kind of question, so maybe you meant for it to apply to
something else.

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

May 2 '07 #14
James Kanze wrote:
On May 1, 3:29 pm, "Victor Bazarov" <v.Abaza...@comAcast.netwrote:
>James Kanze wrote:
>>On Apr 30, 3:15 pm, "Victor Bazarov" <v.Abaza...@comAcast.net>
wrote:
nw wrote:
I'd like to compare 2 floating point numbers within a given error.
[...]
>>>>However, as the article states, this relies on a number of
compiler specific features, such as the size of int (and I guess
float). It also relies on the floats using IEEE representation (I
guess all compilers use this, but is it in the standard?).
>>>Right.
>>Which standard. IEEE representation is a standard, but it's not
required in C++, and is far from universal.
>Yes. Are you agreeing or disagreeing with my saying "right"?

It depends on what you meant by "right".
It is meant as a confirmation response. What else could I mean?
The way I read it was:
>>I guess all compilers use IEEE representation, but is it in
the standard?
> Right [meaning yes, it is in the standard].

However, I don't find "right" very idiomatic for responding to
this kind of question, so maybe you meant for it to apply to
something else.
Good, you're doubting yourself. That's the first step to finding
the common ground.

I responded to the whole paragraph. Chiefly, it means I responded
to "this relies" and "It also relies". If I were to answer the
the "is it in the standard?" question, I'd use the word "standard"
in my reply somehow.

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

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...
687
by: cody | last post by:
no this is no trollposting and please don't get it wrong but iam very curious why people still use C instead of other languages especially C++. i heard people say C++ is slower than C but i can't...
11
by: John | last post by:
Hi, I encountered a strange problem while debugging C code for a Windows-based application in LabWindows CVI V5.5, which led me to write the test code below. I tried this code with a different...
15
by: michael.mcgarry | last post by:
Hi, I have a question about floating point precision in C. What is the minimum distinguishable difference between 2 floating point numbers? Does this differ for various computers? Is this...
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 );...
12
by: John Smith | last post by:
This code for the comparison of fp types is taken from the C FAQ. Any problems using it in a macro? /* compare 2 doubles for equality */ #define DBL_ISEQUAL(a,b)...
27
by: Thomas Kowalski | last post by:
Hi everyone, To determine equality of two doubles a and b the following is often done: bool isEqual ( double a, double b ) { return ( fabs (a-b) < THRESHOLD ); } But this a approach usually...
18
by: eman.abu.samra | last post by:
Hi all, i have encountered the strangest behavior. Check out this simple program: #include <stdio.h> int main() { double time = 1;
5
by: saneman | last post by:
I have a function: int F(double a) { if (a = =1.0) { return 22; } return 44; }
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
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
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...

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.