forked from Mirror/wren
Add Num.[max/min]SafeInteger (#874)
This commit is contained in:
committed by
GitHub
parent
61cc6cb745
commit
28da4b449c
@ -35,6 +35,17 @@ The largest representable numeric value.
|
||||
|
||||
The smallest positive representable numeric value.
|
||||
|
||||
### Num.**maxSafeInteger**
|
||||
|
||||
The largest integer that Wren can safely represent. It's a constant value of `9007199254740991`.
|
||||
|
||||
This is relevant because Wren uses double precision [floating-point format](https://en.wikipedia.org/wiki/IEEE_floating_point)
|
||||
for numbers, which can only safely represent integers between <code>-(2<sup>53</sup> - 1)</code> and <code>2<sup>53</sup> - 1</code>.
|
||||
|
||||
### Num.**minSafeInteger**
|
||||
|
||||
The smallest integer Wren can safely represent. It's a constant value of `-9007199254740991`.
|
||||
|
||||
## Methods
|
||||
|
||||
### **abs**
|
||||
|
||||
@ -640,6 +640,9 @@ DEF_NUM_CONSTANT(tau, 6.28318530717958647692528676655900577)
|
||||
DEF_NUM_CONSTANT(largest, DBL_MAX)
|
||||
DEF_NUM_CONSTANT(smallest, DBL_MIN)
|
||||
|
||||
DEF_NUM_CONSTANT(maxSafeInteger, 9007199254740991.0)
|
||||
DEF_NUM_CONSTANT(minSafeInteger, -9007199254740991.0)
|
||||
|
||||
// Defines a primitive on Num that calls infix [op] and returns [type].
|
||||
#define DEF_NUM_INFIX(name, op, type) \
|
||||
DEF_PRIMITIVE(num_##name) \
|
||||
@ -1336,6 +1339,8 @@ void wrenInitializeCore(WrenVM* vm)
|
||||
PRIMITIVE(vm->numClass->obj.classObj, "tau", num_tau);
|
||||
PRIMITIVE(vm->numClass->obj.classObj, "largest", num_largest);
|
||||
PRIMITIVE(vm->numClass->obj.classObj, "smallest", num_smallest);
|
||||
PRIMITIVE(vm->numClass->obj.classObj, "maxSafeInteger", num_maxSafeInteger);
|
||||
PRIMITIVE(vm->numClass->obj.classObj, "minSafeInteger", num_minSafeInteger);
|
||||
PRIMITIVE(vm->numClass, "-(_)", num_minus);
|
||||
PRIMITIVE(vm->numClass, "+(_)", num_plus);
|
||||
PRIMITIVE(vm->numClass, "*(_)", num_multiply);
|
||||
|
||||
1
test/core/number/maxSafeInteger.wren
Normal file
1
test/core/number/maxSafeInteger.wren
Normal file
@ -0,0 +1 @@
|
||||
System.print(Num.maxSafeInteger) // expect: 9.007199254741e+15
|
||||
1
test/core/number/minSafeInteger.wren
Normal file
1
test/core/number/minSafeInteger.wren
Normal file
@ -0,0 +1 @@
|
||||
System.print(Num.minSafeInteger) // expect: -9.007199254741e+15
|
||||
Reference in New Issue
Block a user