From dea4cb54d10b9cecbf54ed30048c0937bc38cea2 Mon Sep 17 00:00:00 2001 From: Spotandjake <40705786+spotandjake@users.noreply.github.com> Date: Wed, 24 Jan 2024 21:36:25 -0500 Subject: [PATCH] feat(stdlib)!: Replace `Float64` arithmatic/comparison functions with operators (#1957) --- compiler/test/stdlib/float64.test.gr | 21 ++-- compiler/test/suites/numbers.re | 2 +- compiler/test/suites/strings.re | 6 +- stdlib/float64.gr | 40 ++++--- stdlib/float64.md | 152 ++++++++++++++++++--------- 5 files changed, 143 insertions(+), 78 deletions(-) diff --git a/compiler/test/stdlib/float64.test.gr b/compiler/test/stdlib/float64.test.gr index d0899e27ca..82b8017315 100644 --- a/compiler/test/stdlib/float64.test.gr +++ b/compiler/test/stdlib/float64.test.gr @@ -5,9 +5,10 @@ from Float64 use * // Constants Tests // smoke test: -assert gt(infinity, 100000000.0d) +assert infinity > 100000000.0d // test infinity-specific semantics: -assert toNumber(infinity) == toNumber(infinity) - 1 +from Pervasives use { (-) as numberSub } +assert toNumber(infinity) == numberSub(toNumber(infinity), 1) assert nan != nan assert pi == 3.141592653589793d @@ -24,11 +25,11 @@ assert fromNumber(0) == 0.0d assert toNumber(555.0d) == 555 assert toNumber(0.0d) == 0 -assert gt(5.0d, 4.0d) -assert gte(5.0d, 5.0d) -assert lt(5.0d, 17.0d) -assert lte(5.0d, 5.0d) -assert !gt(5.0d, 5.0d) -assert !gte(5.0d, 22.0d) -assert !lt(5.0d, -17.0d) -assert !lte(5.0d, 4.0d) +assert 5.0d > 4.0d +assert 5.0d >= 5.0d +assert 5.0d < 17.0d +assert 5.0d <= 5.0d +assert !(5.0d > 5.0d) +assert !(5.0d >= 22.0d) +assert !(5.0d < -17.0d) +assert !(5.0d <= 4.0d) diff --git a/compiler/test/suites/numbers.re b/compiler/test/suites/numbers.re index df22c8626a..4f2c58426a 100644 --- a/compiler/test/suites/numbers.re +++ b/compiler/test/suites/numbers.re @@ -94,7 +94,7 @@ describe("numbers", ({test, testSkip}) => { assertRun("nan_equality1", {|print(NaNf == NaNf)|}, "false\n"); assertRun( "nan_equality2", - {|include "float64"; print(Float64.div(0.0d, 0.0d) == Float64.div(0.0d, 0.0d))|}, + {|include "float64"; from Float64 use { (/) }; print((0.0d / 0.0d) == (0.0d / 0.0d))|}, "false\n", ); assertRun("nan_equality3", {|print(0.0 / 0.0 == 0.0 / 0.0)|}, "false\n"); diff --git a/compiler/test/suites/strings.re b/compiler/test/suites/strings.re index f9e31b872a..c5393a96bf 100644 --- a/compiler/test/suites/strings.re +++ b/compiler/test/suites/strings.re @@ -270,17 +270,17 @@ bar", 1))|}, assertRun("string_float3", {|print(-Infinityf)|}, "-Infinity\n"); assertRun( "string_float4", - {|include "float64"; from Float64 use *; print(div(0.0d, 0.0d))|}, + {|include "float64"; from Float64 use *; print(0.0d / 0.0d)|}, "NaN\n", ); assertRun( "string_float5", - {|include "float64"; from Float64 use *; print(div(1.0d, 0.0d))|}, + {|include "float64"; from Float64 use *; print(1.0d / 0.0d)|}, "Infinity\n", ); assertRun( "string_float6", - {|include "float64"; from Float64 use *; print(div(-1.0d, 0.0d))|}, + {|include "float64"; from Float64 use *; print(-1.0d / 0.0d)|}, "-Infinity\n", ); diff --git a/stdlib/float64.gr b/stdlib/float64.gr index bcdb2a8f72..502da340bf 100644 --- a/stdlib/float64.gr +++ b/stdlib/float64.gr @@ -66,10 +66,11 @@ provide { fromNumber, toNumber } * @param y: The second operand * @returns The sum of the two operands * - * @since v0.2.0 + * @since v0.6.0 + * @history v0.2.0: Originally named `add` */ @unsafe -provide let add = (x: Float64, y: Float64) => { +provide let (+) = (x: Float64, y: Float64) => { let xv = WasmF64.load(WasmI32.fromGrain(x), 8n) let yv = WasmF64.load(WasmI32.fromGrain(y), 8n) let ptr = newFloat64(xv + yv) @@ -83,10 +84,11 @@ provide let add = (x: Float64, y: Float64) => { * @param y: The second operand * @returns The difference of the two operands * - * @since v0.2.0 + * @since v0.6.0 + * @history v0.2.0: Originally named `sub` */ @unsafe -provide let sub = (x: Float64, y: Float64) => { +provide let (-) = (x: Float64, y: Float64) => { let xv = WasmF64.load(WasmI32.fromGrain(x), 8n) let yv = WasmF64.load(WasmI32.fromGrain(y), 8n) let ptr = newFloat64(xv - yv) @@ -100,10 +102,11 @@ provide let sub = (x: Float64, y: Float64) => { * @param y: The second operand * @returns The product of the two operands * - * @since v0.2.0 + * @since v0.6.0 + * @history v0.2.0: Originally named `mul` */ @unsafe -provide let mul = (x: Float64, y: Float64) => { +provide let (*) = (x: Float64, y: Float64) => { let xv = WasmF64.load(WasmI32.fromGrain(x), 8n) let yv = WasmF64.load(WasmI32.fromGrain(y), 8n) let ptr = newFloat64(xv * yv) @@ -117,10 +120,11 @@ provide let mul = (x: Float64, y: Float64) => { * @param y: The second operand * @returns The quotient of the two operands * - * @since v0.2.0 + * @since v0.6.0 + * @history v0.2.0: Originally named `div` */ @unsafe -provide let div = (x: Float64, y: Float64) => { +provide let (/) = (x: Float64, y: Float64) => { let xv = WasmF64.load(WasmI32.fromGrain(x), 8n) let yv = WasmF64.load(WasmI32.fromGrain(y), 8n) let ptr = newFloat64(xv / yv) @@ -134,10 +138,11 @@ provide let div = (x: Float64, y: Float64) => { * @param y: The second value * @returns `true` if the first value is less than the second value or `false` otherwise * - * @since v0.2.0 + * @since v0.6.0 + * @history v0.2.0: Originally named `lt` */ @unsafe -provide let lt = (x: Float64, y: Float64) => { +provide let (<) = (x: Float64, y: Float64) => { let xv = WasmF64.load(WasmI32.fromGrain(x), 8n) let yv = WasmF64.load(WasmI32.fromGrain(y), 8n) xv < yv @@ -150,10 +155,11 @@ provide let lt = (x: Float64, y: Float64) => { * @param y: The second value * @returns `true` if the first value is greater than the second value or `false` otherwise * - * @since v0.2.0 + * @since v0.6.0 + * @history v0.2.0: Originally named `gt` */ @unsafe -provide let gt = (x: Float64, y: Float64) => { +provide let (>) = (x: Float64, y: Float64) => { let xv = WasmF64.load(WasmI32.fromGrain(x), 8n) let yv = WasmF64.load(WasmI32.fromGrain(y), 8n) xv > yv @@ -166,10 +172,11 @@ provide let gt = (x: Float64, y: Float64) => { * @param y: The second value * @returns `true` if the first value is less than or equal to the second value or `false` otherwise * - * @since v0.2.0 + * @since v0.6.0 + * @history v0.2.0: Originally named `lte` */ @unsafe -provide let lte = (x: Float64, y: Float64) => { +provide let (<=) = (x: Float64, y: Float64) => { let xv = WasmF64.load(WasmI32.fromGrain(x), 8n) let yv = WasmF64.load(WasmI32.fromGrain(y), 8n) xv <= yv @@ -182,10 +189,11 @@ provide let lte = (x: Float64, y: Float64) => { * @param y: The second value * @returns `true` if the first value is greater than or equal to the second value or `false` otherwise * - * @since v0.2.0 + * @since v0.6.0 + * @history v0.2.0: Originally named `gte` */ @unsafe -provide let gte = (x: Float64, y: Float64) => { +provide let (>=) = (x: Float64, y: Float64) => { let xv = WasmF64.load(WasmI32.fromGrain(x), 8n) let yv = WasmF64.load(WasmI32.fromGrain(y), 8n) xv >= yv diff --git a/stdlib/float64.md b/stdlib/float64.md index b2b8aa0779..64a8b4d17e 100644 --- a/stdlib/float64.md +++ b/stdlib/float64.md @@ -132,15 +132,22 @@ Returns: |----|-----------| |`Number`|The Float64 represented as a Number| -### Float64.**add** - -
-Added in 0.2.0 -No other changes yet. +### Float64.**(+)** + +
+Added in next + + + + + + + +
versionchanges
0.2.0Originally named `add`
```grain -add : (x: Float64, y: Float64) => Float64 +(+) : (x: Float64, y: Float64) => Float64 ``` Computes the sum of its operands. @@ -158,15 +165,22 @@ Returns: |----|-----------| |`Float64`|The sum of the two operands| -### Float64.**sub** - -
-Added in 0.2.0 -No other changes yet. +### Float64.**(-)** + +
+Added in next + + + + + + + +
versionchanges
0.2.0Originally named `sub`
```grain -sub : (x: Float64, y: Float64) => Float64 +(-) : (x: Float64, y: Float64) => Float64 ``` Computes the difference of its operands. @@ -184,15 +198,22 @@ Returns: |----|-----------| |`Float64`|The difference of the two operands| -### Float64.**mul** - -
-Added in 0.2.0 -No other changes yet. +### Float64.**(*)** + +
+Added in next + + + + + + + +
versionchanges
0.2.0Originally named `mul`
```grain -mul : (x: Float64, y: Float64) => Float64 +(*) : (x: Float64, y: Float64) => Float64 ``` Computes the product of its operands. @@ -210,15 +231,22 @@ Returns: |----|-----------| |`Float64`|The product of the two operands| -### Float64.**div** - -
-Added in 0.2.0 -No other changes yet. +### Float64.**(/)** + +
+Added in next + + + + + + + +
versionchanges
0.2.0Originally named `div`
```grain -div : (x: Float64, y: Float64) => Float64 +(/) : (x: Float64, y: Float64) => Float64 ``` Computes the quotient of its operands. @@ -236,15 +264,22 @@ Returns: |----|-----------| |`Float64`|The quotient of the two operands| -### Float64.**lt** - -
-Added in 0.2.0 -No other changes yet. +### Float64.**(<)** + +
+Added in next + + + + + + + +
versionchanges
0.2.0Originally named `lt`
```grain -lt : (x: Float64, y: Float64) => Bool +(<) : (x: Float64, y: Float64) => Bool ``` Checks if the first value is less than the second value. @@ -262,15 +297,22 @@ Returns: |----|-----------| |`Bool`|`true` if the first value is less than the second value or `false` otherwise| -### Float64.**gt** - -
-Added in 0.2.0 -No other changes yet. +### Float64.**(>)** + +
+Added in next + + + + + + + +
versionchanges
0.2.0Originally named `gt`
```grain -gt : (x: Float64, y: Float64) => Bool +(>) : (x: Float64, y: Float64) => Bool ``` Checks if the first value is greater than the second value. @@ -288,15 +330,22 @@ Returns: |----|-----------| |`Bool`|`true` if the first value is greater than the second value or `false` otherwise| -### Float64.**lte** - -
-Added in 0.2.0 -No other changes yet. +### Float64.**(<=)** + +
+Added in next + + + + + + + +
versionchanges
0.2.0Originally named `lte`
```grain -lte : (x: Float64, y: Float64) => Bool +(<=) : (x: Float64, y: Float64) => Bool ``` Checks if the first value is less than or equal to the second value. @@ -314,15 +363,22 @@ Returns: |----|-----------| |`Bool`|`true` if the first value is less than or equal to the second value or `false` otherwise| -### Float64.**gte** - -
-Added in 0.2.0 -No other changes yet. +### Float64.**(>=)** + +
+Added in next + + + + + + + +
versionchanges
0.2.0Originally named `gte`
```grain -gte : (x: Float64, y: Float64) => Bool +(>=) : (x: Float64, y: Float64) => Bool ``` Checks if the first value is greater than or equal to the second value.