floating point verification using is?

Don nospam at nospam.com
Mon Dec 28 03:51:30 PST 2009


Stewart Gordon wrote:
> Steven Schveighoffer wrote:
> <snip>
>> Are you still working on this?  :)  I think this proves my point.  The 
>> compiler does not provide an easy way to compare floats bitwise, so 
>> this means convoluted hard-to-write code.  When we have two operators 
>> that do equality -- and one of those means bitwise compare in all 
>> other contexts -- I think this is a no-brainer.
> 
> I entirely agree.
> 
> I've identified these cases in which float equality disagrees with 
> bitwise equality:
> - one is +0, one is -0
> - both are NaNs, identically signed
> - both are infinity, identically signed
> 
> In each case, is just does the same as ==.
> 
> Indeed, isIdentical is an ugly workaround for this, apparently created 
> instead of sanitising the definition of is to avoid the minuscule amount 
> of code breakage that the latter would effect.

No, it was because I have write access to Phobos, but not to the compiler...

But, this isn't as simple as people are assuming. There's a problem in 
the original request. We need to be clear about what T.init is. Is T.init
(a) the bit pattern which all variables of type T get default 
initialized to? Or is it
(b) an arbitrary valid value for type T?
In the case of floats, they are NOT "default initialized" -- they are 
marked as "not initialized". So there's a big question about whether 
semantic (a) T.init actually makes sense.

Consider this code:

T x;
if (x==T.init) { writefln("x is uninitialized"); }

Is this code valid? You are reading the value of an uninitialized variable.


More information about the Digitalmars-d-learn mailing list