Extreme floating point values: Difference between revisions

Standard ML solution
imported>Vukung
(Standard ML solution)
 
(19 intermediate revisions by 6 users not shown)
Line 27:
 
For performance reasons, the built-in floating-point types like Float and Long_Float are allowed to have IEEE 754 semantics if the machine numbers are IEEE 754. But the language provides means to exclude all non numbers from these types by defining a subtype with an explicit range:
<syntaxhighlight lang="ada">
<lang Ada>
subtype Consistent_Float is Float range Float'Range; -- No IEEE ideals
</syntaxhighlight>
</lang>
In general in properly written Ada programs variables may not become invalid when standard numeric operations are applied. The language also provides the attribute 'Valid to verify values obtained from unsafe sources e.g. from input, unchecked conversions etc.
 
As stated above on a machine where Float is implemented by an IEEE 754 machine number, IEEE 754 is permitted leak through. The following program illustrates how this leak can be exploited:
<syntaxhighlight lang="ada">
<lang Ada>
with Ada.Text_IO; use Ada.Text_IO;
Line 63:
 
end IEEE;
</syntaxhighlight>
</lang>
The expression -1.0 / 0.0 were non-numeric and thus could not be used.
To fool the compiler the variable Zero is used,
Line 98:
 
{{works with|nawk|20100523}}
<langsyntaxhighlight lang="awk">BEGIN {
# This requires 1e400 to overflow to infinity.
nzero = -0
Line 131:
print "nan == nan?", (nan == nan) ? "yes" : "no"
print "nan == 42?", (nan == 42) ? "yes" : "no"
}</langsyntaxhighlight>
 
----
Line 217:
 
{{works with|gcc|4.4.3}}
<langsyntaxhighlight Clang="c">#include <stdio.h>
 
int main()
Line 247:
 
return 0;
}</langsyntaxhighlight>
 
{{out}}
Line 298:
(most significant bit first for each byte).
It should be pretty portable.
<langsyntaxhighlight lang="c">#include <stdio.h>
#include <values.h>
#include <math.h>
Line 328:
 
return 0;
}</langsyntaxhighlight>
 
=={{header|Clojure}}==
{{Trans|Groovy}}
<langsyntaxhighlight lang="clojure">
(def neg-inf (/ -1.0 0.0)) ; Also Double/NEGATIVE_INFINITY
(def inf (/ 1.0 0.0)) ; Also Double/POSITIVE_INFINITY
Line 344:
(println " NaN == NaN: " (= Double/NaN Double/NaN))
(println "NaN equals NaN: " (.equals Double/NaN Double/NaN))
</syntaxhighlight>
</lang>
 
{{out}}
Line 362:
This program shows only part of the floating point features
supported by D and its Phobos standard library.
<langsyntaxhighlight lang="d">// Compile this module without -O
 
import std.stdio: writeln, writefln;
Line 425:
immutable real f3 = NaN(0x3FFF_FFFF_FFFF_FFFF);
writeln(getNaNPayload(f3));
}</langsyntaxhighlight>
{{out}}
<pre>Computed extreme float values:
Line 554:
 
Among other things, it is possible to trap FP hardware exceptions:
<langsyntaxhighlight lang="d">import std.math: FloatingPointControl;
 
void main() {
Line 566:
double y1 = f0 / f0; // generates hardware exception
// unless it's compiled with -O)
}</langsyntaxhighlight>
{{out}}
<pre>object.Error: Invalid Floating Point Operation</pre>
Line 574:
Tested on Delphi 2009:
 
<langsyntaxhighlight Delphilang="delphi">program Floats;
 
{$APPTYPE CONSOLE}
Line 612:
 
Readln;
end.</langsyntaxhighlight>
 
=={{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}}==
<syntaxhighlight lang="eiffel">
<lang Eiffel>
class
APPLICATION
Line 648 ⟶ 678:
print("(0.0 = -0.0) = ") print((0.0 = negZero)) print("%N")
end
end</langsyntaxhighlight>
{{out}}
<pre>Negative Infinity: -Infinity
Line 666 ⟶ 696:
=={{header|Euphoria}}==
{{trans|C}}
<langsyntaxhighlight Euphorialang="euphoria">constant inf = 1E400
constant minus_inf = -inf
constant nan = 0*inf
Line 681 ⟶ 711:
printf(1,"0.0 * +inf = %f\n", 0.0 * inf)
printf(1,"NaN + 1.0 = %f\n", nan + 1.0)
printf(1,"NaN + NaN = %f\n", nan + nan)</langsyntaxhighlight>
 
{{out}}
Line 695 ⟶ 725:
 
