-
Notifications
You must be signed in to change notification settings - Fork 158
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
spec: floating point numbers #113
Comments
The list of arithmetic operators is missing multiplication |
Thanks Ulf, I added |
Depending on how far down the compatible-with-Python rabbit hole we want to go, it can be enormously complex to get all the formatting details right, especially across two different formatting systems (
Even if we'll behave as IEEE-754 in practice, do we want to put that in the spec or leave it implementation-defined? |
I meant: specify the semantics of |
The source code (now in |
Fixes bazelbuild#113 Change-Id: I33d7fb9f7b6e4227261ad484a97fd1c998c6355a
@ulfjack, would really appreciate your review of adonovan@265dd04 as a rare expert on both Starlark and floating-point. |
@b5, would also appreciate your review as a user of Starlark for database queries. (I presume some of them contain floating point numbers.) Note that this spec would govern the Go implementation too, so changes are in the pipeline. |
Fixes bazelbuild#113 Change-Id: I912998a190ea11c5455e6753d55e0948e80e5da1
Fixes bazelbuild#113 Change-Id: I912998a190ea11c5455e6753d55e0948e80e5da1
Fixes bazelbuild#113 Change-Id: I912998a190ea11c5455e6753d55e0948e80e5da1
Fixes bazelbuild#113 Change-Id: I912998a190ea11c5455e6753d55e0948e80e5da1
Fixes bazelbuild#113 Change-Id: I912998a190ea11c5455e6753d55e0948e80e5da1
Fixes bazelbuild#113 Change-Id: I612221610ac6e8a63c14fef2edfe5b0c72c3990c
Fixes bazelbuild#113 Change-Id: Id04ce3ce07b528c6bccb6143189da2870bb22d05
Fixes bazelbuild#113 Change-Id: Iaa676da8cd6c3142537b5bc5a654490a4a1f0887
This proposal argues for Starlark support for floating point numbers.
The Starlark implementations in Go and Java both support bigint (multiprecision) integers, allowing them to represent and manipulate all kinds of signed and unsigned numbers ranging from 8 bits to 64 that appear in protocol messages and other binary interchange formats. Proposal #112 argues for a byte string data type. The only remaining type required to support all values in protocol message is floating point numbers.
I propose that we support:
-1.23e+45
, following Python.float
, capable of representing all IEEE-754 double-precision (float64) values without loss.+
,-
,*
,/
,//
, and%
, over float x float.float x int
andint x float
.%e
,%f
, and%g
in string formatting (str % tuple
).<=
of (non-NaN) float and integer values according to mathematical tradition.float(int)
andint(float)
conversions.All of this is already implemented in go.starlark.net. See https://github.com/google/starlark-go/blob/master/doc/spec.md#floating-point-numbers for details.
On the equivalence of 1 and 1.0: the Go implementation uses mathematical equivalence for == and hashing. Until recently Java used j.l.Integer for its integers, so it determines the hash and equivalence relations---and Integers are never equal to Floats. However, as of last week, the Java implementation used its own bigint type, so we are free to define our own float type and have them collaborate on hash and equals.
On ordering and NaN: in the Go implementation, all ordering is expressed in terms of
<=
operations, so NaN <= x and x <= NaN are both false, and this does not imply NaN == x. However, Java's Comparator mechanism uses three-way comparison, like Python'scmp
, and cannot accommodate NaN. One possibility is to stop using Java's comparator and comparable, or perhaps expose it to Java but don't use it in the Starlark operators<=
andsorted
. After all, Comparator doesn't work the same way as Java's own<=
operator w.r.t. NaN.[UPDATE: the proposed spec change orders all floats, with NaN == NaN and Nan > +Inf.]
On rounding: past discussions have raised the specter of hardware floating-point operations revealing greater precision than IEEE-754 double-precision for intermediate results in e.g.
x+y*z
. I do not believe this is a real concern for Starlark. First, it's easy to enable strictfp arithmetic in the Java implementation. But more importantly, this is a Python-like language we are talking about, and there is so much loading and storing into the heap, and our interpreters are so unsophisticated, that there is no danger of a Starlark expressionx+y*z
being reduced to two ALU instructions on three registers. (We can dream...)The text was updated successfully, but these errors were encountered: