Skip to content

Commit

Permalink
feat(stdlib)!: Replace Float64 arithmatic/comparison functions with…
Browse files Browse the repository at this point in the history
… operators (#1957)
  • Loading branch information
spotandjake authored Jan 25, 2024
1 parent 57f070c commit dea4cb5
Show file tree
Hide file tree
Showing 5 changed files with 143 additions and 78 deletions.
21 changes: 11 additions & 10 deletions compiler/test/stdlib/float64.test.gr
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
2 changes: 1 addition & 1 deletion compiler/test/suites/numbers.re
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down
6 changes: 3 additions & 3 deletions compiler/test/suites/strings.re
Original file line number Diff line number Diff line change
Expand Up @@ -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",
);

Expand Down
40 changes: 24 additions & 16 deletions stdlib/float64.gr
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down
Loading

0 comments on commit dea4cb5

Please sign in to comment.