Extreme floating point values: Difference between revisions

Standard ML solution
(Added XPL0 example.)
imported>Vukung
(Standard ML solution)
 
(8 intermediate revisions by 2 users not shown)
Line 613:
Readln;
end.</syntaxhighlight>
 
=={{header|EasyLang}}==
{{trans|C}}
<syntaxhighlight lang=easylang>
inf = 1 / 0
minus_inf = -1 / 0
minus_zero = -1 / inf
nan = 0.0 / 0.0
#
# in Easylang there is -0, but when
# converting it to a string it becomes "0"
#
print "positive infinity: " & inf
print "negative infinity: " & minus_inf
print "negative zero: " & minus_zero
print "not a number: " & nan
#
# some arithmetic
print "+inf + 2 = " & inf + 2
print "+inf - 10.1 = " & inf - 10.1
print "+inf + -inf = " & inf + minus_inf
print "0 * +inf = " & 0 * inf
print "1/-0 = " & 1 / minus_zero
print "NaN + 1 = " & nan + 1
print "NaN + NaN = " & nan + nan
#
# some comparisons
print "NaN == NaN = " & if nan = nan
print "0 = -0 = " & if 0 = minus_zero
</syntaxhighlight>
 
=={{header|Eiffel}}==
Line 1,315 ⟶ 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,329 ⟶ 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>
 
Line 2,410 ⟶ 2,518:
 
=={{header|Wren}}==
Wren has built-in constants for infinity and NaN though they can also be generated from 'normal' 64 bit floating point values which is the underlying type of all numbers in Wren.
With the exception of negative zero, ''extreme'' floating point values cannot be entered directly in Wren but they are easy to manufacture and perform arithmetic on.
 
<syntaxhighlight lang="ecmascript">var inf = 1/0
Note that when using the built-in operators NaN's never compare equal, even to themselves. Also, internally, Wren generates several different NaN values but the value returned by the built-in constant ''Num.nan'' is always the minimum 'quiet' NaN whose bit value is 0x7FF800000000000. The ''Object.same'' method reveals whether NaNs have the same bit value or not.
var negInf = -1/0
<syntaxhighlight lang="wren">// using pre-defined constants
var nan = 0/0
var inf = Num.infinity
var negInf = -inf
var nan = Num.nan
var negZero = -0
System.print([inf, negInf, nan, negZero])
System.print([inf + inf, negInf + inf, nan * nan, negZero == 0])
System.print([inf/inf, negInf/2, nan + inf, negZero/0])
System.print()
</syntaxhighlight>
 
// using values computed from other 'normal' values
var inf2 = 1 / 0
var negInf2 = -1 / 0
var nan2 = 0 / 0
 
// using built-in comparison operators
System.print(inf2 == inf)
System.print(negInf == negInf2)
System.print(nan == nan2)
System.print(nan == nan)
System.print()
 
// using object equality
System.print(Object.same(nan, nan))
System.print(Object.same(nan, nan2))</syntaxhighlight>
 
{{out}}
Line 2,425 ⟶ 2,552:
[infinity, nan, nan, true]
[nan, -infinity, nan, nan]
 
true
true
false
false
 
true
false
</pre>
 
Anonymous user