Skip to content

Commit

Permalink
Add Num.[max/min]SafeInteger (#874)
Browse files Browse the repository at this point in the history
  • Loading branch information
ChayimFriedman2 authored Apr 8, 2021
1 parent 61cc6cb commit 28da4b4
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 0 deletions.
11 changes: 11 additions & 0 deletions doc/site/modules/core/num.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -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**
Expand Down
5 changes: 5 additions & 0 deletions src/vm/wren_core.c
Original file line number Diff line number Diff line change
Expand Up @@ -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) \
Expand Down Expand Up @@ -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);
Expand Down
1 change: 1 addition & 0 deletions test/core/number/maxSafeInteger.wren
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
System.print(Num.maxSafeInteger) // expect: 9.007199254741e+15
1 change: 1 addition & 0 deletions test/core/number/minSafeInteger.wren
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
System.print(Num.minSafeInteger) // expect: -9.007199254741e+15

0 comments on commit 28da4b4

Please sign in to comment.