Hacker News new | ask | show | jobs
by bazoom42 17 days ago
You suggestions seem like complex solutions to a non-existing problem. If you need a decimal literal, just use the “m”-suffix! Why introduce weird ambiguities? E.g what would be the type of the expression `1.0 / 3.0` in your system? It might not be assigned directly to a variable but could be an argument to an overloaded method.

The bottom line is you need to know if you are using a decimal or a double. If you think suffix-less literals are confusing, just make it a rule to always use a suffix.

Why is double the default for suffix-less numbers? Probably because it is a standard floating point type supported by all processors and languages. Seems like a reasonable default.

1 comments

> E.g what would be the type of the expression `1.0 / 3.0` in your system? It might not be assigned directly to a variable but could be an argument to an overloaded method.

Ugh, I think it's extra gross that it would automatically set the overload when you never told it what size to use.

> The bottom line is you need to know if you are using a decimal or a double. If you think suffix-less literals are confusing, just make it a rule to always use a suffix.

Making a rule for my codebase to always use a suffix doesn't remove the confusion from all other codebases.

But even that is impossible because there is no suffix that specifies double.

> Why is double the default for suffix-less numbers? Probably because it is a standard floating point type supported by all processors and languages. Seems like a reasonable default.

So's single precision. And integer literals pick the opposite size and default to 32 bits in C#.

Btw, processors tend to use the same precision for all floating point operations. So there is really no reason to use float in the common case, since it is not faster or cheaper than double.

Float may be appropriate in some specific cases (interoperability, huge arrays) but should not be the default.

Which processors? Nobody uses x87 these days. x86 and Arm have separate instructions for doing floating point math in different precisions, since the vector units needed that circuitry anyway. The tendency is up to the program.

Most math is getting done in a single cycle but there's no reason to waste bytes. The same goes for both floating point and integers.

(And I didn't say single precision should be the default.)

The suffix for double is “d”. It is just a syntax-level shortcut to leave it out. From the compilers perspective “1.0d” and “1.0” are exactly equivalent.

It is just like `int` being a syntactic shortcut for System.Int32. It doesnt mean the size is unspecified, it just means it is specified using a shorter syntax.

> The suffix for double is “d”.

Well that's annoying, since I checked the documentation a couple time before posting that and it's not listed: https://learn.microsoft.com/en-us/dotnet/csharp/fundamentals...

I can find it on other pages but why would I have thought to do that...