Change the default value of native num variables from NaN to 0e0 #4
Comments
I don't think that's an equivalent comparison. This is: Without an initializer, you get the "undefined" value. Since native type variables can't contain type objects, you get the closest approximation. Since
As a counter-argument, the IEEE 754-2008 Standard for Floating-Point
There are two consistencies that can be argued here: the "undefined" value consistency vs. "all native numerics gets 0 as default" consistency. IMO the fact that 6.c language already explicitly defines |
|
One more point towards changing it was mentioned: |
|
Just adding a few more notes... Let's forget for a second that num variables are initialized to NaN, and let's consider it to hold garbage before the first assignment. In this scenario, a careful programmer would want to explicit initialize it with 0e0 or NaN, if he doesn't know a better value to use at the declaration spot. (I.e. the programmer will only update the value further down the program.) Both values 0e0 and NaN are indicators that the variable is fresh and wasn't touched. Between these two values, 0e0 just feels more useful, because it can be operated on to arrive at new values, in contrast to NaN, which propagates itself. If the programmer wants 0e0, they have to type 0e0, because just "0" won't work, which could confuse newcomers: These 2 snippets, that only differs by a single upper/lowercase letter, gives different results: NaN is true, which feels strange for a fresh not-assigned-before variable: I tried to gather examples from other languages. Besides C, other languages that initialize (instance or global) float variables to 0e0 are Java, C#, and Go. The D language on the other hand initializes it with NaN. [1] https://docs.oracle.com/javase/specs/jls/se9/html/jls-4.html#jls-4.12.5 |
my num @x; dd @x[5]😂 |
|
↑ That's not the only inconsistency by the way. A native string (str) is initialized to "" (empty string), but arrays of native strs contain nulls (which is not right at all I think). |
|
I vote +1 for 0e0. Why? Consider which of these two hypothetical docs snippets you'd rather read (or have to defend, every time a newbie comes to the IRC to ask WTF is going on):
|
|
@lucasbuchala i just landed a change in rakudo that makes that error better:
now it's:
|
But it's of type |
fixed. |
While native int variables are initialized with 0:
Native num variables are initialized with NaN:
Is it possible to change the default value of native num variables to
0e0?This way, it would match the value returned by
Num.new:(As an argument, the C standard requires that static numeric variables be initialized to 0.)
The text was updated successfully, but these errors were encountered: