Extreme floating point values
NaN and Inf literals can be used to represent the Not-a-Number and Infinity values, which are returned in special cases, such as 0/0 and 1/0. However, one thing to notice, is that in Sidef there is no distinction between 0.0 and -0.0 and can't be differentiated from each other.
1
var inf = 1/0 # same as: Inf
2
var nan = 0/0 # same as: NaN
3
4
var exprs = [
5
"1.0 / 0.0", "-1.0 / 0.0", "0.0 / 0.0", "- 0.0",
6
"inf + 1", "5 - inf", "inf * 5", "inf / 5", "inf * 0",
7
"1.0 / inf", "-1.0 / inf", "inf + inf", "inf - inf",
8
"inf * inf", "inf / inf", "inf * 0.0", " 0 < inf", "inf == inf",
9
"nan + 1", "nan * 5", "nan - nan", "nan * inf", "- nan",
10
"nan == nan", "nan > 0", "nan < 0", "nan == 0", "0.0 == -0.0",
11
]
12
13
exprs.each { |expr|
14
"%15s => %s\n".printf(expr, eval(expr))
15
}
16
17
say "-"*40
18
say("NaN equality: ", NaN == nan)
19
say("Infinity equality: ", Inf == inf)
20
say("-Infinity equality: ", -Inf == -inf)
21
22
say "-"*40
23
say("sqrt(-1) = ", sqrt(-1))
24
say("tanh(-Inf) = ", tanh(-inf))
25
say("(-Inf)**2 = ", (-inf)**2)
26
say("(-Inf)**3 = ", (-inf)**3)
27
say("acos(Inf) = ", acos(inf))
28
say("atan(Inf) = ", atan(inf))
29
say("log(-1) = ", log(-1))
30
say("atanh(Inf) = ", atanh(inf))
Copied!

Output:

1
1.0 / 0.0 => Inf
2
-1.0 / 0.0 => -Inf
3
0.0 / 0.0 => NaN
4
- 0.0 => 0
5
inf + 1 => Inf
6
5 - inf => -Inf
7
inf * 5 => Inf
8
inf / 5 => Inf
9
inf * 0 => NaN
10
1.0 / inf => 0
11
-1.0 / inf => 0
12
inf + inf => Inf
13
inf - inf => NaN
14
inf * inf => Inf
15
inf / inf => NaN
16
inf * 0.0 => NaN
17
0 < inf => true
18
inf == inf => true
19
nan + 1 => NaN
20
nan * 5 => NaN
21
nan - nan => NaN
22
nan * inf => NaN
23
- nan => NaN
24
nan == nan => false
25
nan > 0 =>
26
nan < 0 =>
27
nan == 0 => false
28
0.0 == -0.0 => true
29
----------------------------------------
30
NaN equality: false
31
Infinity equality: true
32
-Infinity equality: true
33
----------------------------------------
34
sqrt(-1) = i
35
tanh(-Inf) = -1
36
(-Inf)**2 = Inf
37
(-Inf)**3 = -Inf
38
acos(Inf) = -Infi
39
atan(Inf) = 1.57079632679489661923132169163975144209858469969
40
log(-1) = 3.14159265358979323846264338327950288419716939938i
41
atanh(Inf) = 1.57079632679489661923132169163975144209858469969i
Copied!
Last modified 1yr ago
Copy link
Contents
Output: