June 30, 2021

On Wednesday, 30 June 2021 at 04:17:19 UTC, someone wrote:

>

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:
[snip]

You've never given something away for free?

June 30, 2021

On Wednesday, 30 June 2021 at 09:36:34 UTC, Dennis wrote:

>

A string is not a class but an array, an immutable(char)[].

You're right. My fault.

June 30, 2021

On Wednesday, 30 June 2021 at 10:38:05 UTC, jmh530 wrote:

>

You've never given something away for free?

... more often than usual LoL

Now, seriously, something for free has not a price = 0, it has NO price, that's what null is for; we use zero for the lack of null.

June 30, 2021

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

>

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");

}

Side note: in case you want to work with money, you may consider using a specific data type like https://code.dlang.org/packages/money instead of float/double.

Kind regards
Andre

June 30, 2021

On Wednesday, 30 June 2021 at 16:24:38 UTC, Andre Pany wrote:

>

Side note: in case you want to work with money, you may consider using a specific data type like https://code.dlang.org/packages/money instead of float/double.

Yes, I've seen it, and in a previous-unrelated post I commented I am planning to use it (or something similar) because floats and currency are a horrible combo. I am not using it right now because I want to learn the language and encountering situations like this one helps me a lot, otherwise, I would have never noted such NaN behavior -to me, there are a lots of things that could fly under the radar at this moment. And by the way, looking at the code, money seems a quite simple non-nonsense implementation making it a solid candidate :)

Thanks for the tip Andre !

July 08, 2021

On Wednesday, 30 June 2021 at 16:19:35 UTC, someone wrote:

>

On Wednesday, 30 June 2021 at 10:38:05 UTC, jmh530 wrote:

>

You've never given something away for free?

... more often than usual LoL

Now, seriously, something for free has not a price = 0, it has NO price, that's what null is for; we use zero for the lack of null.

Actually a price can be 0, say if you have a sale that gives 100 % off then the price doesn’t magically disappear. It could be that the 100 % off is only for specific people or businesses. Or that they pay fees instead of the price of a product, in which case there’s a fee price but a product with the price of 0 because otherwise you’ll have accounting problems since you can’t make accounting on records without prices, because mathematically that makes no sense.

July 08, 2021

On Wednesday, 30 June 2021 at 16:41:40 UTC, someone wrote:

>

On Wednesday, 30 June 2021 at 16:24:38 UTC, Andre Pany wrote:

>

Side note: in case you want to work with money, you may consider using a specific data type like https://code.dlang.org/packages/money instead of float/double.

Yes, I've seen it, and in a previous-unrelated post I commented I am planning to use it (or something similar) because floats and currency are a horrible combo. I am not using it right now because I want to learn the language and encountering situations like this one helps me a lot, otherwise, I would have never noted such NaN behavior -to me, there are a lots of things that could fly under the radar at this moment. And by the way, looking at the code, money seems a quite simple non-nonsense implementation making it a solid candidate :)

Thanks for the tip Andre !

What you actually should do when working with money which is what a lot of banking solutions etc. do is working with cents only in 2 decimal places.

So 25.98 would be represented as 2598.

It makes sure that you’ll never have rounding errors or floating point representations that are wrong.

1 2
Next ›   Last »