float price; if (price == float.nan) { // initialized } else { // uninitialized } ... valid ?
bauss
jj_1337 at live.dk
Thu Jul 8 16:12:06 UTC 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.
More information about the Digitalmars-d-learn
mailing list