May 26, 2016 Re: Why do some T.init evaluate to true while others to false? | ||||
---|---|---|---|---|
| ||||
Posted in reply to ArturG | On 05/26/2016 05:28 PM, ArturG wrote: > On Thursday, 26 May 2016 at 15:25:26 UTC, ag0aep6g wrote: [...] >> What does it matter? > > You would have to create special cases for them. When? If you want to check if something is the .init value, compare against .init. |
May 26, 2016 Re: Why do some T.init evaluate to true while others to false? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Basile B. | On Thursday, 26 May 2016 at 15:38:55 UTC, Basile B. wrote:
>
> because nan is not 0 and that the shortcut for float is
>
> if (fpValue) <=> if (fpValue != 0)
> if (!fpValue)<=> if (fpValue == 0)
>
> There's no relation between the initializer and the shortcut. It's not because for some values the shortcut matches to the initializer that it must always be the case...But I admit I don't know the exact rationale.
>
> Does anyone know ?
Ok sorry for the noise and thanks anyway.
|
May 26, 2016 Re: Why do some T.init evaluate to true while others to false? | ||||
---|---|---|---|---|
| ||||
Posted in reply to ArturG | On Thursday, 26 May 2016 at 15:34:50 UTC, ArturG wrote:
> On Thursday, 26 May 2016 at 15:29:52 UTC, Basile B. wrote:
>
>>
>> float.init is not equal to 0.0f. In D FP points values are initialized to nan (not a number).
>>
>> By the way for strings it works, it's like the array case I described in the first answer).
>
> yes i guess i tested all/most types and know that float.init is float.nan but why is nan true and not false?
Oh, I'm so sorry ! I totally missed the point of the Q.
float.nan is not a "unique" value. Several values verify "nan" (Look at std.math.isNan). So I suppose it's simpler to test for nullity. Though with the sign there's also two possible 0...
|
May 26, 2016 Re: Why do some T.init evaluate to true while others to false? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Basile B. | On Thursday, 26 May 2016 at 15:48:18 UTC, Basile B. wrote:
> On Thursday, 26 May 2016 at 15:34:50 UTC, ArturG wrote:
>> On Thursday, 26 May 2016 at 15:29:52 UTC, Basile B. wrote:
>>
>>>
>>> float.init is not equal to 0.0f. In D FP points values are initialized to nan (not a number).
>>>
>>> By the way for strings it works, it's like the array case I described in the first answer).
>>
>> yes i guess i tested all/most types and know that float.init is float.nan but why is nan true and not false?
>
> Oh, I'm so sorry ! I totally missed the point of the Q.
>
> float.nan is not a "unique" value. Several values verify "nan" (Look at std.math.isNan). So I suppose it's simpler to test for nullity. Though with the sign there's also two possible 0...
void main(string[] args)
{
writeln(float.nan == float.init); // false
import std.math: isNaN;
writeln(isNaN(float.nan)); // true
writeln(isNaN(float.init)); //true
}
So the shortcut in the compiler might be more simple, there is only a single test for "if(myFloat)"...
|
May 26, 2016 Re: Why do some T.init evaluate to true while others to false? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Basile B. | On Thursday, 26 May 2016 at 15:51:39 UTC, Basile B. wrote: >> Oh, I'm so sorry ! I totally missed the point of the Q. >> >> float.nan is not a "unique" value. Several values verify "nan" (Look at std.math.isNan). So I suppose it's simpler to test for nullity. Though with the sign there's also two possible 0... > > void main(string[] args) > { > writeln(float.nan == float.init); // false > import std.math: isNaN; > writeln(isNaN(float.nan)); // true > writeln(isNaN(float.init)); //true > } > > So the shortcut in the compiler might be more simple, there is only a single test for "if(myFloat)"... im just playing with this template[1] is there anything else i missed? (if you dont mind) it basically treats any T.init as false and skips the function/delegate and just returns type. [1] https://dpaste.dzfl.pl/d159d83e3167 |
May 27, 2016 Re: Why do some T.init evaluate to true while others to false? | ||||
---|---|---|---|---|
| ||||
Posted in reply to ArturG | On Thursday, 26 May 2016 at 16:45:22 UTC, ArturG wrote:
> im just playing with this template[1] is there anything else i missed? (if you dont mind)
> it basically treats any T.init as false and skips the function/delegate and just returns type.
>
> [1] https://dpaste.dzfl.pl/d159d83e3167
If you just want to check whether something is equal to its type's .init value, use the `is` operator, which does a bitwise comparison:
if(value is typeof(value).init) ...
|
May 27, 2016 Re: Why do some T.init evaluate to true while others to false? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Marc Schütz | On Friday, 27 May 2016 at 09:25:55 UTC, Marc Schütz wrote:
> On Thursday, 26 May 2016 at 16:45:22 UTC, ArturG wrote:
>> im just playing with this template[1] is there anything else i missed? (if you dont mind)
>> it basically treats any T.init as false and skips the function/delegate and just returns type.
>>
>> [1] https://dpaste.dzfl.pl/d159d83e3167
>
> If you just want to check whether something is equal to its type's .init value, use the `is` operator, which does a bitwise comparison:
>
> if(value is typeof(value).init) ...
that still requiers a special case for floating points, arrays and optionally empty string literals.
|
May 27, 2016 Re: Why do some T.init evaluate to true while others to false? | ||||
---|---|---|---|---|
| ||||
Posted in reply to ArturG | On Friday, 27 May 2016 at 14:43:47 UTC, ArturG wrote:
>> if(value is typeof(value).init) ...
>
> that still requiers a special case for floating points, arrays and optionally empty string literals.
Have you tried? That should work in all cases.
|
May 27, 2016 Re: Why do some T.init evaluate to true while others to false? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Adam D. Ruppe | On Friday, 27 May 2016 at 14:48:59 UTC, Adam D. Ruppe wrote:
> On Friday, 27 May 2016 at 14:43:47 UTC, ArturG wrote:
>>> if(value is typeof(value).init) ...
>>
>> that still requiers a special case for floating points, arrays and optionally empty string literals.
>
> Have you tried? That should work in all cases.
float f;
if(f is float.init) "float init".writeln;
f = float.nan;
if(f is float.init) "float nan".writeln;
|
May 27, 2016 Re: Why do some T.init evaluate to true while others to false? | ||||
---|---|---|---|---|
| ||||
Posted in reply to ArturG | On Friday, 27 May 2016 at 14:56:28 UTC, ArturG wrote:
> float f;
> if(f is float.init) "float init".writeln;
> f = float.nan;
> if(f is float.init) "float nan".writeln;
You changed it to a value that isn't float.init, so of course it isn't going to match!
float.nan and float.init are NOT the same thing. float.init is a kind of NAN, but not the same kind.
|
Copyright © 1999-2021 by the D Language Foundation