Thread overview | ||||||||
---|---|---|---|---|---|---|---|---|
|
October 07, 2007 Check floats for .nan | ||||
---|---|---|---|---|
| ||||
I have this code: float number=getFloat(); assert(number!=float.nan); //this passes assert(to!(char[])(number)!="nan"); //this fails writefln(string.toString(number)); //this outputs: "nan" writefln( to!(char[])(to!(long)(number)) ); //this generates an error What did I do wrong? |
October 07, 2007 Re: Check floats for .nan | ||||
---|---|---|---|---|
| ||||
Posted in reply to Acarion | Reply to acarion, > I have this code: > > float number=getFloat(); > assert(number!=float.nan); //this passes IIRC this will also pass: assert(float.nan != float.nan); this is because D used IEEE floating point semantics. Under IEEE a comparison with NaN will always be not equal If you want to test for nan use "testedValue !<>= 0"* or the isnan function > assert(to!(char[])(number)!="nan"); //this fails > writefln(string.toString(number)); //this outputs: "nan" > writefln( to!(char[])(to!(long)(number)) ); //this generates an error > What did I do wrong? > * this is the not grater than, less than, or equal to operator. |
October 07, 2007 Re: Check floats for .nan | ||||
---|---|---|---|---|
| ||||
Posted in reply to BCS |
BCS wrote:
> Reply to acarion,
>
>> I have this code:
>>
>> float number=getFloat();
>> assert(number!=float.nan); //this passes
>
> IIRC this will also pass:
>
> assert(float.nan != float.nan);
>
> this is because D used IEEE floating point semantics. Under IEEE a comparison with NaN will always be not equal
>
> If you want to test for nan use "testedValue !<>= 0"* or the isnan function
You can also use (x!=x) to test to see if x is NaN, and (x==x) to make
sure it is not NaN.
-- Daniel
|
October 07, 2007 Re: Check floats for .nan | ||||
---|---|---|---|---|
| ||||
Posted in reply to BCS | On 10/7/07, BCS <ao@pathlink.com> wrote:
> If you want to test for nan use "testedValue !<>= 0"
Are we really sure about that?
If testedValue is complex (with non-zero real part) then it will not be less than 0, it will not be greater than zero, and it will not be equal to zero. But it will also not be NaN.
|
October 07, 2007 Re: Check floats for .nan | ||||
---|---|---|---|---|
| ||||
Posted in reply to Janice Caron | Janice Caron wrote:
> On 10/7/07, BCS <ao@pathlink.com> wrote:
>> If you want to test for nan use "testedValue !<>= 0"
>
> Are we really sure about that?
>
> If testedValue is complex (with non-zero real part) then it will not
> be less than 0, it will not be greater than zero, and it will not be
> equal to zero. But it will also not be NaN.
I believe you'd get a type error in that case (real and creal can't be directly compared, etc.) Haven't tried it though.
Thanks,
Nathan Reed
|
October 07, 2007 Re: Check floats for .nan | ||||
---|---|---|---|---|
| ||||
Posted in reply to BCS | BCS wrote:
> Reply to acarion,
>
>> I have this code:
>>
>> float number=getFloat();
>> assert(number!=float.nan); //this passes
>
> IIRC this will also pass:
>
> assert(float.nan != float.nan);
>
> this is because D used IEEE floating point semantics. Under IEEE a comparison with NaN will always be not equal
>
> If you want to test for nan use "testedValue !<>= 0"* or the isnan function
I tend to use:
testedValue !<>= testedValue
Though I've occasionally wondered whether the 'is' identity operator should work for checking against nan:
testedValue is float.nan
Sean
|
Copyright © 1999-2021 by the D Language Foundation