Movement against float.init being nan
Tejas
notrealemail at gmail.com
Sat Aug 20 07:08:50 UTC 2022
On Saturday, 20 August 2022 at 04:04:08 UTC, Ali Çehreli wrote:
> On 8/19/22 06:42, Hipreme wrote:
>
> > that float and double are initialized to `nan`.
>
> I think nan is the right choice. As Walter said, integrals are
> the outliers because there is not non equivalent for them.
>
> > This is really bad
>
> Although we are all guilty, what is worse is using fundamental
> types directly. I think the following is a solution to nan
> being inappropriate e.g. for Distance:
>
> struct initted(T, T initValue) {
> T value = initValue;
> alias value this;
> }
>
> alias Distance = initted!(double, 0);
>
> void main() {
> Distance d; // Not a nan ;)
> d += 1.5;
> d /= 2;
> assert(d == 0.75);
> }
>
> However, it is not type-safe enough because one can mix and
> match Distance with e.g. Temperature, which may not be correct
> for the program.
>
> Ali
Obligatory mention of how F# solved this with a feature called
`units of measure`
https://docs.microsoft.com/en-us/dotnet/fsharp/language-reference/units-of-measure
More information about the Digitalmars-d
mailing list