Skip to content

Commit

Permalink
feat(stdlib): Add abs, neg, isNaN, isInfinite to Float32 (#…
Browse files Browse the repository at this point in the history
…2116)

Co-authored-by: Oscar Spencer <[email protected]>
  • Loading branch information
spotandjake and ospencer committed Jul 28, 2024
1 parent 9469346 commit fb1d481
Show file tree
Hide file tree
Showing 3 changed files with 264 additions and 0 deletions.
32 changes: 32 additions & 0 deletions compiler/test/stdlib/float32.test.gr
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,35 @@ assert compare(1.0f, 1.0f) == 0
assert compare(nan, nan) == 0
assert compare(1.0f, nan) > 0
assert compare(nan, 1.0f) < 0

// isNaN
assert Float32.isNaN(NaNf)
assert Float32.isNaN(1.0f) == false
assert Float32.isNaN(0.0f) == false
assert Float32.isNaN(-1.0f) == false
assert Float32.isNaN(25.76f) == false
assert Float32.isNaN(-25.00f) == false
assert Float32.isNaN(Infinityf) == false
assert Float32.isNaN(-Infinityf) == false

// isInfinite
assert Float32.isInfinite(Infinityf)
assert Float32.isInfinite(-Infinityf)
assert Float32.isInfinite(NaNf) == false
assert Float32.isInfinite(1.0f) == false
assert Float32.isInfinite(0.0f) == false
assert Float32.isInfinite(-1.0f) == false
assert Float32.isInfinite(25.76f) == false
assert Float32.isInfinite(-25.00f) == false

// abs
assert Float32.abs(-25.5f) == 25.5f
assert Float32.abs(25.5f) == 25.5f
assert Float32.isNaN(Float32.abs(NaNf))
assert Float32.abs(Infinityf) == Infinityf

// neg
assert Float32.neg(-25.5f) == 25.5f
assert Float32.neg(25.5f) == -25.5f
assert Float32.isNaN(-NaNf)
assert Float32.neg(Infinityf) == -Infinityf
68 changes: 68 additions & 0 deletions stdlib/float32.gr
Original file line number Diff line number Diff line change
Expand Up @@ -247,3 +247,71 @@ provide let (>=) = (x: Float32, y: Float32) => {
let yv = WasmF32.load(WasmI32.fromGrain(y), _VALUE_OFFSET)
xv >= yv
}

/**
* Checks if the value is a float NaN value (Not A Number).
*
* @param x: The value to check
* @returns `true` if the value is NaN, otherwise `false`
*
* @example Float32.isNaN(NaNf)
* @example Float32.isNaN(Infinityf) == false
* @example Float32.isNaN(-Infinityf) == false
* @example Float32.isNaN(0.5f) == false
* @example Float32.isNaN(1.0f) == false
*
* @since v0.6.5
*/
provide let isNaN = (x: Float32) => x != x

/**
* Checks if a float is infinite, that is either of positive or negative infinity.
*
* @param x: The value to check
* @returns `true` if the value is infinite or `false` otherwise
*
* @example Float32.isInfinite(Infinityf)
* @example Float32.isInfinite(-Infinityf)
* @example Float32.isInfinite(NaNf) == false
* @example Float32.isInfinite(0.5f) == false
* @example Float32.isInfinite(1.0f) == false
*
* @since v0.6.5
*/
provide let isInfinite = (x: Float32) => x == Infinityf || x == -Infinityf

/**
* Returns the absolute value. That is, it returns `x` if `x` is positive or zero and the negation of `x` if `x` is negative.
*
* @param x: The operand
* @returns The absolute value of the operand
*
* @example Float32.abs(-1.0f) == 1.0f
* @example Float32.abs(5.0f) == 5.0f
*
* @since v0.6.5
*/
@unsafe
provide let abs = (x: Float32) => {
let xv = WasmF32.load(WasmI32.fromGrain(x), _VALUE_OFFSET)
let ptr = newFloat32(WasmF32.abs(xv))
WasmI32.toGrain(ptr): Float32
}

/**
* Returns the negation of its operand.
*
* @param x: The operand
* @returns The negated operand
*
* @example Float32.neg(-1.0f) == 1.0f
* @example Float32.neg(1.0f) == -1.0f
*
* @since v0.6.5
*/
@unsafe
provide let neg = (x: Float32) => {
let xv = WasmF32.load(WasmI32.fromGrain(x), _VALUE_OFFSET)
let ptr = newFloat32(WasmF32.neg(xv))
WasmI32.toGrain(ptr): Float32
}
164 changes: 164 additions & 0 deletions stdlib/float32.md
Original file line number Diff line number Diff line change
Expand Up @@ -480,3 +480,167 @@ use Float32.{ (>=) }
assert 3.0f >= 3.0f
```

### Float32.**isNaN**

<details disabled>
<summary tabindex="-1">Added in <code>next</code></summary>
No other changes yet.
</details>

```grain
isNaN : (x: Float32) => Bool
```

Checks if the value is a float NaN value (Not A Number).

Parameters:

|param|type|description|
|-----|----|-----------|
|`x`|`Float32`|The value to check|

Returns:

|type|description|
|----|-----------|
|`Bool`|`true` if the value is NaN, otherwise `false`|

Examples:

```grain
Float32.isNaN(NaNf)
```

```grain
Float32.isNaN(Infinityf) == false
```

```grain
Float32.isNaN(-Infinityf) == false
```

```grain
Float32.isNaN(0.5f) == false
```

```grain
Float32.isNaN(1.0f) == false
```

### Float32.**isInfinite**

<details disabled>
<summary tabindex="-1">Added in <code>next</code></summary>
No other changes yet.
</details>

```grain
isInfinite : (x: Float32) => Bool
```

Checks if a float is infinite, that is either of positive or negative infinity.

Parameters:

|param|type|description|
|-----|----|-----------|
|`x`|`Float32`|The value to check|

Returns:

|type|description|
|----|-----------|
|`Bool`|`true` if the value is infinite or `false` otherwise|

Examples:

```grain
Float32.isInfinite(Infinityf)
```

```grain
Float32.isInfinite(-Infinityf)
```

```grain
Float32.isInfinite(NaNf) == false
```

```grain
Float32.isInfinite(0.5f) == false
```

```grain
Float32.isInfinite(1.0f) == false
```

### Float32.**abs**

<details disabled>
<summary tabindex="-1">Added in <code>next</code></summary>
No other changes yet.
</details>

```grain
abs : (x: Float32) => Float32
```

Returns the absolute value. That is, it returns `x` if `x` is positive or zero and the negation of `x` if `x` is negative.

Parameters:

|param|type|description|
|-----|----|-----------|
|`x`|`Float32`|The operand|

Returns:

|type|description|
|----|-----------|
|`Float32`|The absolute value of the operand|

Examples:

```grain
Float32.abs(-1.0f) == 1.0f
```

```grain
Float32.abs(5.0f) == 5.0f
```

### Float32.**neg**

<details disabled>
<summary tabindex="-1">Added in <code>next</code></summary>
No other changes yet.
</details>

```grain
neg : (x: Float32) => Float32
```

Returns the negation of its operand.

Parameters:

|param|type|description|
|-----|----|-----------|
|`x`|`Float32`|The operand|

Returns:

|type|description|
|----|-----------|
|`Float32`|The negated operand|

Examples:

```grain
Float32.neg(-1.0f) == 1.0f
```

```grain
Float32.neg(1.0f) == -1.0f
```

0 comments on commit fb1d481

Please sign in to comment.