Skip to content

Commit dea4cb5

Browse files
authored
feat(stdlib)!: Replace Float64 arithmatic/comparison functions with operators (#1957)
1 parent 57f070c commit dea4cb5

File tree

5 files changed

+143
-78
lines changed

5 files changed

+143
-78
lines changed

compiler/test/stdlib/float64.test.gr

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,10 @@ from Float64 use *
55

66
// Constants Tests
77
// smoke test:
8-
assert gt(infinity, 100000000.0d)
8+
assert infinity > 100000000.0d
99
// test infinity-specific semantics:
10-
assert toNumber(infinity) == toNumber(infinity) - 1
10+
from Pervasives use { (-) as numberSub }
11+
assert toNumber(infinity) == numberSub(toNumber(infinity), 1)
1112
assert nan != nan
1213

1314
assert pi == 3.141592653589793d
@@ -24,11 +25,11 @@ assert fromNumber(0) == 0.0d
2425
assert toNumber(555.0d) == 555
2526
assert toNumber(0.0d) == 0
2627

27-
assert gt(5.0d, 4.0d)
28-
assert gte(5.0d, 5.0d)
29-
assert lt(5.0d, 17.0d)
30-
assert lte(5.0d, 5.0d)
31-
assert !gt(5.0d, 5.0d)
32-
assert !gte(5.0d, 22.0d)
33-
assert !lt(5.0d, -17.0d)
34-
assert !lte(5.0d, 4.0d)
28+
assert 5.0d > 4.0d
29+
assert 5.0d >= 5.0d
30+
assert 5.0d < 17.0d
31+
assert 5.0d <= 5.0d
32+
assert !(5.0d > 5.0d)
33+
assert !(5.0d >= 22.0d)
34+
assert !(5.0d < -17.0d)
35+
assert !(5.0d <= 4.0d)

compiler/test/suites/numbers.re

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ describe("numbers", ({test, testSkip}) => {
9494
assertRun("nan_equality1", {|print(NaNf == NaNf)|}, "false\n");
9595
assertRun(
9696
"nan_equality2",
97-
{|include "float64"; print(Float64.div(0.0d, 0.0d) == Float64.div(0.0d, 0.0d))|},
97+
{|include "float64"; from Float64 use { (/) }; print((0.0d / 0.0d) == (0.0d / 0.0d))|},
9898
"false\n",
9999
);
100100
assertRun("nan_equality3", {|print(0.0 / 0.0 == 0.0 / 0.0)|}, "false\n");

compiler/test/suites/strings.re

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -270,17 +270,17 @@ bar", 1))|},
270270
assertRun("string_float3", {|print(-Infinityf)|}, "-Infinity\n");
271271
assertRun(
272272
"string_float4",
273-
{|include "float64"; from Float64 use *; print(div(0.0d, 0.0d))|},
273+
{|include "float64"; from Float64 use *; print(0.0d / 0.0d)|},
274274
"NaN\n",
275275
);
276276
assertRun(
277277
"string_float5",
278-
{|include "float64"; from Float64 use *; print(div(1.0d, 0.0d))|},
278+
{|include "float64"; from Float64 use *; print(1.0d / 0.0d)|},
279279
"Infinity\n",
280280
);
281281
assertRun(
282282
"string_float6",
283-
{|include "float64"; from Float64 use *; print(div(-1.0d, 0.0d))|},
283+
{|include "float64"; from Float64 use *; print(-1.0d / 0.0d)|},
284284
"-Infinity\n",
285285
);
286286

stdlib/float64.gr

Lines changed: 24 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -66,10 +66,11 @@ provide { fromNumber, toNumber }
6666
* @param y: The second operand
6767
* @returns The sum of the two operands
6868
*
69-
* @since v0.2.0
69+
* @since v0.6.0
70+
* @history v0.2.0: Originally named `add`
7071
*/
7172
@unsafe
72-
provide let add = (x: Float64, y: Float64) => {
73+
provide let (+) = (x: Float64, y: Float64) => {
7374
let xv = WasmF64.load(WasmI32.fromGrain(x), 8n)
7475
let yv = WasmF64.load(WasmI32.fromGrain(y), 8n)
7576
let ptr = newFloat64(xv + yv)
@@ -83,10 +84,11 @@ provide let add = (x: Float64, y: Float64) => {
8384
* @param y: The second operand
8485
* @returns The difference of the two operands
8586
*
86-
* @since v0.2.0
87+
* @since v0.6.0
88+
* @history v0.2.0: Originally named `sub`
8789
*/
8890
@unsafe
89-
provide let sub = (x: Float64, y: Float64) => {
91+
provide let (-) = (x: Float64, y: Float64) => {
9092
let xv = WasmF64.load(WasmI32.fromGrain(x), 8n)
9193
let yv = WasmF64.load(WasmI32.fromGrain(y), 8n)
9294
let ptr = newFloat64(xv - yv)
@@ -100,10 +102,11 @@ provide let sub = (x: Float64, y: Float64) => {
100102
* @param y: The second operand
101103
* @returns The product of the two operands
102104
*
103-
* @since v0.2.0
105+
* @since v0.6.0
106+
* @history v0.2.0: Originally named `mul`
104107
*/
105108
@unsafe
106-
provide let mul = (x: Float64, y: Float64) => {
109+
provide let (*) = (x: Float64, y: Float64) => {
107110
let xv = WasmF64.load(WasmI32.fromGrain(x), 8n)
108111
let yv = WasmF64.load(WasmI32.fromGrain(y), 8n)
109112
let ptr = newFloat64(xv * yv)
@@ -117,10 +120,11 @@ provide let mul = (x: Float64, y: Float64) => {
117120
* @param y: The second operand
118121
* @returns The quotient of the two operands
119122
*
120-
* @since v0.2.0
123+
* @since v0.6.0
124+
* @history v0.2.0: Originally named `div`
121125
*/
122126
@unsafe
123-
provide let div = (x: Float64, y: Float64) => {
127+
provide let (/) = (x: Float64, y: Float64) => {
124128
let xv = WasmF64.load(WasmI32.fromGrain(x), 8n)
125129
let yv = WasmF64.load(WasmI32.fromGrain(y), 8n)
126130
let ptr = newFloat64(xv / yv)
@@ -134,10 +138,11 @@ provide let div = (x: Float64, y: Float64) => {
134138
* @param y: The second value
135139
* @returns `true` if the first value is less than the second value or `false` otherwise
136140
*
137-
* @since v0.2.0
141+
* @since v0.6.0
142+
* @history v0.2.0: Originally named `lt`
138143
*/
139144
@unsafe
140-
provide let lt = (x: Float64, y: Float64) => {
145+
provide let (<) = (x: Float64, y: Float64) => {
141146
let xv = WasmF64.load(WasmI32.fromGrain(x), 8n)
142147
let yv = WasmF64.load(WasmI32.fromGrain(y), 8n)
143148
xv < yv
@@ -150,10 +155,11 @@ provide let lt = (x: Float64, y: Float64) => {
150155
* @param y: The second value
151156
* @returns `true` if the first value is greater than the second value or `false` otherwise
152157
*
153-
* @since v0.2.0
158+
* @since v0.6.0
159+
* @history v0.2.0: Originally named `gt`
154160
*/
155161
@unsafe
156-
provide let gt = (x: Float64, y: Float64) => {
162+
provide let (>) = (x: Float64, y: Float64) => {
157163
let xv = WasmF64.load(WasmI32.fromGrain(x), 8n)
158164
let yv = WasmF64.load(WasmI32.fromGrain(y), 8n)
159165
xv > yv
@@ -166,10 +172,11 @@ provide let gt = (x: Float64, y: Float64) => {
166172
* @param y: The second value
167173
* @returns `true` if the first value is less than or equal to the second value or `false` otherwise
168174
*
169-
* @since v0.2.0
175+
* @since v0.6.0
176+
* @history v0.2.0: Originally named `lte`
170177
*/
171178
@unsafe
172-
provide let lte = (x: Float64, y: Float64) => {
179+
provide let (<=) = (x: Float64, y: Float64) => {
173180
let xv = WasmF64.load(WasmI32.fromGrain(x), 8n)
174181
let yv = WasmF64.load(WasmI32.fromGrain(y), 8n)
175182
xv <= yv
@@ -182,10 +189,11 @@ provide let lte = (x: Float64, y: Float64) => {
182189
* @param y: The second value
183190
* @returns `true` if the first value is greater than or equal to the second value or `false` otherwise
184191
*
185-
* @since v0.2.0
192+
* @since v0.6.0
193+
* @history v0.2.0: Originally named `gte`
186194
*/
187195
@unsafe
188-
provide let gte = (x: Float64, y: Float64) => {
196+
provide let (>=) = (x: Float64, y: Float64) => {
189197
let xv = WasmF64.load(WasmI32.fromGrain(x), 8n)
190198
let yv = WasmF64.load(WasmI32.fromGrain(y), 8n)
191199
xv >= yv

0 commit comments

Comments
 (0)