May 27, 2016
On Friday, 27 May 2016 at 15:07:50 UTC, Adam D. Ruppe wrote:
> 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.

yes but i have to check for that when some one does

float.nan.checkThen!((f){ this fun should not run });

which should be the same as

float.init.checkThen!((f){ this fun should not run });
May 27, 2016
On Friday, 27 May 2016 at 15:19:50 UTC, ArturG wrote:
> yes but i have to check for that when some one does

Why? This is no different than if they set any of the other four billion possible values.

May 27, 2016
On Friday, 27 May 2016 at 15:24:18 UTC, Adam D. Ruppe wrote:
> On Friday, 27 May 2016 at 15:19:50 UTC, ArturG wrote:
>> yes but i have to check for that when some one does
>
> Why? This is no different than if they set any of the other four billion possible values.

What do you mean?

operation on float.nan gives you a float.nan so why does the shortcut evaluate to true and not false wouldnt that make more sense?


float f;
if(f) "why does this print".writeln; // it should not

same for char

char c;
if(c) "why does this print".writeln; // it should not

it works for most other types is there any reason why it doesnt work or couldnt work with floating points and character types?
May 27, 2016
On 5/27/16 11:49 AM, ArturG wrote:
> On Friday, 27 May 2016 at 15:24:18 UTC, Adam D. Ruppe wrote:
>> On Friday, 27 May 2016 at 15:19:50 UTC, ArturG wrote:
>>> yes but i have to check for that when some one does
>>
>> Why? This is no different than if they set any of the other four
>> billion possible values.
>
> What do you mean?
>
> operation on float.nan gives you a float.nan so why does the shortcut
> evaluate to true and not false wouldnt that make more sense?
>
>
> float f;
> if(f) "why does this print".writeln; // it should not
>
> same for char
>
> char c;
> if(c) "why does this print".writeln; // it should not
>
> it works for most other types is there any reason why it doesnt work or
> couldnt work with floating points and character types?

conversion to bool is not universally (val !is val.init)

Why are you expecting it to be?

Won't work for enums with first elements that are non-zero either:

enum foo : int {
 bar = 1;
}

foo f;

if(f) writeln("this will output too");

-Steve
May 27, 2016
On Friday, 27 May 2016 at 16:56:21 UTC, Steven Schveighoffer wrote:
> Why are you expecting it to be?
>
> Won't work for enums with first elements that are non-zero either:
>
> enum foo : int {
>  bar = 1;
> }
>
> foo f;
>
> if(f) writeln("this will output too");
>
> -Steve


but by default it works you just changed the default so its ok that it doesnt work.
May 27, 2016
On 5/27/16 1:42 PM, ArturG wrote:
> On Friday, 27 May 2016 at 16:56:21 UTC, Steven Schveighoffer wrote:
>> Why are you expecting it to be?
>>
>> Won't work for enums with first elements that are non-zero either:
>>
>> enum foo : int {
>>  bar = 1;
>> }
>>
>> foo f;
>>
>> if(f) writeln("this will output too");
>>
>
>
> but by default it works you just changed the default so its ok that it
> doesnt work.

I didn't change the default. The default is to pick the first member and use that as the init value. I may not have even considered what foo.init might be when I was creating my enum.

-Steve
May 27, 2016
On Friday, 27 May 2016 at 18:03:23 UTC, Steven Schveighoffer wrote:

> I didn't change the default. The default is to pick the first member and use that as the init value. I may not have even considered what foo.init might be when I was creating my enum.
>
> -Steve

by default i ment this

enum foo
{
    bar
}
foo f;
if(f) "dosnt print".writeln;

but i understand what you mean which adds a problem to my checkThen template, as the return type of the template depends on the return type of the callable which right now returns the init value of the callable return type if the type you pass into the template evaluates to false.

an example:

class Foo { int x; }
Foo foo(){ return null; }

foo.checkThen!( f => f.x = 5; ).writeln; // writes f.x.init because i kinda need a common return type if foo wouldnt return null

May 30, 2016
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.

does this count?

struct Foo
{
    int x;
    float f;
}

void main()
{
    Foo foo;
    if(foo is typeof(foo).init) "A: does'nt work".writeln;
    foo = Foo();
    if(foo is typeof(foo).init) "B: works".writeln;
}


if you remove the float from the struct both cases work or if you define the float inside the struct like this:

struct Foo
{
    int x;
    // float f = float.init; // does'nt work
    float f = float.nan;
}
May 30, 2016
On Friday, 27 May 2016 at 15:49:16 UTC, ArturG wrote:
> On Friday, 27 May 2016 at 15:24:18 UTC, Adam D. Ruppe wrote:
>> On Friday, 27 May 2016 at 15:19:50 UTC, ArturG wrote:
>>> yes but i have to check for that when some one does
>>
>> Why? This is no different than if they set any of the other four billion possible values.
>
> What do you mean?
>
> operation on float.nan gives you a float.nan so why does the shortcut evaluate to true and not false wouldnt that make more sense?

NaN in IEEE 754 floating-point numbers (the floating-point number system most languages and processors use) is defined as a number with all exponent bits set and a non-zero mantissa. The mantissa value is the "NaN payload", and can be any value.

`is` does a binary comparison on floating-point numbers, so NaNs with different payloads will not be considered equal, as you have found out with `float.init !is float.nan`.
May 31, 2016
On Monday, 30 May 2016 at 19:06:53 UTC, ArturG wrote:
> does this count?
>
> struct Foo
> {
>     int x;
>     float f;
> }
>
> void main()
> {
>     Foo foo;
>     if(foo is typeof(foo).init) "A: does'nt work".writeln;
>     foo = Foo();
>     if(foo is typeof(foo).init) "B: works".writeln;
> }

This one is a bug in DMD. It works correctly with LDC.

`Foo()` is supposed to be identical to `Foo.init`.

File here:
https://issues.dlang.org/show_bug.cgi?id=16105
1 2 3
Next ›   Last »