Skip to content
This repository has been archived by the owner on Nov 16, 2020. It is now read-only.

Change the default value of native num variables from NaN to 0e0 #4

Closed
lucasbuchala opened this issue Feb 13, 2018 · 9 comments
Closed

Comments

@lucasbuchala
Copy link

While native int variables are initialized with 0:

> say my int $
0

Native num variables are initialized with NaN:

> say my num $
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:

> say Num.new.perl
0e0

(As an argument, the C standard requires that static numeric variables be initialized to 0.)

@zoffixznet
Copy link
Contributor

zoffixznet commented Feb 13, 2018

This way, it would match the value returned by Num.new:

I don't think that's an equivalent comparison. This is:

> my Num $x; say $x.perl
Num

Without an initializer, you get the "undefined" value. Since native type variables can't contain type objects, you get the closest approximation. Since int types can only contain integers, 0 is the most "not like the other" value and feels a reasonable default, while num also support the value of NaN, so it gets that as "undefined" default.

(As an argument, the C standard requires that static numeric variables be initialized to 0.)

As a counter-argument, the IEEE 754-2008 Standard for Floating-Point
Arithmetic
, section 6.2 Operations with NaNs, second sentence says:

Signaling NaNs afford representations for uninitialized variables [...]


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 num default as NaN gives that consistency an edge.

@zoffixznet
Copy link
Contributor

One more point towards changing it was mentioned:

17:24 	lucasb 	I found a new argument for my proposal. NaN is true! and I don't like that :)
17:24 		m: say so my num $

@lucasbuchala
Copy link
Author

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.)

my num $x = 0e0;
my num $x = NaN;

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:

> my num $ = 0
This type cannot unbox to a native number: P6opaque, Int

These 2 snippets, that only differs by a single upper/lowercase letter, gives different results:

> my Num $x; $x++; say $x
1

> my num $x; $x++; say $x
NaN

NaN is true, which feels strange for a fresh not-assigned-before variable:

> say so my num $
True

> say so my num $ = 0e0
False

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
[2] https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/default-values-table
[3] https://golang.org/ref/spec#The_zero_value
[4] https://dlang.org/spec/type.html#basic-data-types

@AlexDaniel
Copy link
Member

my num @x; dd @x[5]
0e0

😂

@AlexDaniel
Copy link
Member

AlexDaniel commented Feb 14, 2018

↑ 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).

@ghost
Copy link

ghost commented Feb 14, 2018

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):

All native number types default to zero if unassigned, like their high-level counterparts.

Native numbers default to zero in most cases, except native num which defaults to NaN (True in boolean context) for compatibility with [standard of unrelated language from 1988]. See language/traps#t_nanayim_rakudotayim for more details [...]

@timo
Copy link

timo commented Feb 15, 2018

@lucasbuchala i just landed a change in rakudo that makes that error better:

If the programmer wants 0e0, they have to type 0e0, because just "0" won't work, which could confuse newcomers:

my num $ = 0
This type cannot unbox to a native number: P6opaque, Int

now it's:

===SORRY!=== Error while compiling -e
Cannot assign a literal of type Int (0) to a native variable of type Num. You can declare the variable to be of type Real, or try to coerce the value with 0.Num or Num(0), or just write the value as 0e0
at -e:1
------> my num $ = ⏏0

@zoffixznet
Copy link
Contributor

to a native variable of type Num

But it's of type num

@timo
Copy link

timo commented Feb 15, 2018

to a native variable of type Num

But it's of type num

fixed.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants