Anonymous user
Extreme floating point values: Difference between revisions
Standard ML solution
(added Factor) |
imported>Vukung (Standard ML solution) |
||
(33 intermediate revisions by 12 users not shown) | |||
Line 14:
;See also:
* [
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">
subtype Consistent_Float is Float range Float'Range; -- No IEEE ideals
</syntaxhighlight>
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">
with Ada.Text_IO; use Ada.Text_IO;
Line 63:
end IEEE;
</syntaxhighlight>
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}}
<
# 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"
}</
----
Line 217:
{{works with|gcc|4.4.3}}
<
int main()
Line 247:
return 0;
}</
{{out}}
Line 298:
(most significant bit first for each byte).
It should be pretty portable.
<
#include <values.h>
#include <math.h>
Line 328:
return 0;
}</
=={{header|Clojure}}==
{{Trans|Groovy}}
<
(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>
{{out}}
Line 362:
This program shows only part of the floating point features
supported by D and its Phobos standard library.
<
import std.stdio: writeln, writefln;
Line 425:
immutable real f3 = NaN(0x3FFF_FFFF_FFFF_FFFF);
writeln(getNaNPayload(f3));
}</
{{out}}
<pre>Computed extreme float values:
Line 554:
Among other things, it is possible to trap FP hardware exceptions:
<
void main() {
Line 566:
double y1 = f0 / f0; // generates hardware exception
// unless it's compiled with -O)
}</
{{out}}
<pre>object.Error: Invalid Floating Point Operation</pre>
Line 574:
Tested on Delphi 2009:
<
{$APPTYPE CONSOLE}
Line 612:
Readln;
end.</
=={{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">
class
APPLICATION
Line 648 ⟶ 678:
print("(0.0 = -0.0) = ") print((0.0 = negZero)) print("%N")
end
end</
{{out}}
<pre>Negative Infinity: -Infinity
Line 666 ⟶ 696:
=={{header|Euphoria}}==
{{trans|C}}
<
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)</
{{out}}
Line 693 ⟶ 723:
NaN + 1.0 = -nan
NaN + NaN = -nan
=={{header|F_Sharp|F#}}==
<syntaxhighlight lang="fsharp">
0.0/0.0 //->nan
0.0/(-0.0) //->nan
1.0/infinity //->0.0
1.0/(-infinity) //->0.0
1.0/0.0 //->infinity
1.0/(-0.0) //->-infinity
-infinity<infinity //->true
(-0.0)<0.0 //->false
</syntaxhighlight>
=={{header|Factor}}==
<
0. neg . ! -0.0 neg works with floating point zeros
0. -1. * . ! -0.0 calculating negative zero
Line 706 ⟶ 748:
! arbitrary 64-bit hex payload
1/0. 1/0. - . ! NAN: 8000000000000 calculating NaN by subtracting
! infinity from infinity</
=={{header|Forth}}==
{{works with|GNU Forth}}
<
-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)</
=={{header|Fortran}}==
Line 741 ⟶ 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">
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 750 ⟶ 792:
PARAMETER (PINF = Z'7FF0000000000000') !May well cause confusion
PARAMETER (NINF = Z'FFF0000000000000') !On a cpu not using this scheme.
</syntaxhighlight>
After experimenting with code such as
<syntaxhighlight lang="fortran">
Cause various arithmetic errors to see what sort of hissy fit is thrown.
REAL X2,X3,X4,Y4,XX,ZERO
Line 831 ⟶ 873:
WRITE (6,*) "Burp!"
END
</syntaxhighlight>
Which provides output such as
<pre>
Line 868 ⟶ 910:
=={{header|FreeBASIC}}==
<
#Include "crt/math.bi"
Line 881 ⟶ 923:
Print notNum, notNum + inf + negInf
Print negZero, negZero - 1
Sleep</
{{out}}
Line 892 ⟶ 934:
=={{header|Go}}==
<
import (
Line 969 ⟶ 1,011:
fmt.Println("!!! Expected", op, " Found not true.")
}
}</
{{out}}
<pre>
Line 990 ⟶ 1,032:
{{Trans|Java}}
Solution:
<
def inf = 1.0d / 0.0d; //also Double.POSITIVE_INFINITY
def nan = 0.0d / 0.0d; //also Double.NaN
Line 1,002 ⟶ 1,044:
println(" 0 * NaN: " + (0 * nan));
println(" NaN == NaN: " + (nan == nan));
println("NaN equals NaN: " + (nan.equals(nan)));</
{{out}}
Line 1,018 ⟶ 1,060:
=={{header|haskell}}==
<
main = do
let inf = 1/0
Line 1,044 ⟶ 1,086:
putStrLn ("0.0 == - 0.0 = "++(show (0.0 == minus_zero)))
putStrLn ("inf == inf = "++(show (inf == inf)))
</syntaxhighlight>
{{out}}
<pre>
Line 1,065 ⟶ 1,107:
Icon and Unicon don't define minimum or maximum values of reals, or a negative 0.0. Real numbers are implemented as C doubles and the behavior could vary somewhat from platform to platform.
Both explicitly check for divide by zero and treat it as a runtime error (201), so it's not clear how you could produce one of these with the possible exception of the value being introduced through externally called code.
=={{header|J}}==
'''Extreme values'''
<
NegInf=: __
NB. Negative zero cannot be represented in J to be distinct from 0.
NaN=. _.</
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'''
<
_ __
(1e234 * 1e234) , (_1e234 * 1e234)
Line 1,093 ⟶ 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>
'''Some arithmetic'''
<
_
__ + __
Line 1,103 ⟶ 1,145:
_
NegInf * 0
0</
=={{header|Java}}==
<
public static void main(String[] args) {
double negInf = -1.0 / 0.0; //also Double.NEGATIVE_INFINITY
Line 1,121 ⟶ 1,163:
System.out.println("NaN == NaN: " + (nan == nan));
}
}</
{{out}}
<pre>Negative inf: -Infinity
Line 1,138 ⟶ 1,180:
For example, here are two expressions and the result of displaying their values:
<
1e1000 #=> 1.7976931348623157e+308</
If your jq does not already have `infinite` and `nan` defined as built-in functions, they can be defined as follows:
<
def nan: 0/0;</
Here are some further expressions with their results:
<
0 == -0 # => true
infinite == infinite #=> true
Line 1,155 ⟶ 1,197:
1/infinite #=> 0
nan == nan #=> false # N.B.</
Since `==` cannot be used to check if a value is IEEE NaN, jq 1.5 provides the builtin function `isnan` for doing so:
<
infinite | isnan #=> false</
Exceptional values can be assigned to jq variables in the usual way:
<
-0 as $z | $z #=> -0</
=={{header|Julia}}==
<
function showextremes()
values = [0.0, -0.0, Inf, -Inf, NaN]
Line 1,183 ⟶ 1,225:
@show NaN == NaN
@show NaN === NaN
</syntaxhighlight>
{{out}}
Line 1,200 ⟶ 1,242:
=={{header|Kotlin}}==
<
@Suppress("DIVISION_BY_ZERO", "FLOAT_LITERAL_CONFORMS_ZERO")
Line 1,227 ⟶ 1,269:
println("nan / nan : ${nan / nan}")
println("negZero + 0.0 : ${negZero + 0.0}")
}</
{{out}}
Line 1,256 ⟶ 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.
<
local inf=math.huge
Line 1,262 ⟶ 1,304:
local NaN=0/0
local negativeZeroSorta=-1E-240
</syntaxhighlight>
Lua seems to break x==1/(1/x) for infinity:
<
1/(1/-math.huge)==math.huge
true
</syntaxhighlight>
=={{header|Mathematica}} / {{header|Wolfram Language}}==
<
Function[expression,
Row@{HoldForm@InputForm@expression, " = ", Quiet@expression},
Line 1,278 ⟶ 1,320:
Infinity - Infinity, 0 Infinity, ComplexInfinity + 1,
ComplexInfinity + ComplexInfinity, 2 ComplexInfinity,
0 ComplexInfinity, Indeterminate + 1, 0 Indeterminate]]}</
{{out}}
<pre>1./0. = ComplexInfinity
Line 1,303 ⟶ 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,317 ⟶ 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">
EXTREMES
NEW INF,NINF,ZERO,NOTNUM,NEGZERO
Line 1,333 ⟶ 1,408:
KILL INF,NINF,ZERO,NONNUM,NEGZERO
QUIT
</syntaxhighlight>
{{out}}
<pre>USER>d EXTREMES^ROSETTA
Line 1,350 ⟶ 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 "extreme" values:
{{trans|Java}}
<
options replace format comments java crossref symbols binary
Line 1,367 ⟶ 1,442:
return
</syntaxhighlight>
{{out}}
<pre>
Line 1,380 ⟶ 1,455:
=={{header|Nim}}==
<
echo 1e234 * -1e234 # -inf
echo 1 /
echo
echo
echo
echo 0.0 == -0.0 # true
echo 0.0 *
echo
echo 0.0 *
echo
=={{header|OCaml}}==
<
- : float = infinity
# neg_infinity;;
Line 1,425 ⟶ 1,500:
- : bool = true
# 0. == -0.;;
- : bool = false</
=={{header|Oforth}}==
Line 1,439 ⟶ 1,514:
1
ok
</pre>
=={{header|Ol}}==
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.
<syntaxhighlight lang="scheme">(import (scheme inexact))
(print "infinity: " (/ 1 0))
(print "minus infinity: " (log 0))
; note: (sqrt -1) function will produce 0+i complex number
; so we need to use simpler function "fsqrt"
(import (owl math fp))
(print "not-a-number: " (fsqrt -1))
; 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>
{{out}}
<pre>
infinity: +inf.0
minus infinity: -inf.0
not-a-number: +nan.0
</pre>
=={{header|Oz}}==
<
Inf = 1.0e234 * 1.0e234
MinusInf = 1.0e234 * ~1.0e234
Line 1,465 ⟶ 1,564:
{Show 1.0/0.0 == Inf} %% true
{Show 1.0/~0.0 == MinusInf} %% true</
{{out}}
<
-infinite: -1.#INF
0: 0.0
Line 1,481 ⟶ 1,580:
true
true
true</
=={{header|PARI/GP}}==
Line 1,512 ⟶ 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 "-":
<
use strict;
use warnings;
Line 1,534 ⟶ 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";</
{{out}}
Line 1,559 ⟶ 1,658:
There is no negative zero.
<
use strict;
use warnings;
Line 1,581 ⟶ 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";</
{{out}}
Line 1,594 ⟶ 1,693:
$nan == $nan? no
$nan == $huge? no</pre>
=={{header|Phix}}==
<!--<syntaxhighlight lang="phix">(phixonline)-->
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
<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>
<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>
<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>
<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>
<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>
<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>
<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>
<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>
<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>
<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>
<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>
<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>
<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>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>
<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<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>
<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>
<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>
<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<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>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
<
: (exp 1000.0) # Too large for IEEE floats
Line 1,688 ⟶ 1,773:
: (+ 1 2 NIL 3) # NaN propagates
-> NIL</
=={{header|PureBasic}}==
<
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</
<pre>positive infinity: +Infinity
negative infinity: -Infinity
Line 1,737 ⟶ 1,822:
=={{header|Python}}==
<
>>> inf = 1e234 * 1e234
>>> _inf = 1e234 * -1e234
Line 1,778 ⟶ 1,863:
nan
>>> inf * 0.0
nan</
<
>>> 1 / -0.0
Line 1,787 ⟶ 1,872:
1 / -0.0
ZeroDivisionError: float division by zero
>>> # (Not minus infinity)</
=={{header|R}}==
<
1/c(0, -0, Inf, -Inf, NaN)
# Inf -Inf 0 0 NaN</
=={{header|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>
This values can be assigned to a variable just as normal values
=={{header|Raku}}==
(formerly Perl 6)
{{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" line>print qq:to 'END'
positive infinity: {1.8e308}
negative infinity: {-1.8e308}
negative zero: {0e0 * -1}
not a number: {0 * 1e309}
+Inf + 2.0 = {Inf + 2}
+Inf - 10.1 = {Inf - 10.1}
0 * +Inf = {0 * Inf}
+Inf + -Inf = {Inf + -Inf}
+Inf == -Inf = {+Inf == -Inf}
(-Inf+0i)**.5 = {(-Inf+0i)**.5}
NaN + 1.0 = {NaN + 1.0}
NaN + NaN = {NaN + NaN}
NaN == NaN = {NaN == NaN}
0.0 == -0.0 = {0e0 == -0e0}
END</syntaxhighlight>
<code>0e0</code> is used to have floating point number.
Simply using <code>0.0</code> makes rational number that doesn't recognize <code>-0</code>.
<code>qq:to</code> is heredoc syntax, where <code>qq</code> means
that variables and closures (between braces) are interpolated.
{{out}}
<pre>positive infinity: Inf
negative infinity: -Inf
negative zero: -0
not a number: NaN
+Inf + 2.0 = Inf
+Inf - 10.1 = Inf
0 * +Inf = NaN
+Inf + -Inf = NaN
+Inf == -Inf = False
(-Inf+0i)**.5 = Inf+Inf\i
NaN + 1.0 = NaN
NaN + NaN = NaN
NaN == NaN = False
0.0 == -0.0 = True</pre>
=={{header|REXX}}==
Line 1,831 ⟶ 1,958:
dictates that a minimum number of decimal digits be supported as well as a minimum number
of decimal digits in the exponent.
<
parse version v; say 'version=' v; say
zero= '0.0' /*a (positive) value for zero. */
Line 1,861 ⟶ 1,988:
p=!; !=! + j /*save number; bump mantissa. */
end /*forever*/
return $ || !</
{{out|output|text= when using Regina REXX:}}
<pre>
Line 1,933 ⟶ 2,060:
=={{header|Ruby}}==
<
nan = 0.0 / 0.0 #
expression = [
Line 1,947 ⟶ 2,074:
expression.each do |exp|
puts "%15s => %p" % [exp, eval(exp)]
end</
{{out}}
Line 1,986 ⟶ 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.
<
let inf: f64 = 1. / 0.; // or std::f64::INFINITY
let minus_inf: f64 = -1. / 0.; // or std::f64::NEG_INFINITY
Line 2,007 ⟶ 2,134:
println!("NaN == NaN = {}", nan == nan);
println!("0.0 == -0.0 = {}", 0. == -0.);
}</
{{out}}
<pre>
Line 2,029 ⟶ 2,156:
=={{header|S-lang}}==
Each of these can be directly input; I'll calc the Infs for good measure:
<
() = printf("%S", $1[0]);
if (length($1) > 1) () = printf("\t%S\n", eval($1[1]));
else () = printf("\n");
}</
<pre>-0.0
inf inf
Line 2,039 ⟶ 2,166:
nan
</pre>
<
() = 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 ");
</
<pre>-0.0 and 0.0 are equal
-_Inf == _Inf are not equal
Line 2,051 ⟶ 2,178:
=={{header|Scala}}==
{{libheader|Scala}}
<
val negInf = -1.0 / 0.0 //also Double.NegativeInfinity
val inf = 1.0 / 0.0 // //also Double.PositiveInfinity
Line 2,065 ⟶ 2,192:
println(f"0 * NaN: ${0 * nan}%9s ${(inf + negInf).isInfinity}%9s ${(inf + negInf).isWhole}%9s")
println(f"NaN == NaN: ${nan == nan}%9s")
}</
{{out}}
<pre>Value: Result: Infinity? Whole?
Line 2,078 ⟶ 2,205:
=={{header|Scheme}}==
<
(define minus-infinity (- infinity))
(define zero 0.0)
Line 2,087 ⟶ 2,214:
(list +inf.0 -inf.0 0.0 -0.0 +nan.0))
; #t
</syntaxhighlight>
=={{header|Seed7}}==
Line 2,099 ⟶ 2,226:
can be used. NaN can be checked with [http://seed7.sourceforge.net/libraries/float.htm#isNaN%28ref_float%29 isNaN].
<
include "float.s7i";
Line 2,124 ⟶ 2,251:
writeln("isNegativeZero(-0.0) = " <& isNegativeZero(-0.0));
writeln("isNegativeZero(0.0) = " <& isNegativeZero(0.0));
end func;</
{{out}}
Line 2,149 ⟶ 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.
<
var nan = 0/0 # same as: NaN
Line 2,178 ⟶ 2,305:
say("atan(Inf) = ", atan(inf))
say("log(-1) = ", log(-1))
say("atanh(Inf) = ", atanh(inf))</
{{out}}
<pre>
Line 2,223 ⟶ 2,350:
atanh(Inf) = 1.57079632679489661923132169163975144209858469969i
</pre>
=={{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):
<syntaxhighlight lang="smalltalk ">FloatE nan
FloatD nan
FloatE infinity
FloatD infinity
FloatE negativeInfinity
FloatD negativeInfinity
Float zero -> 0.0
Float negativeZero -> -0.0
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}}==
Line 2,228 ⟶ 2,443:
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:
<
+1.0000000000000X+3ff
Line 2,238 ⟶ 2,453:
. display %21x c(maxdouble)
+1.fffffffffffffX+3fe</
Notice that .z > ... > .a > . and . is greater than any real number, and c(maxdouble) is the value 8.9884656743115785e+307.
Line 2,245 ⟶ 2,460:
=={{header|Swift}}==
<
let inf = 1.0 / 0.0 //also Double.infinity
let nan = 0.0 / 0.0 //also Double.NaN
Line 2,256 ⟶ 2,471:
println("inf + -inf: \(inf + negInf)")
println("0 * NaN: \(0 * nan)")
println("NaN == NaN: \(nan == nan)")</
{{out}}
<pre>
Line 2,274 ⟶ 2,489:
(they are parsed case-insensitively).
For example, see this log of an interactive session:
<
8.5.2
% expr inf+1
Line 2,289 ⟶ 2,504:
domain error: argument not in valid range
% expr {1/-inf}
-0.0</
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):
<
1
% puts $nan
Line 2,300 ⟶ 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 "+"</
=={{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.
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.
<syntaxhighlight lang="wren">// using pre-defined constants
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()
// 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}}
<pre>
[infinity, -infinity, nan, -0]
[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>
|