=={{header|F_Sharp|F#}}==
<langsyntaxhighlight lang="fsharp">
0.0/0.0 //->nan
0.0/(-0.0) //->nan
Line 704 ⟶ 734:
-infinity<infinity //->true
(-0.0)<0.0 //->false
</syntaxhighlight>
</lang>
 
=={{header|Factor}}==
<langsyntaxhighlight lang="factor">-0. . ! -0.0 literal negative zero
0. neg . ! -0.0 neg works with floating point zeros
0. -1. * . ! -0.0 calculating negative zero
Line 718 ⟶ 748:
! arbitrary 64-bit hex payload
1/0. 1/0. - . ! NAN: 8000000000000 calculating NaN by subtracting
! infinity from infinity</langsyntaxhighlight>
 
=={{header|Forth}}==
{{works with|GNU Forth}}
<langsyntaxhighlight lang="forth"> 1e 0e f/ f. \ inf
-1e 0e f/ f. \ inf (output bug: should say "-inf")
-1e 0e f/ f0< . \ -1 (true, it is -inf)
0e 0e f/ f. \ nan
-1e 0e f/ 1/f f0< . \ 0 (false, can't represent IEEE negative zero)</langsyntaxhighlight>
 
=={{header|Fortran}}==
Line 753 ⟶ 783:
===Pragmatics===
To prepare variables with these non-numerical states is troublesome, because attaining infinity by x = 1/0 or the like is not only bad behaviour, it invites complaint from the compiler or the generation of a run-time error and immediate cancellation of the run. One could mess about by using a READ statement on special texts, but that prevents the results being constants. Instead, one studies the definitions and devises code such as ...
<syntaxhighlight lang="fortran">
<lang Fortran>
REAL*8 BAD,NaN !Sometimes a number is not what is appropriate.
PARAMETER (NaN = Z'FFFFFFFFFFFFFFFF') !This value is recognised in floating-point arithmetic.
Line 762 ⟶ 792:
PARAMETER (PINF = Z'7FF0000000000000') !May well cause confusion
PARAMETER (NINF = Z'FFF0000000000000') !On a cpu not using this scheme.
</syntaxhighlight>
</lang>
After experimenting with code such as
<syntaxhighlight lang="fortran">
<lang Fortran>
Cause various arithmetic errors to see what sort of hissy fit is thrown.
REAL X2,X3,X4,Y4,XX,ZERO
Line 843 ⟶ 873:
WRITE (6,*) "Burp!"
END
</syntaxhighlight>
</lang>
Which provides output such as
<pre>
Line 880 ⟶ 910:
 
=={{header|FreeBASIC}}==
<langsyntaxhighlight lang="freebasic">' FB 1.05.0 Win64
 
#Include "crt/math.bi"
Line 893 ⟶ 923:
Print notNum, notNum + inf + negInf
Print negZero, negZero - 1
Sleep</langsyntaxhighlight>
 
{{out}}
Line 904 ⟶ 934:
 
=={{header|Go}}==
<langsyntaxhighlight lang="go">package main
 
import (
Line 981 ⟶ 1,011:
fmt.Println("!!! Expected", op, " Found not true.")
}
}</langsyntaxhighlight>
{{out}}
<pre>
Line 1,002 ⟶ 1,032:
{{Trans|Java}}
Solution:
<langsyntaxhighlight lang="groovy">def negInf = -1.0d / 0.0d; //also Double.NEGATIVE_INFINITY
def inf = 1.0d / 0.0d; //also Double.POSITIVE_INFINITY
def nan = 0.0d / 0.0d; //also Double.NaN
Line 1,014 ⟶ 1,044:
println(" 0 * NaN: " + (0 * nan));
println(" NaN == NaN: " + (nan == nan));
println("NaN equals NaN: " + (nan.equals(nan)));</langsyntaxhighlight>
 
{{out}}
Line 1,030 ⟶ 1,060:
 
=={{header|haskell}}==
<langsyntaxhighlight lang="haskell">
main = do
let inf = 1/0
Line 1,056 ⟶ 1,086:
putStrLn ("0.0 == - 0.0 = "++(show (0.0 == minus_zero)))
putStrLn ("inf == inf = "++(show (inf == inf)))
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 1,082 ⟶ 1,112:
 
'''Extreme values'''
<langsyntaxhighlight lang="j"> Inf=: _
NegInf=: __
NB. Negative zero cannot be represented in J to be distinct from 0.
NaN=. _.</langsyntaxhighlight>
The numeric atom <code>_.</code> ([[j:Essays/Indeterminate|Indeterminate]]) is provided as a means for dealing with NaN in data from sources outside J.
J itself generates NaN errors rather than NaN values and recommends that <code>_.</code> be removed from data as soon as possible because, by definition, NaN values will produce inconsistent results in contexts where value is important.
 
'''Extreme values from expressions'''
<langsyntaxhighlight lang="j"> (1 % 0) , (_1 % 0)
_ __
(1e234 * 1e234) , (_1e234 * 1e234)
Line 1,105 ⟶ 1,135:
%__ NB. Under the covers, the reciprocal of NegInf produces NegZero, but this fact isn't exposed to the user, who just sees zero
0
</syntaxhighlight>
</lang>
 
'''Some arithmetic'''
<langsyntaxhighlight lang="j"> _ + _
_
__ + __
Line 1,115 ⟶ 1,145:
_
NegInf * 0
0</langsyntaxhighlight>
 
=={{header|Java}}==
<langsyntaxhighlight lang="java">public class Extreme {
public static void main(String[] args) {
double negInf = -1.0 / 0.0; //also Double.NEGATIVE_INFINITY
Line 1,133 ⟶ 1,163:
System.out.println("NaN == NaN: " + (nan == nan));
}
}</langsyntaxhighlight>
{{out}}
<pre>Negative inf: -Infinity
Line 1,150 ⟶ 1,180:
 
For example, here are two expressions and the result of displaying their values:
<langsyntaxhighlight lang="jq">0/0 #=> null
1e1000 #=> 1.7976931348623157e+308</langsyntaxhighlight>
 
If your jq does not already have `infinite` and `nan` defined as built-in functions, they can be defined as follows:
 
<langsyntaxhighlight lang="jq">def infinite: 1e1000;
def nan: 0/0;</langsyntaxhighlight>
 
Here are some further expressions with their results:
 
<langsyntaxhighlight lang="jq">-0 #=> -0
0 == -0 # => true
infinite == infinite #=> true
Line 1,167 ⟶ 1,197:
1/infinite #=> 0
 
nan == nan #=> false # N.B.</langsyntaxhighlight>
 
Since `==` cannot be used to check if a value is IEEE NaN, jq 1.5 provides the builtin function `isnan` for doing so:
<langsyntaxhighlight lang="jq">nan | isnan #=> true
infinite | isnan #=> false</langsyntaxhighlight>
 
Exceptional values can be assigned to jq variables in the usual way:
<langsyntaxhighlight lang="jq">infinite as $inf | 1 / $inf #=> 0
-0 as $z | $z #=> -0</langsyntaxhighlight>
 
=={{header|Julia}}==
<langsyntaxhighlight lang="julia">
function showextremes()
values = [0.0, -0.0, Inf, -Inf, NaN]
Line 1,195 ⟶ 1,225:
@show NaN == NaN
@show NaN === NaN
</syntaxhighlight>
</lang>
 
{{out}}
Line 1,212 ⟶ 1,242:
 
=={{header|Kotlin}}==
<langsyntaxhighlight lang="scala">// version 1.0.5-2
 
@Suppress("DIVISION_BY_ZERO", "FLOAT_LITERAL_CONFORMS_ZERO")
Line 1,239 ⟶ 1,269:
println("nan / nan : ${nan / nan}")
println("negZero + 0.0 : ${negZero + 0.0}")
}</langsyntaxhighlight>
 
{{out}}
Line 1,268 ⟶ 1,298:
Infinity and NaN are straight forward. for negative 0 you need to resort to a literal with a large,
negative exponent, which is not the same thing.
<langsyntaxhighlight lang="lua">
 
local inf=math.huge
Line 1,274 ⟶ 1,304:
local NaN=0/0
local negativeZeroSorta=-1E-240
</syntaxhighlight>
</lang>
Lua seems to break x==1/(1/x) for infinity:
<langsyntaxhighlight lang="lua">
1/(1/-math.huge)==math.huge
true
</syntaxhighlight>
</lang>
 
=={{header|Mathematica}} / {{header|Wolfram Language}}==
<langsyntaxhighlight lang="mathematica">Column@{ReleaseHold[
Function[expression,
Row@{HoldForm@InputForm@expression, " = ", Quiet@expression},
Line 1,290 ⟶ 1,320:
Infinity - Infinity, 0 Infinity, ComplexInfinity + 1,
ComplexInfinity + ComplexInfinity, 2 ComplexInfinity,
0 ComplexInfinity, Indeterminate + 1, 0 Indeterminate]]}</langsyntaxhighlight>
{{out}}
<pre>1./0. = ComplexInfinity
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 1,329 ⟶ 1,392:
===Intersystems Caché===
Caché has the function $DOUBLE which complies with the IEEE 754 standard. The negative zero is indistinguishable from positive zero by operations. The special values evaluate to 0 when converted to a number in a later operation.
<syntaxhighlight lang="mumps">
<lang MUMPS>
EXTREMES
NEW INF,NINF,ZERO,NOTNUM,NEGZERO
Line 1,345 ⟶ 1,408:
KILL INF,NINF,ZERO,NONNUM,NEGZERO
QUIT
</syntaxhighlight>
</lang>
{{out}}
<pre>USER>d EXTREMES^ROSETTA
Line 1,362 ⟶ 1,425:
While [[NetRexx]] native support for numbers allows for very large decimal precision, the [[Java]] primitives (<tt>int</tt>, <tt>long</tt>, <tt>float</tt>, <tt>double</tt> etc.), can use the constants and methods provided for &quot;extreme&quot; values:
{{trans|Java}}
<langsyntaxhighlight NetRexxlang="netrexx">/* NetRexx */
options replace format comments java crossref symbols binary
 
Line 1,379 ⟶ 1,442:
 
return
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 1,392 ⟶ 1,455:
 
=={{header|Nim}}==
<langsyntaxhighlight lang="nim">echo 1e234 * 1e234 # inf
echo 1e234 * -1e234 # -inf
echo 1 / Inf # 0
Line 1,403 ⟶ 1,466:
echo NaN * 0.0 # nan
echo 0.0 * Inf # nan
echo Inf * 0.0 # nan</langsyntaxhighlight>
 
=={{header|OCaml}}==
<langsyntaxhighlight lang="ocaml"># infinity;;
- : float = infinity
# neg_infinity;;
Line 1,437 ⟶ 1,500:
- : bool = true
# 0. == -0.;;
- : bool = false</langsyntaxhighlight>
 
=={{header|Oforth}}==
Line 1,456 ⟶ 1,519:
By default numbers in Ol is rational (exact) with unlimited exactness (if you have enought RAM). Therefore, we must explicitly use the 'inexact' function to force vm to use machine dependent floating point values.
 
<langsyntaxhighlight lang="scheme">(import (scheme inexact))
 
(print "infinity: " (/ 1 0))
Line 1,469 ⟶ 1,532:
; note: your must use equal? or eqv? but not eq? for comparison
(print "is this is not a number? " (equal? (fsqrt -1) +nan.0))
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 1,478 ⟶ 1,541:
 
=={{header|Oz}}==
<langsyntaxhighlight lang="oz">declare
Inf = 1.0e234 * 1.0e234
MinusInf = 1.0e234 * ~1.0e234
Line 1,501 ⟶ 1,564:
 
{Show 1.0/0.0 == Inf} %% true
{Show 1.0/~0.0 == MinusInf} %% true</langsyntaxhighlight>
 
{{out}}
<langsyntaxhighlight lang="oz">infinite: 1.#INF
-infinite: -1.#INF
0: 0.0
Line 1,517 ⟶ 1,580:
true
true
true</langsyntaxhighlight>
 
=={{header|PARI/GP}}==
Line 1,548 ⟶ 1,611:
Division by zero, <tt>sqrt(-1)</tt> and <tt>log(0)</tt> are fatal errors.
To get infinity and NaN, use corresponding string and force a numeric conversion by adding zero to it, or prepending a "+" or "-":
<langsyntaxhighlight lang="perl">#!/usr/bin/perl
use strict;
use warnings;
Line 1,570 ⟶ 1,633:
printf "nan test: %g\n", (1 + 2 * 3 - 4) / (-5.6e7 * $nan);
printf "nan == nan? %s\n", ($nan == $nan) ? "yes" : "no";
printf "nan == 42? %s\n", ($nan == 42) ? "yes" : "no";</langsyntaxhighlight>
 
{{out}}
Line 1,595 ⟶ 1,658:
There is no negative zero.
 
<langsyntaxhighlight lang="perl">#!/usr/bin/perl
use strict;
use warnings;
Line 1,617 ⟶ 1,680:
printf "\$nan * \$huge = %s\n", $nan->copy()->bmul($huge);
printf "\$nan == \$nan? %s\n", defined($nan->bcmp($nan)) ? "maybe" : "no";
printf "\$nan == \$huge? %s\n", defined($nan->bcmp($huge)) ? "maybe" : "no";</langsyntaxhighlight>
 
{{out}}
Line 1,632 ⟶ 1,695:
 
=={{header|Phix}}==
<!--<syntaxhighlight lang="phix">(phixonline)-->
<lang Phix>constant inf = 1e300*1e300, -- (works on both 32 and 64 bit)
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
ninf = -inf,
<span style="color: #008080;">constant</span> <span style="color: #000000;">inf</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">1e300</span><span style="color: #0000FF;">*</span><span style="color: #000000;">1e300</span><span style="color: #0000FF;">,</span> <span style="color: #000080;font-style:italic;">-- (works on both 32 and 64 bit)</span>
nan = -(inf/inf),
<span style="color: #000000;">ninf</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">-</span><span style="color: #000000;">inf</span><span style="color: #0000FF;">,</span>
nzero = -1/inf -- (not supported)
<span style="color: #000000;">nan</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">-(</span><span style="color: #000000;">inf</span><span style="color: #0000FF;">/</span><span style="color: #000000;">inf</span><span style="color: #0000FF;">),</span>
 
<span style="color: #000000;">nzero</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">-</span><span style="color: #000000;">1</span><span style="color: #0000FF;">/</span><span style="color: #000000;">inf</span> <span style="color: #000080;font-style:italic;">-- (not supported)</span>
printf(1," inf: %f\n",{inf})
printf(1," ninf: %f\n",{ninf})
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">" inf: %f\n"</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">inf</span><span style="color: #0000FF;">})</span>
printf(1," nan: %f\n",{nan})
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">" ninf: %f\n"</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">ninf</span><span style="color: #0000FF;">})</span>
printf(1,"*nzero: %f\n",{nzero})
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">" nan: %f\n"</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">nan</span><span style="color: #0000FF;">})</span>
printf(1," inf+2: %f\n",{inf+2})
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"*nzero: %f\n"</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">nzero</span><span style="color: #0000FF;">})</span>
printf(1," inf+ninf: %f\n",{inf+ninf})
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">" inf+2: %f\n"</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">inf</span><span style="color: #0000FF;">+</span><span style="color: #000000;">2</span><span style="color: #0000FF;">})</span>
printf(1," 0*inf: %f\n",{0*inf})
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">" inf+ninf: %f\n"</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">inf</span><span style="color: #0000FF;">+</span><span style="color: #000000;">ninf</span><span style="color: #0000FF;">})</span>
printf(1," nan+1: %f\n",{nan+1})
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">" 0*inf: %f\n"</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">0</span><span style="color: #0000FF;">*</span><span style="color: #000000;">inf</span><span style="color: #0000FF;">})</span>
printf(1," nan+nan: %f\n",{nan+nan})
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">" nan+1: %f\n"</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">nan</span><span style="color: #0000FF;">+</span><span style="color: #000000;">1</span><span style="color: #0000FF;">})</span>
printf(1," inf>1e300: %d\n",{inf>1e300})
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">" nan+nan: %f\n"</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">nan</span><span style="color: #0000FF;">+</span><span style="color: #000000;">nan</span><span style="color: #0000FF;">})</span>
printf(1," ninf<1e300: %d\n",{ninf<-1e300})
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">" inf&gt;1e300: %d\n"</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">inf</span><span style="color: #0000FF;">></span><span style="color: #000000;">1e300</span><span style="color: #0000FF;">})</span>
printf(1,"*nan=nan: %d\n",{nan=nan})
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">" ninf&lt;1e300: %d\n"</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">ninf</span><span style="color: #0000FF;"><-</span><span style="color: #000000;">1e300</span><span style="color: #0000FF;">})</span>
printf(1," nan=42: %d\n",{nan=42})
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"*nan=nan: %d\n"</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">nan</span><span style="color: #0000FF;">=</span><span style="color: #000000;">nan</span><span style="color: #0000FF;">})</span>
printf(1,"*nan<0: %d\n",{nan<0})
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">" nan=42: %d\n"</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">nan</span><span style="color: #0000FF;">=</span><span style="color: #000000;">42</span><span style="color: #0000FF;">})</span>
printf(1," nan>0: %d\n",{nan>0})</lang>
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"*nan&lt;0: %d\n"</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">nan</span><span style="color: #0000FF;"><</span><span style="color: #000000;">0</span><span style="color: #0000FF;">})</span>
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">" nan&gt;0: %d\n"</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">nan</span><span style="color: #0000FF;">></span><span style="color: #000000;">0</span><span style="color: #0000FF;">})</span>
<!--</syntaxhighlight>-->
{{out}}
<pre>
Line 1,676 ⟶ 1,742:
 
