Extreme floating point values: Difference between revisions

Standard ML solution
m (→‎{{header|Wren}}: Changed to Wren S/H)
imported>Vukung
(Standard ML solution)
 
(One intermediate revision by one other user not shown)
Line 1,345:
for positive and negative infinitesimal (though their usage is quite obscure),
and <code>und</code> for undefined value.
 
=={{header|MiniScript}}==
MiniScript doesn't have built-in constants for infinity and NaN though they can easily be generated from 'normal' 64 bit floating point values which is the underlying type of all numbers in the language.
 
Note that NaNs never compare equal, even to themselves. Note also that for some unknown reason (at least on Linux using the command line version) NaNs seem to always print as '-nan'.
<syntaxhighlight lang="miniscript">import "listUtil"
 
toBoolStr = function(n)
if n == 0 then return "false"
return "true"
end function
 
// create 'extreme' values from 'normal' values
negZero = -0
inf = 1 / 0
negInf = -1 / 0
nan = 0 / 0
 
// print them and do some arithmetic on them
print [inf, negInf, nan, negZero]
print [inf + inf, negInf + inf, inf * nan, nan * nan]
print [inf/inf, negInf/2, nan + inf, negZero/0]
 
// show some comparisons
comps = [negZero == 0, inf == -inf, inf == nan, nan == nan]
comps.apply @toBoolStr
print comps</syntaxhighlight>
 
{{out}}
<pre>[INF, -INF, -nan, -0]
[INF, -nan, -nan, -nan]
[-nan, -INF, -nan, -nan]
["true", "false", "false", "false"]</pre>
 
=={{header|MUMPS}}==
Line 2,359 ⟶ 2,392:
[ -1 log10 ] on:DomainError do:[:ex | ex proceed] -> nan
 
</syntaxhighlight>
 
 
=={{header|Standard ML}}==
Based on the C solution.
 
<syntaxhighlight lang="standardml">
let val inf = 1.0/0.0
val ninf = ~1.0/0.0
val nzero = ~0.0
val nan = 0.0/0.0
fun f (s, x) = print (s ^ " \t= " ^ Real.toString x ^ "\n")
fun g (s, x) = print (s ^ " \t= " ^ Bool.toString x ^ "\n")
in app f [("positive infinity", inf),
("negative infinity", ninf),
("negative zero", nzero),
("not a number", nan),
("+inf + 2.0", inf + 2.0),
("+inf - 10.1", inf - 10.1),
("+inf + -inf", inf + ninf),
("0.0 * +inf", 0.0 * inf),
("1.0/-0.0", 1.0 / nzero),
("NaN + 1.0", nan + 1.0),
("NaN + NaN", nan + nan)];
app g [("NaN == NaN", Real.==(nan, nan)),
("0.0 == -0.0", Real.==(0.0, nzero))]
end
</syntaxhighlight>
 
Output:
 
<syntaxhighlight>
positive infinity = inf
negative infinity = ~inf
negative zero = ~0.0
not a number = nan
+inf + 2.0 = inf
+inf - 10.1 = inf
+inf + -inf = nan
0.0 * +inf = nan
1.0/-0.0 = ~inf
NaN + 1.0 = nan
NaN + NaN = nan
NaN == NaN = false
0.0 == -0.0 = true
</syntaxhighlight>
 
Anonymous user