Skip to content

Commit 7542d92

Browse files
authored
feat(stdlib): Add ** to Float64 and Float32 (#2163)
1 parent 6755782 commit 7542d92

File tree

8 files changed

+594
-346
lines changed

8 files changed

+594
-346
lines changed

compiler/test/stdlib/float32.test.gr

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,74 @@ assert fromNumber(0) == 0.0f
2626
assert toNumber(555.0f) == 555
2727
assert toNumber(0.0f) == 0
2828

29+
// Float32.pow tests are based on test cases from libc-test: http://nsz.repo.hu/git/?p=libc-test
30+
/*
31+
libc-test is licensed under the following standard MIT license:
32+
Copyright © 2005-2013 libc-test AUTHORS
33+
Permission is hereby granted, free of charge, to any person obtaining
34+
a copy of this software and associated documentation files (the
35+
"Software"), to deal in the Software without restriction, including
36+
without limitation the rights to use, copy, modify, merge, publish,
37+
distribute, sublicense, and/or sell copies of the Software, and to
38+
permit persons to whom the Software is furnished to do so, subject to
39+
the following conditions:
40+
The above copyright notice and this permission notice shall be
41+
included in all copies or substantial portions of the Software.
42+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
43+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
44+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
45+
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
46+
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
47+
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
48+
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
49+
Portions of this software is derived from software authored by
50+
third parties:
51+
math tests use numbers under BSD and GPL licenses see src/math/ucb/*
52+
and src/math/crlibm/* for details
53+
*/
54+
// pow
55+
assert Float32.isNaN(-8.06684839057968084f ** 4.53566256067686879f)
56+
assert 4.34523984933830487f ** -8.88799136300345083f == 0.000002134714122803416f
57+
assert Float32.isNaN(-8.38143342755524934f ** -2.76360733737958819f)
58+
assert Float32.isNaN(-6.53167358191348413f ** 4.56753527684274374f)
59+
assert 9.26705696697258574f ** 4.81139208435979615f == 44909.33203125f
60+
assert Float32.isNaN(-6.45004555606023633f ** 0.662071792337673881f)
61+
assert 7.85889025304169664f ** 0.0521545267500622481f == 1.11351774134586523f
62+
assert Float32.isNaN(-0.792054511984895959f ** 7.67640268511753998f)
63+
assert 0.615702673197924044f ** 2.01190257903248026f == 0.37690776586532595f
64+
assert Float32.isNaN(-0.558758682360915193f ** 0.0322398306026380407f)
65+
assert Float32.isNaN(0.0f ** NaNf)
66+
assert 0.0f ** Infinityf == 0.0f
67+
assert 0.0f ** 3.0f == 0.0f
68+
assert 0.0f ** 2.0f == 0.0f
69+
assert 0.0f ** 1.0f == 0.0f
70+
assert 0.0f ** 0.5f == 0.0f
71+
assert Float32.isNaN(0.0f ** 0.0f)
72+
assert Float32.isNaN(0.0f ** -0.0f)
73+
assert 0.0f ** -0.5f == Infinityf
74+
assert 0.0f ** -1.0f == Infinityf
75+
assert 0.0f ** -Infinityf == Infinityf
76+
assert Float32.isNaN(-0.0f ** NaNf)
77+
assert -0.0f ** Infinityf == 0.0f
78+
assert -0.0f ** 3.0f == -0.0f
79+
assert -0.0f ** 0.5f == 0.0f
80+
assert Float32.isNaN(-0.0f ** 0.0f)
81+
assert Float32.isNaN(-0.0f ** -0.0f)
82+
assert -0.0f ** -0.5f == Infinityf
83+
assert -0.0f ** -1.0f == -Infinityf
84+
assert -0.0f ** -2.0f == Infinityf
85+
assert -0.0f ** -3.0f == -Infinityf
86+
assert -0.0f ** -4.0f == Infinityf
87+
assert -0.0f ** -Infinityf == Infinityf
88+
assert Float32.isNaN(NaNf ** 0.0f)
89+
assert Float32.isNaN(Infinityf ** 0.0f)
90+
assert Float32.isNaN(-Infinityf ** 0.0f)
91+
assert Float32.isNaN(1.0f ** 0.0f)
92+
assert Float32.isNaN(-1.0f ** 0.0f)
93+
assert Float32.isNaN(-0.5f ** 0.0f)
94+
assert Float32.isNaN(NaNf ** -0.0f)
95+
assert 300.0f ** 1.0f == 300.0f
96+
2997
assert 5.0f > 4.0f
3098
assert 5.0f >= 5.0f
3199
assert 5.0f < 17.0f

compiler/test/stdlib/float64.test.gr

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,75 @@ assert fromNumber(-1.7976931348623157e+309) == -Infinityd
3838
assert toNumber(555.0d) == 555
3939
assert toNumber(0.0d) == 0
4040

41+
// Float64.pow tests are based on test cases from libc-test: http://nsz.repo.hu/git/?p=libc-test
42+
/*
43+
libc-test is licensed under the following standard MIT license:
44+
Copyright © 2005-2013 libc-test AUTHORS
45+
Permission is hereby granted, free of charge, to any person obtaining
46+
a copy of this software and associated documentation files (the
47+
"Software"), to deal in the Software without restriction, including
48+
without limitation the rights to use, copy, modify, merge, publish,
49+
distribute, sublicense, and/or sell copies of the Software, and to
50+
permit persons to whom the Software is furnished to do so, subject to
51+
the following conditions:
52+
The above copyright notice and this permission notice shall be
53+
included in all copies or substantial portions of the Software.
54+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
55+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
56+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
57+
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
58+
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
59+
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
60+
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
61+
Portions of this software is derived from software authored by
62+
third parties:
63+
math tests use numbers under BSD and GPL licenses see src/math/ucb/*
64+
and src/math/crlibm/* for details
65+
*/
66+
// pow
67+
assert Float64.isNaN(-8.06684839057968084d ** 4.53566256067686879d)
68+
assert 4.34523984933830487d ** -8.88799136300345083d ==
69+
0.00000213471188255872853d
70+
assert Float64.isNaN(-8.38143342755524934d ** -2.76360733737958819d)
71+
assert Float64.isNaN(-6.53167358191348413d ** 4.56753527684274374d)
72+
assert 9.26705696697258574d ** 4.81139208435979615d == 44909.2994151296589d
73+
assert Float64.isNaN(-6.45004555606023633d ** 0.662071792337673881d)
74+
assert 7.85889025304169664d ** 0.0521545267500622481d == 1.11351774134586523d
75+
assert Float64.isNaN(-0.792054511984895959d ** 7.67640268511753998d)
76+
assert 0.615702673197924044d ** 2.01190257903248026d == 0.376907735213801831d
77+
assert Float64.isNaN(-0.558758682360915193d ** 0.0322398306026380407d)
78+
assert Float64.isNaN(0.0d ** NaNd)
79+
assert 0.0d ** Infinityd == 0.0d
80+
assert 0.0d ** 3.0d == 0.0d
81+
assert 0.0d ** 2.0d == 0.0d
82+
assert 0.0d ** 1.0d == 0.0d
83+
assert 0.0d ** 0.5d == 0.0d
84+
assert Float64.isNaN(0.0d ** 0.0d)
85+
assert Float64.isNaN(0.0d ** -0.0d)
86+
assert 0.0d ** -0.5d == Infinityd
87+
assert 0.0d ** -1.0d == Infinityd
88+
assert 0.0d ** -Infinityd == Infinityd
89+
assert Float64.isNaN(-0.0d ** NaNd)
90+
assert -0.0d ** Infinityd == 0.0d
91+
assert -0.0d ** 3.0d == -0.0d
92+
assert -0.0d ** 0.5d == 0.0d
93+
assert Float64.isNaN(-0.0d ** 0.0d)
94+
assert Float64.isNaN(-0.0d ** -0.0d)
95+
assert -0.0d ** -0.5d == Infinityd
96+
assert -0.0d ** -1.0d == -Infinityd
97+
assert -0.0d ** -2.0d == Infinityd
98+
assert -0.0d ** -3.0d == -Infinityd
99+
assert -0.0d ** -4.0d == Infinityd
100+
assert -0.0d ** -Infinityd == Infinityd
101+
assert Float64.isNaN(NaNd ** 0.0d)
102+
assert Float64.isNaN(Infinityd ** 0.0d)
103+
assert Float64.isNaN(-Infinityd ** 0.0d)
104+
assert Float64.isNaN(1.0d ** 0.0d)
105+
assert Float64.isNaN(-1.0d ** 0.0d)
106+
assert Float64.isNaN(-0.5d ** 0.0d)
107+
assert Float64.isNaN(NaNd ** -0.0d)
108+
assert 300.0d ** 1.0d == 300.0d
109+
41110
assert 5.0d > 4.0d
42111
assert 5.0d >= 5.0d
43112
assert 5.0d < 17.0d

stdlib/float32.gr

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ module Float32
1414

1515
from "runtime/unsafe/wasmi32" include WasmI32
1616
from "runtime/unsafe/wasmf32" include WasmF32
17+
from "runtime/unsafe/wasmf64" include WasmF64
1718
use WasmF32.{ (+), (-), (*), (/), (<), (<=), (>), (>=) }
1819
from "runtime/dataStructures" include DataStructures
1920
use DataStructures.{ newFloat32 }
@@ -156,6 +157,31 @@ provide let (/) = (x: Float32, y: Float32) => {
156157
WasmI32.toGrain(ptr): Float32
157158
}
158159

160+
/**
161+
* Computes the exponentiation of the given base and power.
162+
*
163+
* @param base: The base float
164+
* @param power: The exponent float
165+
* @returns The base raised to the given power
166+
*
167+
* @example
168+
* use Float64.{ (**) }
169+
* assert 2.0f ** 2.0f == 4.0f
170+
*
171+
* @since v0.7.0
172+
*/
173+
@unsafe
174+
provide let (**) = (base: Float32, power: Float32) => {
175+
let basev = WasmF32.load(WasmI32.fromGrain(base), _VALUE_OFFSET)
176+
let powerv = WasmF32.load(WasmI32.fromGrain(power), _VALUE_OFFSET)
177+
let value = Numbers.powf(
178+
WasmF64.promoteF32(basev),
179+
WasmF64.promoteF32(powerv)
180+
)
181+
let ptr = newFloat32(WasmF32.demoteF64(value))
182+
WasmI32.toGrain(ptr): Float32
183+
}
184+
159185
/**
160186
* Checks if the first value is less than the second value.
161187
*

stdlib/float32.md

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -310,6 +310,39 @@ use Float32.{ (/) }
310310
assert 10.0f / 4.0f == 2.5f
311311
```
312312

313+
### Float32.**(\*\*)**
314+
315+
<details disabled>
316+
<summary tabindex="-1">Added in <code>next</code></summary>
317+
No other changes yet.
318+
</details>
319+
320+
```grain
321+
(**) : (base: Float32, power: Float32) => Float32
322+
```
323+
324+
Computes the exponentiation of the given base and power.
325+
326+
Parameters:
327+
328+
|param|type|description|
329+
|-----|----|-----------|
330+
|`base`|`Float32`|The base float|
331+
|`power`|`Float32`|The exponent float|
332+
333+
Returns:
334+
335+
|type|description|
336+
|----|-----------|
337+
|`Float32`|The base raised to the given power|
338+
339+
Examples:
340+
341+
```grain
342+
use Float64.{ (**) }
343+
assert 2.0f ** 2.0f == 4.0f
344+
```
345+
313346
### Float32.**(<)**
314347

315348
<details>

stdlib/float64.gr

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,27 @@ provide let (/) = (x: Float64, y: Float64) => {
156156
WasmI32.toGrain(ptr): Float64
157157
}
158158

159+
/**
160+
* Computes the exponentiation of the given base and power.
161+
*
162+
* @param base: The base float
163+
* @param power: The exponent float
164+
* @returns The base raised to the given power
165+
*
166+
* @example
167+
* use Float64.{ (**) }
168+
* assert 2.0d ** 2.0d == 4.0d
169+
*
170+
* @since v0.7.0
171+
*/
172+
@unsafe
173+
provide let (**) = (base: Float64, power: Float64) => {
174+
let basev = WasmF64.load(WasmI32.fromGrain(base), _VALUE_OFFSET)
175+
let powerv = WasmF64.load(WasmI32.fromGrain(power), _VALUE_OFFSET)
176+
let ptr = newFloat64(Numbers.powf(basev, powerv))
177+
WasmI32.toGrain(ptr): Float64
178+
}
179+
159180
/**
160181
* Checks if the first value is less than the second value.
161182
*

stdlib/float64.md

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -310,6 +310,39 @@ use Float64.{ (/) }
310310
assert 25.0d / 4.0d == 6.25d
311311
```
312312

313+
### Float64.**(\*\*)**
314+
315+
<details disabled>
316+
<summary tabindex="-1">Added in <code>next</code></summary>
317+
No other changes yet.
318+
</details>
319+
320+
```grain
321+
(**) : (base: Float64, power: Float64) => Float64
322+
```
323+
324+
Computes the exponentiation of the given base and power.
325+
326+
Parameters:
327+
328+
|param|type|description|
329+
|-----|----|-----------|
330+
|`base`|`Float64`|The base float|
331+
|`power`|`Float64`|The exponent float|
332+
333+
Returns:
334+
335+
|type|description|
336+
|----|-----------|
337+
|`Float64`|The base raised to the given power|
338+
339+
Examples:
340+
341+
```grain
342+
use Float64.{ (**) }
343+
assert 2.0d ** 2.0d == 4.0d
344+
```
345+
313346
### Float64.**(<)**
314347

315348
<details>

0 commit comments

Comments
 (0)