If you fancy having a go at negative zero support (ditto), your first stop should be :%pStoreFlt in builtins\VM\pHeap.e and use whatever the test is for -0.0 there. I would be happiest if apps that needed support of -0.0 had to explicitly call something in pHeap.e to set a flag to enable any new code.
 
Under pwa/p2js slightly different and perhaps more correct output is generated:
<pre>
 inf: Infinity
 ninf: -Infinity
 nan: NaN
*nzero: 0.000000
 inf+2: Infinity
 inf+ninf: NaN
 0*inf: NaN
 nan+1: NaN
 nan+nan: NaN
 inf>1e300: 1
 ninf<1e300: 1
*nan=nan: 0
 nan=42: 0
*nan<0: 0
 nan>0: 1
</pre>
 
=={{header|PicoLisp}}==
Line 1,682 ⟶ 1,767:
Minus zero and negative infinity cannot be represented,
while NaN is represented by NIL
<langsyntaxhighlight PicoLisplang="picolisp">(load "@lib/math.l")
 
: (exp 1000.0) # Too large for IEEE floats
Line 1,688 ⟶ 1,773:
 
: (+ 1 2 NIL 3) # NaN propagates
-> NIL</langsyntaxhighlight>
 
=={{header|PureBasic}}==
<langsyntaxhighlight PureBasiclang="purebasic">Define.f
If OpenConsole()
inf = Infinity() ; or 1/None ;None represents a variable of value = 0
Line 1,716 ⟶ 1,801:
Print(#CRLF$+"Press ENTER to EXIT"): Input()
EndIf</langsyntaxhighlight>
<pre>positive infinity: +Infinity
negative infinity: -Infinity
Line 1,737 ⟶ 1,822:
 
=={{header|Python}}==
<langsyntaxhighlight lang="python">>>> # Extreme values from expressions
>>> inf = 1e234 * 1e234
>>> _inf = 1e234 * -1e234
Line 1,778 ⟶ 1,863:
nan
>>> inf * 0.0
nan</langsyntaxhighlight>
 
<langsyntaxhighlight lang="python">>>> # But note!
>>> 1 / -0.0
 
Line 1,787 ⟶ 1,872:
1 / -0.0
ZeroDivisionError: float division by zero
>>> # (Not minus infinity)</langsyntaxhighlight>
 
=={{header|R}}==
<langsyntaxhighlight lang="r"># 0 and -0 are recognized but are both printed as simply 0.
1/c(0, -0, Inf, -Inf, NaN)
# Inf -Inf 0 0 NaN</langsyntaxhighlight>
 
=={{header|Racket}}==
<langsyntaxhighlight Racketlang="racket">#lang racket
(define division-by-zero (/ 1.0 0.0)) ;+inf.0
(define negative-inf (- (/ 1.0 0.0))) ;-inf.0
Line 1,812 ⟶ 1,897:
(+ zero nan) ; +nan.0
(= nan +nan.0) ;#f
</syntaxhighlight>
</lang>
 
This values can be assigned to a variable just as normal values
Line 1,820 ⟶ 1,905:
{{Works with|rakudo|2018.03}}
Floating point limits are to a large extent implementation dependent, but currently both Raku backends (MoarVM, JVM) running on a 64 bit OS have an infinity threshold of just under 1.8e308.
<syntaxhighlight lang="raku" perl6line>print qq:to 'END'
positive infinity: {1.8e308}
negative infinity: {-1.8e308}
Line 1,835 ⟶ 1,920:
NaN == NaN = {NaN == NaN}
0.0 == -0.0 = {0e0 == -0e0}
END</langsyntaxhighlight>
 
<code>0e0</code> is used to have floating point number.
Line 1,873 ⟶ 1,958:
dictates that a minimum number of decimal digits be supported as well as a minimum number
of decimal digits in the exponent.
<langsyntaxhighlight lang="rexx">/*REXX pgm shows smallest & largest positive numbers that can be expressed, compares 0's*/
parse version v; say 'version=' v; say
zero= '0.0' /*a (positive) value for zero. */
Line 1,903 ⟶ 1,988:
p=!; !=! + j /*save number; bump mantissa. */
end /*forever*/
return $ || !</langsyntaxhighlight>
{{out|output|text= &nbsp; when using Regina REXX:}}
<pre>
Line 1,975 ⟶ 2,060:
 
=={{header|Ruby}}==
<langsyntaxhighlight lang="ruby">inf = 1.0 / 0.0 # or Float::INFINITY
nan = 0.0 / 0.0 # or Float::NAN
 
Line 1,989 ⟶ 2,074:
expression.each do |exp|
puts "%15s => %p" % [exp, eval(exp)]
end</langsyntaxhighlight>
 
{{out}}
Line 2,028 ⟶ 2,113:
Negative zero needs to printed using the [https://doc.rust-lang.org/std/fmt/trait.Debug.html Debug trait] (rather than the "user-facing" [https://doc.rust-lang.org/std/fmt/trait.Display.html Display trait]) because <code>0 == -0</code> to most users. See https://github.com/rust-lang/rfcs/issues/1074 and https://github.com/rust-lang/rust/issues/24623 for further discussion about this.
 
<langsyntaxhighlight lang="rust">fn main() {
let inf: f64 = 1. / 0.; // or std::f64::INFINITY
let minus_inf: f64 = -1. / 0.; // or std::f64::NEG_INFINITY
Line 2,049 ⟶ 2,134:
println!("NaN == NaN = {}", nan == nan);
println!("0.0 == -0.0 = {}", 0. == -0.);
}</langsyntaxhighlight>
{{out}}
<pre>
Line 2,071 ⟶ 2,156:
=={{header|S-lang}}==
Each of these can be directly input; I'll calc the Infs for good measure:
<langsyntaxhighlight Slang="s-lang">foreach $1 ([{-0.0}, {_Inf, "1.0/0"}, {-_Inf, "-1.0/0"}, {_NaN}]) {
() = printf("%S", $1[0]);
if (length($1) > 1) () = printf("\t%S\n", eval($1[1]));
else () = printf("\n");
}</langsyntaxhighlight>{{out}}
<pre>-0.0
inf inf
Line 2,081 ⟶ 2,166:
nan
</pre>
<langsyntaxhighlight Slang="s-lang">% And make some comparisons:
() = printf("-0.0 and 0.0 are %sequal\n", -0.0 == 0.0 ? "" : "not ");
() = printf("-_Inf == _Inf are %sequal\n", -_Inf == _Inf ? "" : "not ");
() = printf("-0.0 and 0.0 are %sthe 'same'\n", __is_same(-0.0, 0.0) ? "" : "not ");
</langsyntaxhighlight>{{out}}
<pre>-0.0 and 0.0 are equal
-_Inf == _Inf are not equal
Line 2,093 ⟶ 2,178:
=={{header|Scala}}==
{{libheader|Scala}}
<langsyntaxhighlight Scalalang="scala">object ExtremeFloatingPoint extends App {
val negInf = -1.0 / 0.0 //also Double.NegativeInfinity
val inf = 1.0 / 0.0 // //also Double.PositiveInfinity
Line 2,107 ⟶ 2,192:
println(f"0 * NaN: ${0 * nan}%9s ${(inf + negInf).isInfinity}%9s ${(inf + negInf).isWhole}%9s")
println(f"NaN == NaN: ${nan == nan}%9s")
}</langsyntaxhighlight>
{{out}}
<pre>Value: Result: Infinity? Whole?
Line 2,120 ⟶ 2,205:
 
=={{header|Scheme}}==
<langsyntaxhighlight Schemelang="scheme">(define infinity (/ 1.0 0.0))
(define minus-infinity (- infinity))
(define zero 0.0)
Line 2,129 ⟶ 2,214:
(list +inf.0 -inf.0 0.0 -0.0 +nan.0))
; #t
</syntaxhighlight>
</lang>
 
=={{header|Seed7}}==
Line 2,141 ⟶ 2,226:
can be used. NaN can be checked with [http://seed7.sourceforge.net/libraries/float.htm#isNaN%28ref_float%29 isNaN].
 
<langsyntaxhighlight lang="seed7">$ include "seed7_05.s7i";
include "float.s7i";
Line 2,166 ⟶ 2,251:
writeln("isNegativeZero(-0.0) = " <& isNegativeZero(-0.0));
writeln("isNegativeZero(0.0) = " <& isNegativeZero(0.0));
end func;</langsyntaxhighlight>
 
{{out}}
Line 2,191 ⟶ 2,276:
{{trans|Ruby}}
''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.
<langsyntaxhighlight lang="ruby">var inf = 1/0 # same as: Inf
var nan = 0/0 # same as: NaN
 
Line 2,220 ⟶ 2,305:
say("atan(Inf) = ", atan(inf))
say("log(-1) = ", log(-1))
say("atanh(Inf) = ", atanh(inf))</langsyntaxhighlight>
{{out}}
<pre>
Line 2,267 ⟶ 2,352:
 
=={{header|Smalltalk}}==
The ANSI standard defines FloatE and FloatD objects, but usually you can also just write Float or Double instead (or define an alias):
<lang Smalltalk >FloatE nan
<syntaxhighlight lang="smalltalk ">FloatE nan
FloatD nan
FloatE infinity
Line 2,274 ⟶ 2,360:
FloatD negativeInfinity
 
Float zero -> 0.0
(FloatE infinity negated = FloatE infinity) -> true
Float negativeZero -> -0.0
</lang>
0.0 negated -> -0.0
0.0 negated = 0.0 -> true. "they have the same value"
0.0 negated < 0.0 -> false
0.0 negated > 0.0 -> false
 
1.0 isFinite -> true
FloatE infinity isFinite -> false
 
(FloatE infinity = FloatD infinity) -> true
FloatE infinity > 1e200 -> true
FloatE infinity > FloatE negativeInfinity -> true
FloatE infinity > Number negativeInfinity -> true
(FloatE infinity negated = FloatE negativeInfinity) -> true
 
(1.0 / 0.0) -> ZeroDivision exception
[ 1.0 / 0.0 ] on:ZeroDivide do:[:ex | ex proceed ] -> infinity
 
(0.0 / 0.0) -> ZeroDivision exception
[ 0.0 / 0.0 ] on:ZeroDivide do:[:ex | ex proceed ] -> nan
 
(1.0 / Float infinity) -> 0.0
(1.0 / Float negativeInfinity) -> -0.0
 
-4 sqrt -> ImaginaryResultError exception
Number trapImaginary:[ -4 sqrt ] -> (0+2.0i) (works in Smalltalk/X)
[ -4 sqrt ] on:DomainError do:[:ex | ex proceed] -> nan
 
-1 log10 -> DomainError exception
[ -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>
 
=={{header|Stata}}==
 
Stata does not use NaN values, but instead it has several kinds of missing values, which are denoted by . and .a to .z. These are stored as large floating point numbers, as can be seen in the hexadecimal representation:
 
<langsyntaxhighlight lang="stata">. display %21x .
+1.0000000000000X+3ff
 
Line 2,290 ⟶ 2,453:
 
. display %21x c(maxdouble)
+1.fffffffffffffX+3fe</langsyntaxhighlight>
 
Notice that .z > ... > .a > . and . is greater than any real number, and c(maxdouble) is the value 8.9884656743115785e+307.
Line 2,297 ⟶ 2,460:
 
=={{header|Swift}}==
<langsyntaxhighlight lang="swift">let negInf = -1.0 / 0.0
let inf = 1.0 / 0.0 //also Double.infinity
let nan = 0.0 / 0.0 //also Double.NaN
Line 2,308 ⟶ 2,471:
println("inf + -inf: \(inf + negInf)")
println("0 * NaN: \(0 * nan)")
println("NaN == NaN: \(nan == nan)")</langsyntaxhighlight>
{{out}}
<pre>
Line 2,326 ⟶ 2,489:
(they are parsed case-insensitively).
For example, see this log of an interactive session:
<langsyntaxhighlight lang="tcl">% package require Tcl 8.5
8.5.2
% expr inf+1
Line 2,341 ⟶ 2,504:
domain error: argument not in valid range
% expr {1/-inf}
-0.0</langsyntaxhighlight>
It ''is'' possible to introduce a real NaN though numeric computation,
but only by using the mechanisms for dealing with external binary data
(it being judged better to just deal with it in that case
rather than throwing an exception):
<langsyntaxhighlight lang="tcl">% binary scan [binary format q nan] q nan
1
% puts $nan
Line 2,352 ⟶ 2,515:
% # Show that it is a real NaN in there
% expr {$nan+0}
can't use non-numeric floating-point value as operand of "+"</langsyntaxhighlight>
 
=={{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.
 
<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()
</lang>
 
// 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,370 ⟶ 2,552:
[infinity, nan, nan, true]
[nan, -infinity, nan, nan]
 
true
true
false
false
 
true
false
</pre>
 
=={{header|XPL0}}==
{{trans|C}}
As run on Raspberry Pi:
<syntaxhighlight lang "XPL0">include xpllib; \for Print
 
real Inf, Minus_inf, Minus_zero, Nan;
[Inf:= 1.0/0.0;
Minus_Inf:= -1.0/0.0;
Minus_zero:= -1.0/ Inf ;
Nan:= 0.0/0.0;
 
Print("positive Infinity: %f\n", Inf);
Print("negative Infinity: %f\n", Minus_Inf);
Print("negative zero: %f\n", Minus_zero);
Print("not a number: %f\n", Nan);
 
\some arithmetic
 
Print("+Inf + 2.0:= %f\n", Inf + 2.0);
Print("+Inf - 10.1:= %f\n", Inf - 10.1);
Print("+Inf + -Inf:= %f\n", Inf + Minus_Inf);
Print("0.0 * +Inf:= %f\n", 0.0 * Inf);
Print("1.0/-0.0:= %f\n", 1.0/Minus_zero);
Print("NaN + 1.0:= %f\n", Nan + 1.0);
Print("NaN + NaN:= %f\n", Nan + Nan);
 
\some comparisons
 
Print("NaN = NaN:= %s\n", if Nan = Nan then "true" else "false");
Print("0.0 = -0.0:= %s\n", if 0.0 = Minus_zero then "true" else "false");
]</syntaxhighlight>
{{out}}
<pre>
positive Infinity: INF
negative Infinity: -INF
negative zero: 0.00000
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
</pre>
Anonymous user