Skip to content

Commit 17c0f8a

Browse files
authored
Add INFINITY and NAN constants to stdlib/math.jou (#1267)
1 parent 3e10f7a commit 17c0f8a

File tree

7 files changed

+24
-35
lines changed

7 files changed

+24
-35
lines changed

doc/json-building.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -107,13 +107,14 @@ For example:
107107
import "stdlib/json.jou"
108108
import "stdlib/io.jou"
109109
import "stdlib/mem.jou"
110+
import "stdlib/math.jou" # defines INFINITY and NAN
110111

111112
def main() -> int:
112113
jb = JSONBuilder{}
113114
jb.begin_array()
114-
jb.number(1.0 / 0.0)
115-
jb.number(-1.0 / 0.0)
116-
jb.number(0.0 / 0.0)
115+
jb.number(INFINITY)
116+
jb.number(-INFINITY)
117+
jb.number(NAN)
117118
jb.end_array()
118119

119120
json = jb.finish()

doc/types.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -177,15 +177,15 @@ For example:
177177

178178
```python
179179
import "stdlib/io.jou"
180+
import "stdlib/math.jou"
180181

181182
def main() -> int:
182183
printf("%d\n", 1234.5 as byte) # Output: 255
183184
printf("%d\n", 24.68 as int) # Output: 24
184185
printf("%d\n", -24.68 as int) # Output: -24
185186

186-
# Infinity and NaN
187-
printf("%d\n", (1.0 / 0.0) as int) # Output: 2147483647
188-
printf("%d\n", (0.0 / 0.0) as int) # Output: 0
187+
printf("%d\n", INFINITY as int) # Output: 2147483647
188+
printf("%d\n", NAN as int) # Output: 0
189189

190190
return 0
191191
```

stdlib/json.jou

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -495,19 +495,19 @@ def skip_number(s: byte*) -> byte*:
495495
@public
496496
def json_to_double(s: byte*) -> double:
497497
if s == NULL:
498-
return 0.0 / 0.0
498+
return NAN
499499

500500
while is_json_whitespace(*s):
501501
s++
502502
if skip_number(s) == NULL:
503-
return 0.0 / 0.0
503+
return NAN
504504

505505
if starts_with(s, "Infinity"):
506-
return 1.0 / 0.0
506+
return INFINITY
507507
if starts_with(s, "-Infinity"):
508-
return -1.0 / 0.0
508+
return -INFINITY
509509
if starts_with(s, "NaN"):
510-
return 0.0 / 0.0
510+
return NAN
511511

512512
return atof(s)
513513

stdlib/math.jou

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -64,14 +64,13 @@ declare fmaxf(a: float, b: float) -> float
6464
# - infinite (plus infinity or minus infinity)
6565
# - NaN (Not a Number, basically indicates that something went wrong)
6666
#
67-
# To create infinite values, you can use e.g. 1.0/0.0 or -1.0/0.0. To create a
68-
# NaN, you can use 0.0/0.0.
69-
#
70-
# The following functions check which kind of number you are dealing with. They
71-
# work with both floats and doubles. Please create an issue on GitHub if you
72-
# need float-only versions of these functions for some reason.
67+
# Like M_PI and M_E, these are doubles. Use "as float" as needed.
68+
@public
69+
const INFINITY: double = 1.0 / 0.0
70+
@public
71+
const NAN: double = 0.0 / 0.0
7372

74-
# Returns True if x a plain old finite value.
73+
# Returns True if x is a finite value, not infinity or minus infinity or NaN.
7574
@public
7675
@inline
7776
def isfinite(x: double) -> bool:

tests/should_succeed/json_test_builder.jou

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -195,9 +195,9 @@ def test_numbers() -> None:
195195
jb.number(pow(2, -20))
196196
# Technically Infinity and NaN are not valid JSON, but many libraries
197197
# support them to some extent so we support them too.
198-
jb.number(1.0 / 0.0) # Infinity
199-
jb.number(-1.0 / 0.0) # -Infinity
200-
jb.number(0.0 / 0.0) # NaN
198+
jb.number(INFINITY)
199+
jb.number(-INFINITY)
200+
jb.number(NAN)
201201
jb.end_array()
202202

203203
json = jb.finish()
@@ -305,7 +305,7 @@ def test_complex_example() -> None:
305305
jb.key("alternative null")
306306
jb.string(NULL)
307307
jb.key("big numba")
308-
jb.number(1.0 / 0.0)
308+
jb.number(INFINITY)
309309
jb.end_object()
310310
jb.end_array()
311311

tests/should_succeed/json_test_parsing.jou

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -362,9 +362,9 @@ def test_numbers() -> None:
362362
assert not json_is_valid("5k")
363363
assert json_to_double("5k") == 5
364364
assert not json_is_valid("Infinityyyy")
365-
assert json_to_double("Infinityyyy") == 1.0 / 0.0
365+
assert json_to_double("Infinityyyy") == INFINITY
366366
assert not json_is_valid("-Infinityyyy")
367-
assert json_to_double("-Infinityyyy") == -1.0 / 0.0
367+
assert json_to_double("-Infinityyyy") == -INFINITY
368368

369369

370370
def test_json_from_github_api() -> None:

tests/should_succeed/math_test.jou

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,6 @@
11
import "stdlib/math.jou"
22
import "stdlib/io.jou"
33

4-
# TODO: move these to stdlib/math.jou
5-
#
6-
# Here are some more readable ways to construct non-finite values. Note that
7-
# there are multiple different NaN values (see also: nan boxing), and the NaN
8-
# constructed here is just one of them.
9-
#
10-
# Like M_PI and M_E, these are doubles. Use "as float" as needed.
11-
@public
12-
const INFINITY: double = 1.0 / 0.0
13-
@public
14-
const NAN: double = 0.0 / 0.0
154

165
def main() -> int:
176
printf("%f\n", M_PI) # Output: 3.141593

0 commit comments

Comments
 (0)