# Comparing floating point numbers

Discussion in 'C++' started by nw, Apr 30, 2007.

1. ### nwGuest

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
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?

nw, Apr 30, 2007

2. ### Victor BazarovGuest

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
--

Victor Bazarov, Apr 30, 2007

3. ### nwGuest

> 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?

nw, Apr 30, 2007
4. ### Victor BazarovGuest

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"

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
--

Victor Bazarov, Apr 30, 2007
5. ### Richard HerringGuest

In message <f14re1\$a8u\$>, Victor Bazarov
<> 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"

>
>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
"is the number of representable floating-point values between given A
and B less than N?"

--
Richard Herring

Richard Herring, Apr 30, 2007
6. ### nwGuest

> 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?

nw, Apr 30, 2007
7. ### Victor BazarovGuest

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
--

Victor Bazarov, Apr 30, 2007
8. ### Victor BazarovGuest

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
--

Victor Bazarov, Apr 30, 2007
9. ### P.J. PlaugerGuest

"nw" <> wrote in message
news:...

>> 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

P.J. Plauger, Apr 30, 2007
10. ### nwGuest

> 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.

nw, Apr 30, 2007
11. ### Markus SchoderGuest

P.J. Plauger wrote:
> "nw" <> wrote in message
> news:...
>
>>> I think you need to read the cited article to find out what this is
>>> 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

Markus Schoder, Apr 30, 2007
12. ### James KanzeGuest

On Apr 30, 3:15 pm, "Victor Bazarov" <> 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.

--
James Kanze (Gabi Software) email:
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

James Kanze, May 1, 2007
13. ### Victor BazarovGuest

James Kanze wrote:
> On Apr 30, 3:15 pm, "Victor Bazarov" <> 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"?

V
--

Victor Bazarov, May 1, 2007
14. ### James KanzeGuest

On May 1, 3:29 pm, "Victor Bazarov" <> wrote:
> James Kanze wrote:
> > On Apr 30, 3:15 pm, "Victor Bazarov" <> 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". 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:
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

James Kanze, May 2, 2007
15. ### Victor BazarovGuest

James Kanze wrote:
> On May 1, 3:29 pm, "Victor Bazarov" <> wrote:
>> James Kanze wrote:
>>> On Apr 30, 3:15 pm, "Victor Bazarov" <>
>>> 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"