Skip to content

Commit fb1d481

Browse files
feat(stdlib): Add abs, neg, isNaN, isInfinite to Float32 (#2116)
Co-authored-by: Oscar Spencer <oscar@grain-lang.org>
1 parent 9469346 commit fb1d481

File tree

3 files changed

+264
-0
lines changed

3 files changed

+264
-0
lines changed

compiler/test/stdlib/float32.test.gr

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,3 +41,35 @@ assert compare(1.0f, 1.0f) == 0
4141
assert compare(nan, nan) == 0
4242
assert compare(1.0f, nan) > 0
4343
assert compare(nan, 1.0f) < 0
44+
45+
// isNaN
46+
assert Float32.isNaN(NaNf)
47+
assert Float32.isNaN(1.0f) == false
48+
assert Float32.isNaN(0.0f) == false
49+
assert Float32.isNaN(-1.0f) == false
50+
assert Float32.isNaN(25.76f) == false
51+
assert Float32.isNaN(-25.00f) == false
52+
assert Float32.isNaN(Infinityf) == false
53+
assert Float32.isNaN(-Infinityf) == false
54+
55+
// isInfinite
56+
assert Float32.isInfinite(Infinityf)
57+
assert Float32.isInfinite(-Infinityf)
58+
assert Float32.isInfinite(NaNf) == false
59+
assert Float32.isInfinite(1.0f) == false
60+
assert Float32.isInfinite(0.0f) == false
61+
assert Float32.isInfinite(-1.0f) == false
62+
assert Float32.isInfinite(25.76f) == false
63+
assert Float32.isInfinite(-25.00f) == false
64+
65+
// abs
66+
assert Float32.abs(-25.5f) == 25.5f
67+
assert Float32.abs(25.5f) == 25.5f
68+
assert Float32.isNaN(Float32.abs(NaNf))
69+
assert Float32.abs(Infinityf) == Infinityf
70+
71+
// neg
72+
assert Float32.neg(-25.5f) == 25.5f
73+
assert Float32.neg(25.5f) == -25.5f
74+
assert Float32.isNaN(-NaNf)
75+
assert Float32.neg(Infinityf) == -Infinityf

stdlib/float32.gr

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -247,3 +247,71 @@ provide let (>=) = (x: Float32, y: Float32) => {
247247
let yv = WasmF32.load(WasmI32.fromGrain(y), _VALUE_OFFSET)
248248
xv >= yv
249249
}
250+
251+
/**
252+
* Checks if the value is a float NaN value (Not A Number).
253+
*
254+
* @param x: The value to check
255+
* @returns `true` if the value is NaN, otherwise `false`
256+
*
257+
* @example Float32.isNaN(NaNf)
258+
* @example Float32.isNaN(Infinityf) == false
259+
* @example Float32.isNaN(-Infinityf) == false
260+
* @example Float32.isNaN(0.5f) == false
261+
* @example Float32.isNaN(1.0f) == false
262+
*
263+
* @since v0.6.5
264+
*/
265+
provide let isNaN = (x: Float32) => x != x
266+
267+
/**
268+
* Checks if a float is infinite, that is either of positive or negative infinity.
269+
*
270+
* @param x: The value to check
271+
* @returns `true` if the value is infinite or `false` otherwise
272+
*
273+
* @example Float32.isInfinite(Infinityf)
274+
* @example Float32.isInfinite(-Infinityf)
275+
* @example Float32.isInfinite(NaNf) == false
276+
* @example Float32.isInfinite(0.5f) == false
277+
* @example Float32.isInfinite(1.0f) == false
278+
*
279+
* @since v0.6.5
280+
*/
281+
provide let isInfinite = (x: Float32) => x == Infinityf || x == -Infinityf
282+
283+
/**
284+
* Returns the absolute value. That is, it returns `x` if `x` is positive or zero and the negation of `x` if `x` is negative.
285+
*
286+
* @param x: The operand
287+
* @returns The absolute value of the operand
288+
*
289+
* @example Float32.abs(-1.0f) == 1.0f
290+
* @example Float32.abs(5.0f) == 5.0f
291+
*
292+
* @since v0.6.5
293+
*/
294+
@unsafe
295+
provide let abs = (x: Float32) => {
296+
let xv = WasmF32.load(WasmI32.fromGrain(x), _VALUE_OFFSET)
297+
let ptr = newFloat32(WasmF32.abs(xv))
298+
WasmI32.toGrain(ptr): Float32
299+
}
300+
301+
/**
302+
* Returns the negation of its operand.
303+
*
304+
* @param x: The operand
305+
* @returns The negated operand
306+
*
307+
* @example Float32.neg(-1.0f) == 1.0f
308+
* @example Float32.neg(1.0f) == -1.0f
309+
*
310+
* @since v0.6.5
311+
*/
312+
@unsafe
313+
provide let neg = (x: Float32) => {
314+
let xv = WasmF32.load(WasmI32.fromGrain(x), _VALUE_OFFSET)
315+
let ptr = newFloat32(WasmF32.neg(xv))
316+
WasmI32.toGrain(ptr): Float32
317+
}

stdlib/float32.md

Lines changed: 164 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -480,3 +480,167 @@ use Float32.{ (>=) }
480480
assert 3.0f >= 3.0f
481481
```
482482

483+
### Float32.**isNaN**
484+
485+
<details disabled>
486+
<summary tabindex="-1">Added in <code>next</code></summary>
487+
No other changes yet.
488+
</details>
489+
490+
```grain
491+
isNaN : (x: Float32) => Bool
492+
```
493+
494+
Checks if the value is a float NaN value (Not A Number).
495+
496+
Parameters:
497+
498+
|param|type|description|
499+
|-----|----|-----------|
500+
|`x`|`Float32`|The value to check|
501+
502+
Returns:
503+
504+
|type|description|
505+
|----|-----------|
506+
|`Bool`|`true` if the value is NaN, otherwise `false`|
507+
508+
Examples:
509+
510+
```grain
511+
Float32.isNaN(NaNf)
512+
```
513+
514+
```grain
515+
Float32.isNaN(Infinityf) == false
516+
```
517+
518+
```grain
519+
Float32.isNaN(-Infinityf) == false
520+
```
521+
522+
```grain
523+
Float32.isNaN(0.5f) == false
524+
```
525+
526+
```grain
527+
Float32.isNaN(1.0f) == false
528+
```
529+
530+
### Float32.**isInfinite**
531+
532+
<details disabled>
533+
<summary tabindex="-1">Added in <code>next</code></summary>
534+
No other changes yet.
535+
</details>
536+
537+
```grain
538+
isInfinite : (x: Float32) => Bool
539+
```
540+
541+
Checks if a float is infinite, that is either of positive or negative infinity.
542+
543+
Parameters:
544+
545+
|param|type|description|
546+
|-----|----|-----------|
547+
|`x`|`Float32`|The value to check|
548+
549+
Returns:
550+
551+
|type|description|
552+
|----|-----------|
553+
|`Bool`|`true` if the value is infinite or `false` otherwise|
554+
555+
Examples:
556+
557+
```grain
558+
Float32.isInfinite(Infinityf)
559+
```
560+
561+
```grain
562+
Float32.isInfinite(-Infinityf)
563+
```
564+
565+
```grain
566+
Float32.isInfinite(NaNf) == false
567+
```
568+
569+
```grain
570+
Float32.isInfinite(0.5f) == false
571+
```
572+
573+
```grain
574+
Float32.isInfinite(1.0f) == false
575+
```
576+
577+
### Float32.**abs**
578+
579+
<details disabled>
580+
<summary tabindex="-1">Added in <code>next</code></summary>
581+
No other changes yet.
582+
</details>
583+
584+
```grain
585+
abs : (x: Float32) => Float32
586+
```
587+
588+
Returns the absolute value. That is, it returns `x` if `x` is positive or zero and the negation of `x` if `x` is negative.
589+
590+
Parameters:
591+
592+
|param|type|description|
593+
|-----|----|-----------|
594+
|`x`|`Float32`|The operand|
595+
596+
Returns:
597+
598+
|type|description|
599+
|----|-----------|
600+
|`Float32`|The absolute value of the operand|
601+
602+
Examples:
603+
604+
```grain
605+
Float32.abs(-1.0f) == 1.0f
606+
```
607+
608+
```grain
609+
Float32.abs(5.0f) == 5.0f
610+
```
611+
612+
### Float32.**neg**
613+
614+
<details disabled>
615+
<summary tabindex="-1">Added in <code>next</code></summary>
616+
No other changes yet.
617+
</details>
618+
619+
```grain
620+
neg : (x: Float32) => Float32
621+
```
622+
623+
Returns the negation of its operand.
624+
625+
Parameters:
626+
627+
|param|type|description|
628+
|-----|----|-----------|
629+
|`x`|`Float32`|The operand|
630+
631+
Returns:
632+
633+
|type|description|
634+
|----|-----------|
635+
|`Float32`|The negated operand|
636+
637+
Examples:
638+
639+
```grain
640+
Float32.neg(-1.0f) == 1.0f
641+
```
642+
643+
```grain
644+
Float32.neg(1.0f) == -1.0f
645+
```
646+

0 commit comments

Comments
 (0)