float price; if (price == float.nan) { // initialized } else { // uninitialized } ... valid ?

someone someone at somewhere.com
Wed Jun 30 03:52:51 UTC 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:

```d
float lnumStockPricePreceding = null;

foreach (float lnumStockPrice; ludtStockPriceEvolution.range)

    if (lnumStockPricePreceding ! is null) {

       /// do something

    }

    lnumStockPricePreceding = lnumStockPrice;

}
```

... were first attempted like following on D:

```d
float lnumStockPricePreceding;

foreach (float lnumStockPrice; ludtStockPriceEvolution.range)

    if (lnumStockPricePreceding != float.nan) {

       /// do something

    }

    lnumStockPricePreceding = lnumStockPrice;

}
```

... and now need to be recoded like:

```d
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 :)



More information about the Digitalmars-d-learn mailing list