Jump to page: 1 2
Thread overview
floating point verification using is?
Dec 18, 2009
bearophile
Dec 18, 2009
grauzone
Dec 18, 2009
bearophile
Dec 18, 2009
bearophile
Dec 18, 2009
bearophile
Dec 18, 2009
bearophile
Dec 18, 2009
div0
Dec 18, 2009
BCS
December 18, 2009
If I have 2 identical floating point values, how do I ensure they are binary equivalents of eachother?  I'm trying to write some unittest/assert code, and it's not exactly trivial.

I thought 'a is b' would work, but it just morphs into a == b, which isn't helpful.  Why doesn't 'is' just do a bit compare for floating points?

-Steve
December 18, 2009
Steven Schveighoffer:

> If I have 2 identical floating point values, how do I ensure they are binary equivalents of eachother?

Try this inside std.math of Phobos2:
bool isIdentical(real x, real y);


> I thought 'a is b' would work, but it just morphs into a == b, which isn't helpful.  Why doesn't 'is' just do a bit compare for floating points?

"is" is used to compare references.

Bye,
bearophile
December 18, 2009
bearophile wrote:
> Steven Schveighoffer:
> 
>> If I have 2 identical floating point values, how do I ensure they are  binary equivalents of eachother?
> 
> Try this inside std.math of Phobos2:
> bool isIdentical(real x, real y);
> 
> 
>> I thought 'a is b' would work, but it just morphs into a == b, which isn't  helpful.  Why doesn't 'is' just do a bit compare for floating points?
> 
> "is" is used to compare references.

No. If you use it with structs, the type's opEquals is not called. "is" is used for a lot of things.

I agree that is should do a bitwise comparison for floating points. That would be a nice fix for D2.

> Bye,
> bearophile
December 18, 2009
Steven Schveighoffer wrote:
> If I have 2 identical floating point values, how do I ensure they are binary equivalents of eachother?  I'm trying to write some unittest/assert code, and it's not exactly trivial.
> 
> I thought 'a is b' would work, but it just morphs into a == b, which
> isn't helpful.  Why doesn't 'is' just do a bit compare for floating points?
> 
> -Steve

Just out of curiosity; how many cases are there where a == b and they have different bit patterns?

I can only think of +-0.

Otherwise you can only get that with denormalised floats; which you won't get on an x86 processor.

- --
My enormous talent is exceeded only by my outrageous laziness.
http://www.ssTk.co.uk
December 18, 2009
On Fri, 18 Dec 2009 13:49:18 -0500, div0 <div0@users.sourceforge.net> wrote:

> -----BEGIN PGP SIGNED MESSAGE-----
> Hash: SHA1
>
> Steven Schveighoffer wrote:
>> If I have 2 identical floating point values, how do I ensure they are
>> binary equivalents of eachother?  I'm trying to write some
>> unittest/assert code, and it's not exactly trivial.
>>
>> I thought 'a is b' would work, but it just morphs into a == b, which
>> isn't helpful.  Why doesn't 'is' just do a bit compare for floating points?
>>
>> -Steve
>
> Just out of curiosity; how many cases are there where a == b
> and they have different bit patterns?
>
> I can only think of +-0.
>
> Otherwise you can only get that with denormalised floats;
> which you won't get on an x86 processor.

nan is the specific one I was trying to test for.

From my very hazy memory, I think there can even be multiple binary representations of nan, but I'm not sure.

-Steve
December 18, 2009
On Fri, 18 Dec 2009 12:34:09 -0500, bearophile <bearophileHUGS@lycos.com> wrote:

> Steven Schveighoffer:
>
>> If I have 2 identical floating point values, how do I ensure they are
>> binary equivalents of eachother?
>
> Try this inside std.math of Phobos2:
> bool isIdentical(real x, real y);

Thanks, that seems to be what I want.

>
>
>> I thought 'a is b' would work, but it just morphs into a == b, which isn't
>> helpful.  Why doesn't 'is' just do a bit compare for floating points?
>
> "is" is used to compare references.

to me, is means "ignore semantic meaning, do a bitwise compare" regardless of reference status.

For example comparing 2 array structs using == will check that all the data is the same, but using "is" makes it compare bitwise the structs directly.

I see no difference with floating points, except you can't get at the bits easily.  The existence of isIdentical could be completely replaced by one or two instructions generated by the compiler when it sees "float is float".

-Steve
December 18, 2009
On Fri, 18 Dec 2009 14:16:17 -0500, Steven Schveighoffer <schveiguy@yahoo.com> wrote:

> On Fri, 18 Dec 2009 12:34:09 -0500, bearophile <bearophileHUGS@lycos.com> wrote:
>
>> Steven Schveighoffer:
>>
>>> If I have 2 identical floating point values, how do I ensure they are
>>> binary equivalents of eachother?
>>
>> Try this inside std.math of Phobos2:
>> bool isIdentical(real x, real y);
>
> Thanks, that seems to be what I want.

Except it doesn't work with literals properly:

float x;

assert(isIdentical(x, float.init)); // fails

This is what I'm trying to test.  It has to do with the fact that float.init is a literal, and I think it's automatically converted to real.init.

This code works:

float x;
float y;

assert(isIdentical(x, y));

I'm going to file a bug on this, float is float should just work!

-Steve
December 18, 2009
Steven Schveighoffer:
> float x;
> 
> assert(isIdentical(x, float.init)); // fails
> 
> This is what I'm trying to test.  It has to do with the fact that float.init is a literal, and I think it's automatically converted to real.init.

Try this: http://www.digitalmars.com/d/2.0/phobos/std_math.html#isNaN

Bye,
bearophile
December 18, 2009
Steven Schveighoffer:
> to me, is means "ignore semantic meaning, do a bitwise compare" regardless of reference status.
> 
> For example comparing 2 array structs using == will check that all the data is the same, but using "is" makes it compare bitwise the structs directly.
> 
> I see no difference with floating points, except you can't get at the bits easily.  The existence of isIdentical could be completely replaced by one or two instructions generated by the compiler when it sees "float is float".

You can try expressing this idea in the main D group.

Bye,
bearophile
December 18, 2009
On Fri, 18 Dec 2009 14:47:13 -0500, bearophile <bearophileHUGS@lycos.com> wrote:

> Steven Schveighoffer:
>> float x;
>>
>> assert(isIdentical(x, float.init)); // fails
>>
>> This is what I'm trying to test.  It has to do with the fact that
>> float.init is a literal, and I think it's automatically converted to
>> real.init.
>
> Try this:
> http://www.digitalmars.com/d/2.0/phobos/std_math.html#isNaN

That's great, but I'm trying to verify that my array building code correctly appends T.init.  isNaN returns true no matter what the bit representation of nan is.  I want to *specifically* compare bit representations of floating point numbers to ensure the code I'm writing is doing what I think it's doing.  It shouldn't be this complicated to do that.

-Steve
« First   ‹ Prev
1 2