Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Comparing floating point numbers

Reply
Thread Tools

Comparing floating point numbers

 
 
nw
Guest
Posts: n/a
 
      04-30-2007
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?

 
Reply With Quote
 
 
 
 
Victor Bazarov
Guest
Posts: n/a
 
      04-30-2007
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


 
Reply With Quote
 
 
 
 
nw
Guest
Posts: n/a
 
      04-30-2007
> 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?

 
Reply With Quote
 
Victor Bazarov
Guest
Posts: n/a
 
      04-30-2007
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


 
Reply With Quote
 
Richard Herring
Guest
Posts: n/a
 
      04-30-2007
In message <f14re1$a8u$(E-Mail Removed)>, Victor Bazarov
<(E-Mail Removed)> writes
>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
 
Reply With Quote
 
nw
Guest
Posts: n/a
 
      04-30-2007
> 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?

 
Reply With Quote
 
Victor Bazarov
Guest
Posts: n/a
 
      04-30-2007
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


 
Reply With Quote
 
Victor Bazarov
Guest
Posts: n/a
 
      04-30-2007
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


 
Reply With Quote
 
P.J. Plauger
Guest
Posts: n/a
 
      04-30-2007
"nw" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) 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


 
Reply With Quote
 
nw
Guest
Posts: n/a
 
      04-30-2007
> 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!

 
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
comparing floating point numbers Adam Majewski C++ 12 02-13-2012 01:59 AM
Share-Point-2010 ,Share-Point -2010 Training , Share-point-2010Hyderabad , Share-point-2010 Institute Saraswati lakki ASP .Net 0 01-06-2012 06:39 AM
Comparing floating point values in Java Philipp Java 11 01-02-2007 12:20 PM
Fixed-point format for floating-point numbers Motaz Saad Java 7 11-05-2005 05:33 PM
comparing floating point numbers Thens Perl Misc 3 07-10-2003 04:52 PM



Advertisments