Jump to page: 1 2
Thread overview
float price; if (price == float.nan) { // initialized } else { // uninitialized } ... valid ?
Jun 30, 2021
someone
Jun 30, 2021
Vladimir Panteleev
Jun 30, 2021
Mathias LANG
Jun 30, 2021
someone
Jun 30, 2021
Mathias LANG
Jun 30, 2021
someone
Jun 30, 2021
Vladimir Panteleev
Jun 30, 2021
someone
Jun 30, 2021
someone
Jun 30, 2021
jmh530
Jun 30, 2021
someone
Jul 08, 2021
bauss
Jun 30, 2021
Dennis
Jun 30, 2021
someone
Jun 30, 2021
Andre Pany
Jun 30, 2021
someone
Jul 08, 2021
bauss
June 30, 2021

Is the following code block valid ?

float price; /// initialized as float.nan by default ... right ?

if (price == float.nan) {

   /// writeln("initialized");

} else {

   /// writeln("uninitialized");

}

if so, the following one should be valid too ... right ?

float price;

if (price != float.nan) {

   /// writeln("initialized");

}
June 30, 2021

On Wednesday, 30 June 2021 at 03:15:46 UTC, someone wrote:

>

Is the following code block valid ?

Comparison with nan always results in false:

See section 10.11.5:

https://dlang.org/spec/expression.html#equality_expressions

You can use the is operator to perform bitwise comparison, or use std.math.isNaN.

June 30, 2021

On Wednesday, 30 June 2021 at 03:32:27 UTC, Vladimir Panteleev wrote:

>

On Wednesday, 30 June 2021 at 03:15:46 UTC, someone wrote:

>

Is the following code block valid ?

Comparison with nan always results in false:

See section 10.11.5:

https://dlang.org/spec/expression.html#equality_expressions

You can use the is operator to perform bitwise comparison, or use std.math.isNaN.

Side note: That doesn't apply to if (myFloat) or assert(myFloat);, unfortunately: https://issues.dlang.org/show_bug.cgi?id=13489

June 30, 2021

On Wednesday, 30 June 2021 at 03:32:27 UTC, Vladimir Panteleev wrote:

>

Comparison with nan always results in false:

THAT explains a lot !

>

See section 10.11.5:

missed it.

One of the things I do not like with D, and it causes me to shoot me on the foot over and over, is the lack of null for every data type. Things like:

float lnumStockPricePreceding = null;

foreach (float lnumStockPrice; ludtStockPriceEvolution.range)

   if (lnumStockPricePreceding ! is null) {

      /// do something

   }

   lnumStockPricePreceding = lnumStockPrice;

}

... were first attempted like following on D:

float lnumStockPricePreceding;

foreach (float lnumStockPrice; ludtStockPriceEvolution.range)

   if (lnumStockPricePreceding != float.nan) {

      /// do something

   }

   lnumStockPricePreceding = lnumStockPrice;

}

... and now need to be recoded like:

float lnumStockPricePreceding = 0f;

foreach (float lnumStockPrice; ludtStockPriceEvolution.range)

   if (lnumStockPricePreceding != 0f) {

      /// do something

   }

   lnumStockPricePreceding = lnumStockPrice;

}

... which oftenly complicates things because sometimes even 0f is a valid value for the task at hand and thus I MISS NULLs A LOT :( ... at least I can do nulls with strings since it a class :)

June 30, 2021

On Wednesday, 30 June 2021 at 03:52:51 UTC, someone wrote:

>

One of the things I do not like with D, and it causes me to shoot me on the foot over and over, is the lack of null for every data type. Things like:

If you want to give any type a "null" value, you could use std.typecons.Nullable.

June 30, 2021

On Wednesday, 30 June 2021 at 03:51:47 UTC, Mathias LANG wrote:

>

or use std.math.isNaN.

import std.math : isNaN;

float lnumStockPricePreceding;

foreach (float lnumStockPrice; ludtStockPriceEvolution.range)

   if (! isNan(lnumStockPricePreceding)) {

      /// do something

   }

   lnumStockPricePreceding = lnumStockPrice;

}

... is far from pretty but it works as expected, thanks for your tip !

June 30, 2021

On Wednesday, 30 June 2021 at 03:55:05 UTC, Vladimir Panteleev wrote:

>

If you want to give any type a "null" value, you could use
std.typecons.Nullable.

Practically Nullable!T stores a T and a bool.

I like the idea :)

June 30, 2021

On Wednesday, 30 June 2021 at 04:03:24 UTC, someone wrote:

>

On Wednesday, 30 June 2021 at 03:51:47 UTC, Mathias LANG wrote:

... is far from pretty but it works as expected, thanks for your tip !

Can be made a bit prettier with UFCS:

>
import std.math : isNaN;

float lnumStockPricePreceding;

foreach (float lnumStockPrice; ludtStockPriceEvolution.range)

   if (!lnumStockPricePreceding.isNan) {

      /// do something

   }

   lnumStockPricePreceding = lnumStockPrice;

}

Or even better, with ranges:

>
import std.math : isNaN;

float lnumStockPricePreceding;

foreach (float lnumStockPrice; ludtStockPriceEvolution.range.filter!(f => !f.isNan))
      /// do something

   lnumStockPricePreceding = lnumStockPrice;

}
June 30, 2021

On Wednesday, 30 June 2021 at 03:55:05 UTC, Vladimir Panteleev wrote:

>

If you want to give any type a "null" value, you could use
std.typecons.Nullable.

At LEAST for some things with currency types like prices which cannot be zero because 0 makes no sense for a price:

Nullable!(T, nullValue) is more storage-efficient than Nullable!T because it does not need to store an extra bool.

This could come handly. We'll see.

June 30, 2021

On Wednesday, 30 June 2021 at 03:52:51 UTC, someone wrote:

>

at least I can do nulls with strings since it a class :)

A string is not a class but an array, an immutable(char)[]. For arrays, null is equal to an empty array [].

void main() {
    string s0 = null;
    string s1 = [];
    assert(s0 == s1);
    assert(s0.length == 0); // no null dereference here
}
« First   ‹ Prev
1 2