Skip to content

Commit f640ec2

Browse files
authored
feat(stdlib): Add number constants to number libraries (#1331)
1 parent 1e4110d commit f640ec2

File tree

9 files changed

+353
-137
lines changed

9 files changed

+353
-137
lines changed

compiler/test/stdlib/float32.test.gr

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,19 @@
11
import * from "float32"
22

3+
// Constants Test
4+
// smoke test:
5+
assert gt(infinity, 100000000.f)
6+
// test infinity-specific semantics:
7+
assert toNumber(infinity) == toNumber(infinity)
8+
assert toNumber(infinity) == (toNumber(infinity) - 1)
9+
assert nan != nan
10+
11+
assert pi == 3.1415927f
12+
13+
assert tau == 6.2831853f
14+
15+
assert e == 2.7182817f
16+
// Operations Tests
317
assert fromNumber(5) == 5.0f
418
assert fromNumber(0) == 0.0f
519

@@ -14,10 +28,3 @@ assert !gt(5.0f, 5.0f)
1428
assert !gte(5.0f, 22.0f)
1529
assert !lt(5.0f, -17.0f)
1630
assert !lte(5.0f, 4.0f)
17-
18-
// smoke test:
19-
assert gt(infinity, 100000000.f)
20-
// test infinity-specific semantics:
21-
assert toNumber(infinity) == toNumber(infinity)
22-
assert toNumber(infinity) == (toNumber(infinity) - 1)
23-
assert nan != nan

compiler/test/stdlib/float64.test.gr

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,18 @@
11
import * from "float64"
22

3+
// Constants Tests
4+
// smoke test:
5+
assert gt(infinity, 100000000.d)
6+
// test infinity-specific semantics:
7+
assert toNumber(infinity) == (toNumber(infinity) - 1)
8+
assert nan != nan
9+
10+
assert pi == 3.141592653589793d
11+
12+
assert tau == 6.283185307179586d
13+
14+
assert e == 2.718281828459045d
15+
// Operation Tests
316
assert fromNumber(5) == 5.0d
417
assert fromNumber(0) == 0.0d
518

@@ -14,9 +27,3 @@ assert !gt(5.0d, 5.0d)
1427
assert !gte(5.0d, 22.0d)
1528
assert !lt(5.0d, -17.0d)
1629
assert !lte(5.0d, 4.0d)
17-
18-
// smoke test:
19-
assert gt(infinity, 100000000.d)
20-
// test infinity-specific semantics:
21-
assert toNumber(infinity) == (toNumber(infinity) - 1)
22-
assert nan != nan

compiler/test/stdlib/number.test.gr

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,13 @@ import Float32 from "float32"
66
import Float64 from "float64"
77
import BI from "bigint"
88

9+
// Constants Test
10+
assert Number.pi == 3.141592653589793
11+
12+
assert Number.tau == 6.283185307179586
13+
14+
assert Number.e == 2.718281828459045
15+
// Operations Tests
916
// add
1017
assert Number.add(25, 5) == 30
1118
// Rational addition tests

stdlib/float32.gr

Lines changed: 50 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,56 @@ import {
1313
coerceFloat32ToNumber as toNumber,
1414
} from "runtime/numbers"
1515

16+
/**
17+
* @section Constants: Float32 constant values.
18+
*/
19+
20+
/**
21+
* Infinity represented as a Float32 value.
22+
*
23+
* @since v0.4.0
24+
*/
25+
@unsafe
26+
export let infinity = {
27+
let ptr = newFloat32(
28+
WasmF32.reinterpretI32(0b01111111100000000000000000000000n)
29+
)
30+
WasmI32.toGrain(ptr): Float32
31+
}
32+
33+
/**
34+
* NaN (Not a Number) represented as a Float32 value.
35+
*
36+
* @since v0.4.0
37+
*/
38+
@unsafe
39+
export let nan = {
40+
let ptr = newFloat32(
41+
WasmF32.reinterpretI32(0b01111111100000000000000000000001n)
42+
)
43+
WasmI32.toGrain(ptr): Float32
44+
}
45+
46+
/**
47+
* Pi represented as a Float32 value.
48+
*
49+
* @since v0.5.2
50+
*/
51+
export let pi = 3.1415927f
52+
53+
/**
54+
* Tau represented as a Float32 value.
55+
*
56+
* @since v0.5.2
57+
*/
58+
export let tau = 6.2831853f
59+
60+
/**
61+
* Euler's number represented as a Float32 value.
62+
*
63+
* @since v0.5.2
64+
*/
65+
export let e = 2.7182817f
1666
/**
1767
* @section Conversions: Functions for converting between Numbers and the Float32 type.
1868
*/
@@ -176,33 +226,3 @@ export let gte = (x: Float32, y: Float32) => {
176226
let yv = WasmF32.load(WasmI32.fromGrain(y), 8n)
177227
WasmF32.ge(xv, yv)
178228
}
179-
180-
/**
181-
* @section Constants: Float32 constant values.
182-
*/
183-
184-
/**
185-
* Infinity represented as a Float32 value.
186-
*
187-
* @since v0.4.0
188-
*/
189-
@unsafe
190-
export let infinity = {
191-
let ptr = newFloat32(
192-
WasmF32.reinterpretI32(0b01111111100000000000000000000000n)
193-
)
194-
WasmI32.toGrain(ptr): Float32
195-
}
196-
197-
/**
198-
* NaN (Not a Number) represented as a Float32 value.
199-
*
200-
* @since v0.4.0
201-
*/
202-
@unsafe
203-
export let nan = {
204-
let ptr = newFloat32(
205-
WasmF32.reinterpretI32(0b01111111100000000000000000000001n)
206-
)
207-
WasmI32.toGrain(ptr): Float32
208-
}

stdlib/float32.md

Lines changed: 69 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,75 @@ No other changes yet.
1313
import Float32 from "float32"
1414
```
1515

16+
## Constants
17+
18+
Float32 constant values.
19+
20+
### Float32.**infinity**
21+
22+
<details disabled>
23+
<summary tabindex="-1">Added in <code>0.4.0</code></summary>
24+
No other changes yet.
25+
</details>
26+
27+
```grain
28+
infinity : Float32
29+
```
30+
31+
Infinity represented as a Float32 value.
32+
33+
### Float32.**nan**
34+
35+
<details disabled>
36+
<summary tabindex="-1">Added in <code>0.4.0</code></summary>
37+
No other changes yet.
38+
</details>
39+
40+
```grain
41+
nan : Float32
42+
```
43+
44+
NaN (Not a Number) represented as a Float32 value.
45+
46+
### Float32.**pi**
47+
48+
<details disabled>
49+
<summary tabindex="-1">Added in <code>next</code></summary>
50+
No other changes yet.
51+
</details>
52+
53+
```grain
54+
pi : Float32
55+
```
56+
57+
Pi represented as a Float32 value.
58+
59+
### Float32.**tau**
60+
61+
<details disabled>
62+
<summary tabindex="-1">Added in <code>next</code></summary>
63+
No other changes yet.
64+
</details>
65+
66+
```grain
67+
tau : Float32
68+
```
69+
70+
Tau represented as a Float32 value.
71+
72+
### Float32.**e**
73+
74+
<details disabled>
75+
<summary tabindex="-1">Added in <code>next</code></summary>
76+
No other changes yet.
77+
</details>
78+
79+
```grain
80+
e : Float32
81+
```
82+
83+
Euler's number represented as a Float32 value.
84+
1685
## Conversions
1786

1887
Functions for converting between Numbers and the Float32 type.
@@ -283,33 +352,3 @@ Returns:
283352
|----|-----------|
284353
|`Bool`|`true` if the first value is greater than or equal to the second value or `false` otherwise|
285354

286-
## Constants
287-
288-
Float32 constant values.
289-
290-
### Float32.**infinity**
291-
292-
<details disabled>
293-
<summary tabindex="-1">Added in <code>0.4.0</code></summary>
294-
No other changes yet.
295-
</details>
296-
297-
```grain
298-
infinity : Float32
299-
```
300-
301-
Infinity represented as a Float32 value.
302-
303-
### Float32.**nan**
304-
305-
<details disabled>
306-
<summary tabindex="-1">Added in <code>0.4.0</code></summary>
307-
No other changes yet.
308-
</details>
309-
310-
```grain
311-
nan : Float32
312-
```
313-
314-
NaN (Not a Number) represented as a Float32 value.
315-

stdlib/float64.gr

Lines changed: 55 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,61 @@ import {
1313
coerceFloat64ToNumber as toNumber,
1414
} from "runtime/numbers"
1515

16+
/**
17+
* @section Constants: Float64 constant values.
18+
*/
19+
20+
/**
21+
* Infinity represented as a Float64 value.
22+
*
23+
* @since v0.4.0
24+
*/
25+
@unsafe
26+
export let infinity = {
27+
let ptr = newFloat64(
28+
WasmF64.reinterpretI64(
29+
0b0111111111110000000000000000000000000000000000000000000000000000N
30+
)
31+
)
32+
WasmI32.toGrain(ptr): Float64
33+
}
34+
35+
/**
36+
* NaN (Not a Number) represented as a Float64 value.
37+
*
38+
* @since v0.4.0
39+
*/
40+
@unsafe
41+
export let nan = {
42+
let ptr = newFloat64(
43+
WasmF64.reinterpretI64(
44+
0b0111111111110000000000000000000000000000000000000000000000000001N
45+
)
46+
)
47+
WasmI32.toGrain(ptr): Float64
48+
}
49+
50+
/**
51+
* Pi represented as a Float64 value.
52+
*
53+
* @since v0.5.2
54+
*/
55+
export let pi = 3.141592653589793d
56+
57+
/**
58+
* Tau represented as a Float64 value.
59+
*
60+
* @since v0.5.2
61+
*/
62+
export let tau = 6.283185307179586d
63+
64+
/**
65+
* Euler's number represented as a Float64 value.
66+
*
67+
* @since v0.5.2
68+
*/
69+
export let e = 2.718281828459045d
70+
1671
/**
1772
* @section Conversions: Functions for converting between Numbers and the Float64 type.
1873
*/
@@ -176,37 +231,3 @@ export let gte = (x: Float64, y: Float64) => {
176231
let yv = WasmF64.load(WasmI32.fromGrain(y), 8n)
177232
WasmF64.ge(xv, yv)
178233
}
179-
180-
/**
181-
* @section Constants: Float64 constant values.
182-
*/
183-
184-
/**
185-
* Infinity represented as a Float64 value.
186-
*
187-
* @since v0.4.0
188-
*/
189-
@unsafe
190-
export let infinity = {
191-
let ptr = newFloat64(
192-
WasmF64.reinterpretI64(
193-
0b0111111111110000000000000000000000000000000000000000000000000000N
194-
)
195-
)
196-
WasmI32.toGrain(ptr): Float64
197-
}
198-
199-
/**
200-
* NaN (Not a Number) represented as a Float64 value.
201-
*
202-
* @since v0.4.0
203-
*/
204-
@unsafe
205-
export let nan = {
206-
let ptr = newFloat64(
207-
WasmF64.reinterpretI64(
208-
0b0111111111110000000000000000000000000000000000000000000000000001N
209-
)
210-
)
211-
WasmI32.toGrain(ptr): Float64
212-
}

0 commit comments

Comments
 (0)