Infinity: Difference between revisions
m
→{{header|Fōrmulæ}}
Puppydrum64 (talk | contribs) mNo edit summary |
|||
(20 intermediate revisions by 12 users not shown) | |||
Line 13:
=={{header|11l}}==
<syntaxhighlight lang
{{out}}
Line 22:
=={{header|ActionScript}}==
ActionScript has the built in function isFinite() to test if a number is finite or not.
<
trace(isFinite(5 / 0)); // outputs "false"</
=={{header|Ada}}==
<
procedure Infinities is
Line 49:
Put_Line ("Supremum" & Float'Image (Sup));
Put_Line ("Infimum " & Float'Image (Inf));
end Infinities;</
The language-defined attribute Machine_Overflows is defined for each floating-point type. It is true when an overflow or divide-by-zero results in Constraint_Error exception propagation. When the underlying machine type is incapable to implement this semantics the attribute is false. It is to expect that on the machines with [[IEEE]] 754 hardware Machine_Overflows is true. The language-defined attributes Succ and Pred yield the value next or previous to the argument, correspondingly.
Line 60:
Here is the code that should work for any type on any machine:
<
procedure Infinities is
Line 85:
Put_Line ("Supremum" & Real'Image (Sup));
Put_Line ("Infimum " & Real'Image (Inf));
end Infinities;</
Sample output. Note that the compiler is required to generate Constraint_Error even if the hardware is [[IEEE]] 754. So the upper and lower bounds are 10.0 and -10.0:
<pre>
Line 93:
===Getting rid of IEEE ideals===
There is a simple way to strip [[IEEE]] 754 ideals (non-numeric values) from a predefined floating-point type such as Float or Long_Float:
<
The subtype Safe_Float keeps all the range of Float, yet behaves properly upon overflow, underflow and zero-divide.
Line 107:
ALGOL 68 does have some 7 built in [[Exceptions#ALGOL_68|exceptions]], these might be used to detect exceptions during transput, and so <u>if</u> the underlying hardware <u>does</u> support ∞, then it would be detected with a ''on value error'' while printing and if ''mended'' would appear as a field full of ''error char''.
<
printf(($"long max int: "gl$,long max int));
printf(($"long long max int: "gl$,long long max int));
Line 113:
printf(($"long max real: "gl$,long max real));
printf(($"long long max real: "gl$,long long max real));
printf(($"error char: "gl$,error char))</
Output:
<pre>
Line 132:
that support infinity, this will be infinity. Otherwise, it will be some large, but finite value.
<syntaxhighlight lang
{{out}}
Line 143:
=={{header|Argile}}==
{{trans|C}} (simplified)
<
printf "%f\n" atof "infinity" (: this prints "inf" :)
#extern :atof<text>: -> real</
=={{header|Arturo}}==
<
print neg infinity</
{{out}}
Line 158:
=={{header|AWK}}==
<syntaxhighlight lang="awk"> BEGIN {
k=1;
while (2^(k-1) < 2^k) k++;
INF = 2^k;
print INF;
}</
This has been tested with GAWK 3.1.7 and MAWK, both return
<pre> inf </pre>
=={{header|
<syntaxhighlight lang="basic256">onerror TratoError
infinity = 1e300*1e300
end
Line 176:
TratoError:
if lasterror = 29 then print lasterrormessage
return</
==={{header|BBC BASIC}}===
{{works with|BBC BASIC for Windows}}
<
PRINT FNinfinity
END
Line 198:
RESTORE ERROR
UNTIL maxpos = prev
= maxpos</
Output:
<pre>
Line 204:
</pre>
==={{header|bootBASIC}}===
There are no floating point numbers in bootBASIC. All numbers and variables are 2 byte unsigned integers.
The code below can't print anything on the screen, plus the program won't end. No way is currently known to break out of the program.
<syntaxhighlight lang
=={{header|BQN}}==
Line 228 ⟶ 230:
C89 has a macro HUGE_VAL in <math.h>. HUGE_VAL is a <tt>double</tt>. HUGE_VAL will be infinity if infinity exists, else it will be the largest possible number. HUGE_VAL is a <tt>double</tt>.
<
#include <stdio.h> /* printf() */
Line 238 ⟶ 240:
printf("%g\n", inf());
return 0;
}</
The output from the above program might be "inf", "1.#INF", or something else.
Line 244 ⟶ 246:
C99 also has a macro for infinity:
<
#include <math.h>
Line 252 ⟶ 254:
printf("%g\n", INFINITY);
return 0;
}</
=={{header|C sharp|C#}}==
<
class Program
Line 268 ⟶ 270:
Console.WriteLine(PositiveInfinity());
}
}</
Output:
<syntaxhighlight lang="text">Infinity</
=={{header|C++}}==
<
double inf()
Line 282 ⟶ 284:
else
return std::numeric_limits<double>::max();
}</
=={{header|Clojure}}==
{{trans|Java}}
Java's floating-point types (float, double) all support infinity. Clojure has literals for infinity:
<
##-Inf ;; same as Double/NEGATIVE_INFINITY
(Double/isInfinite ##Inf) ;; true</
The largest possible number in Java (without using the Big classes) is also in the Double class
Line 299 ⟶ 301:
JavaScript has a special global property called "Infinity":
<syntaxhighlight lang
as well as constants in the Number class:
<
Number.NEGATIVE_INFINITY</
The global isFinite function tests for finiteness:
<syntaxhighlight lang
=={{header|Common Lisp}}==
Line 313 ⟶ 315:
{{works with|LispWorks}} 5.1.2, Intel, OS X, 32-bit
<
MOST-POSITIVE-LONG-FLOAT, value: 1.7976931348623158D308
MOST-POSITIVE-SHORT-FLOAT, value: 3.4028172S38
Line 325 ⟶ 327:
MOST-NEGATIVE-SHORT-FLOAT, value: -3.4028172S38
MOST-NEGATIVE-DOUBLE-FLOAT, value: -1.7976931348623158D308
MOST-NEGATIVE-FIXNUM, value: -536870912</
=={{header|Component Pascal}}==
BlackBox Component Builder
<
MODULE Infinity;
IMPORT StdLog;
Line 341 ⟶ 343:
END Do;
</syntaxhighlight>
Execute: ^Q Infinity.Do<br/>
Output:
Line 350 ⟶ 352:
=={{header|D}}==
<
return typeof(1.5).infinity;
}
void main() {}</
=={{header|Delphi}}==
Delphi defines the following constants in Math:
<
NegInfinity = -1.0 / 0.0;</
Test for infinite value using:
<syntaxhighlight lang
=={{header|Dyalect}}==
Line 369 ⟶ 371:
Dyalect floating point number support positive infinity:
<
=={{header|E}}==
<
return Infinity # predefined variable holding positive infinity
}</
=={{header|EasyLang}}==
<syntaxhighlight>
print number "inf"
# or
print 1 / 0
</syntaxhighlight>
=={{header|Eiffel}}==
<
class
APPLICATION
Line 397 ⟶ 407:
end
end
</syntaxhighlight>
Output:
Line 413 ⟶ 423:
floating and MAXLONGREAL for double precision floating. An infinity test can be achieved with
an EXCEPTION:
<syntaxhighlight lang="erre">
PROGRAM INFINITY
Line 429 ⟶ 439:
END WHILE
END PROGRAM
</syntaxhighlight>
=={{header|Euphoria}}==
<
? infinity -- outputs "inf"</
=={{header|F_Sharp|F#}}==
<
printfn "%f" (1.0/0.0)
</syntaxhighlight>
{{out}}
<pre>
Line 447 ⟶ 457:
=={{header|Factor}}==
<syntaxhighlight lang
=={{header|Fantom}}==
Line 453 ⟶ 463:
Fantom's <code>Float</code> data type is an IEEE 754 64-bit floating point type. Positive infinity is represented by the constant <code>posInf</code>.
<
class Main
{
Line 459 ⟶ 469:
public static Void main () { echo (getInfinity ()) }
}
</syntaxhighlight>
=={{header|Forth}}==
<
inf f. \ implementation specific. GNU Forth will output "inf"
Line 468 ⟶ 478:
\ IEEE infinity is the only value for which this will return true
: has-inf ( -- ? ) ['] inf catch if false else inf? then ;</
=={{header|Fortran}}==
ISO Fortran 2003 or later supports an IEEE_ARITHMETIC module which defines a wide range of intrinsic functions and types in support of IEEE floating point formats and arithmetic rules.
<
use, intrinsic :: ieee_arithmetic
integer :: i
Line 489 ⟶ 499:
end if
end do
end program to_f_the_ineffable</
ISO Fortran 90 or later supports a HUGE intrinsic which returns the largest value supported by the data type of the number given.
<
real :: huge_real = huge(x)</
=={{header|FreeBASIC}}==
<
#Include "crt/math.bi"
Line 504 ⟶ 514:
Print d; " (String representation of Positive Infinity)"
Sleep
</syntaxhighlight>
{{out}}
<pre>
1.#INF (String representation of Positive Infinity)
</pre>
=={{header|FutureBasic}}==
FB has a native definition for infinite floating point types. As demonstrated below, it returns "inf".
<syntaxhighlight lang="futurebasic">
printf @"%g", INFINITY
HandleEvents
</syntaxhighlight>
{{output}}
<pre>
inf
</pre>
=={{header|Fōrmulæ}}==
{{FormulaeEntry|page=https://formulae.org/?script=examples/Infinity}}
'''Solution'''
Fōrmulæ does not use floating point numbers, but arbitrary-size integers and arbitrary-precision decimal numbers.
Infinity is a predefined expression in Fōrmulæ.
Reduction of certain expressions can produce it:
[[File:Fōrmulæ - Infinity 01.png]]
[[File:Fōrmulæ - Infinity 02.png]]
=={{header|GAP}}==
<
inf := FLOAT_INT(1) / FLOAT_INT(0);
Line 528 ⟶ 559:
# GAP has also a formal ''infinity'' value
infinity in Cyclotomics;
# true</
=={{header|Go}}==
<
import (
Line 549 ⟶ 580:
x = posInf() // test function
fmt.Println(x, math.IsInf(x, 1)) // demonstrate result
}</
Output:
<pre>
Line 557 ⟶ 588:
=={{header|Groovy}}==
Groovy, like Java, requires full support for IEEE 32-bit (Float) and 64-bit (Double) formats. So the solution function would simply return either the Float or Double constant encoded as IEEE infinity.
<
Test program:
<
printf ( "0x%xL \n", Double.doubleToLongBits(biggest()) )</
Output:
Line 573 ⟶ 604:
Nevertheless, the following may come close to the task description:
<
maxRealFloat x = encodeFloat b (e-1) `asTypeOf` x where
b = floatRadix x - 1
Line 580 ⟶ 611:
infinity :: RealFloat a => a
infinity = if isInfinite inf then inf else maxRealFloat 1.0 where
inf = 1/0</
Test for the two standard floating point types:
<
Infinity
*Main> infinity :: Double
Infinity</
Or you can simply use division by 0:
<
Infinity
Prelude> 1 / 0 :: Double
Infinity</
Or use "read" to read the string representation:
<
Infinity
Prelude> read "Infinity" :: Double
Infinity</
=={{header|Icon}} and {{header|Unicon}}==
Line 610 ⟶ 641:
IDL provides the standard IEEE values for _inf and _NaN in the !Values system structure:
<
print, !Values.D_infinity ;; for doubles</
=={{header|Io}}==
<
or
<syntaxhighlight lang
=={{header|IS-BASIC}}==
<syntaxhighlight lang
Output:
<pre>
Line 632 ⟶ 663:
Example:
<syntaxhighlight lang="j">
_ * 5 NB. multiplying infinity to 5 results in infinity
_
Line 639 ⟶ 670:
5 % 0 NB. dividing 5 by 0 results in infinity
_
</syntaxhighlight>
=={{header|Java}}==
Java's floating-point types (<tt>float</tt>, <tt>double</tt>) all support infinity. You can get infinity from constants in the corresponding wrapper class; for example, <tt>Double</tt>:
<
Double.isInfinite(infinity); //true</
As a function:
<
return Double.POSITIVE_INFINITY;
}</
The largest possible number in Java (without using the <tt>Big</tt> classes) is also in the <tt>Double</tt> class.
<
Its value is (2-2<sup>-52</sup>)*2<sup>1023</sup> or 1.7976931348623157*10<sup>308</sup> (a.k.a. "big"). Other number classes (<tt>Integer</tt>, <tt>Long</tt>, <tt>Float</tt>, <tt>Byte</tt>, and <tt>Short</tt>) have maximum values that can be accessed in the same way.
=={{header|JavaScript}}==
JavaScript has a special global property called "Infinity":
<syntaxhighlight lang
as well as constants in the Number class:
<
Number.NEGATIVE_INFINITY</
The global isFinite() function tests for finiteness:
<syntaxhighlight lang
=={{header|
<syntaxhighlight lang="joy">1 1024 ldexp dup neg stack.</syntaxhighlight>
{{out}}
<pre>[-inf inf]</pre>
=={{header|jq}}==
Sufficiently recent versions of the C, Go and Rust implementations of jq (jq, gojq, and jaq, respectively) all allow `infinite` as a scalar value in jq programs; jq and gojq display its value as 1.7976931348623157e+308. The C implementation also allows the token `inf` when reading JSON, and stores it as `infinite`.
<syntaxhighlight lang
To test whether a jq value is equal to `infinite` or `- infinite`, one can use the built-in filter `isinfinite`. One can also use `==` in the expected manner.
=={{header|Julia}}==
Julia uses IEEE floating-point arithmetic and includes a built-in constant `Inf` for (64-bit) floating-point infinity. Inf32 can be used as 32-bit infinity, when avoiding type promotions to Int64.
<syntaxhighlight lang="julia">
julia> julia> Inf32 == Inf64 == Inf16 == Inf
true
</syntaxhighlight>
=={{header|K}}==
K has predefined positive and negative integer and float infinities: -0I, 0I, -0i, 0i. They have following properties:
{{works with|Kona}}
<
/ 0I is just 2147483647
/ -0I is just -2147483647
Line 733 ⟶ 767:
/ but
0.0%0.0
0.0</
=={{header|Klingphix}}==
<
" " input</
=={{header|Kotlin}}==
<
val p = Double.POSITIVE_INFINITY // +∞
println(p.isInfinite()) // true
Line 751 ⟶ 785:
println(n.isFinite()) // false
println("${n < 0} ${n > 0}") // true false
}</
{{out}}
<pre>true
Line 762 ⟶ 796:
=={{header|Lambdatalk}}==
Lambdatalk is built on Javascript and can inherit lots of its capabilities. For instance:
<
{/ 1 0}
-> Infinity
Line 771 ⟶ 805:
{< {pow 10 1000} Infinity}
-> false
</syntaxhighlight>
=={{header|Lasso}}==
Lasso supports 64-bit decimals.. This gives Lasso's decimal numbers a range from approximately negative to positive 2x10^300 and with precision down to 2x10^-300. Lasso also supports decimal literals for NaN (not a number) as well and positive and negative infinity.
<
'<br />'
infinity -> type</
-> inf
Line 785 ⟶ 819:
Lingo stores floats using IEEE 754 double-precision (64-bit) format.
INF is not a constant that can be used programmatically, but only a special return value.
<
put ilk(x), x
-- #float 1.79769313486232e308
Line 791 ⟶ 825:
x = (1-power(2, -53)) * power(2, 1023) * 3
put ilk(x), x, -x
-- #float INF -INF</
=={{header|Lua}}==
<
function infinity()
return 1/0 --lua uses unboxed C floats for all numbers
end
</syntaxhighlight>
=={{header|M2000 Interpreter}}==
<syntaxhighlight lang="m2000 interpreter">
Rem : locale 1033
Module CheckIt {
Line 849 ⟶ 883:
}
Checkit
</syntaxhighlight>
=={{header|Maple}}==
Maple's floating point numerics are a strict extension of IEEE/754 and IEEE/854 so there is already a built-in infinity. (In fact, there are several.) The following procedure just returns the floating point (positive) infinity directly.
<syntaxhighlight lang="maple">
> proc() Float(infinity) end();
Float(infinity)
</syntaxhighlight>
There is also an exact infinity ("infinity"), a negative float infinity ("Float(-infinity)" or "-Float(infinity)") and a suite of complex infinities. The next procedure returns a boxed machine (double precision) float infinity.
<syntaxhighlight lang="maple">
> proc() HFloat(infinity) end();
HFloat(infinity)
</syntaxhighlight>
=={{header|Mathematica}} / {{header|Wolfram Language}}==
Mathematica has infinity built-in as a symbol. Which can be used throughout the software:
<
1/Infinity
Integrate[Exp[-x^2], {x, -Infinity, Infinity}]
10^100 < Infinity</
gives back:
<pre>Pi^2/6
Line 875 ⟶ 909:
True</pre>
Moreover Mathematica has 2 other variables that represent 'infinity': DirectedInfinity[r] and ComplexInfinity. DirectInfinity[r] represents an infinite quantity with complex direction r. ComplexInfinity represents an infinite quantity with an undetermined direction; like 1/0. Which has infinite size but undetermined direction. So the general infinity is DirectedInfinity, however if the direction is unknown it will turn to ComplexInfinity, DirectedInfinity[-1] will return -infinity and DirectedInfinity[1] will return infinity. Directed infinity can, for example, be used to integrate over an infinite domain with a given complex direction: one might want to integrate Exp[-x^2]/(x^2-1) from 0 to DirectedInfinity[Exp[I Pi/4]]:
<
gives back:
<pre>-((Pi (I+Erfi[1]))/(2 E))</pre>
Line 882 ⟶ 916:
MATLAB implements the IEEE 754 floating point standard as the default for all numeric data types. +Inf and -Inf are by default implemented and supported by MATLAB. To check if a variable has the value +/-Inf, one can use the built-in function "isinf()" which will return a Boolean 1 if the number is +/-inf.
<
isinf(a)
</syntaxhighlight>
Returns:
Line 893 ⟶ 927:
=={{header|Maxima}}==
<
declare(x, real)$
Line 906 ⟶ 940:
1.0/0.0;
/* expt: undefined: 0 to a negative exponent.
-- an error. To debug this try: debugmode(true); */</
=={{header|Metafont}}==
Line 912 ⟶ 946:
Metafont numbers are a little bit odd (it uses fixed binary arithmetic). For Metafont, the biggest number (and so the one which is also considered to be infinity) is 4095.99998. In fact, in the basic set of macros for Metafont, we can read
<
=={{header|MiniScript}}==
MiniScript uses IEEE numerics, so:
<
print posInfinity</
{{out}}
<pre>INF</pre>
=={{header|Modula-2}}==
<
IMPORT InOut;
Line 930 ⟶ 964:
InOut.WriteReal (1.0 / 0.0, 12, 12);
InOut.WriteLn
END inf.</
Producing
<
**** RUNTIME ERROR bound check error
Floating point exception</
=={{header|Modula-3}}==
Line 941 ⟶ 975:
If the implementation doesn't support IEEE floats, the program prints arbitrary values (Critical Mass Modula-3 implementation does support IEEE floats).
<
IMPORT IO, IEEESpecial;
Line 948 ⟶ 982:
IO.PutReal(IEEESpecial.RealPosInf);
IO.Put("\n");
END Inf.</
Output:
Line 957 ⟶ 991:
=={{header|Nemerle}}==
Both single and double precision floating point numbers support PositiveInfinity, NegativeInfinity and NaN.
<
def a = IsInfinity(posinf); // a = true
def b = IsNegativeInfinity(posinf); // b = false
def c = IsPositiveInfinity(posinf); // c = true</
=={{header|Nim}}==
<syntaxhighlight lang
is a predefined constant in Nim:
<
echo f</
=={{header|NS-HUBASIC}}==
<syntaxhighlight lang
{{out}}
?DZ ERROR is a division by zero error in NS-HUBASIC.
Line 977 ⟶ 1,011:
=={{header|OCaml}}==
<syntaxhighlight lang
is already a pre-defined value in OCaml.
Line 989 ⟶ 1,023:
=={{header|Oforth}}==
<
{{out}}
Line 1,004 ⟶ 1,038:
-inf.0 ; negative infinity
<
(define (infinite? x) (or (equal? x +inf.0) (equal? x -inf.0)))
Line 1,013 ⟶ 1,047:
(infinite? 1/3456) ==> #false
(infinite? 17+28i) ==> #false
</syntaxhighlight>
=={{header|OpenEdge/Progress}}==
Line 1,019 ⟶ 1,053:
The unknown value (represented by a question mark) can be considered to equal infinity. There is no difference between positive and negative infinity but the unknown value sometimes sorts low and sometimes sorts high when used in queries.
<
1.0 / 0.0 SKIP
-1.0 / 0.0 SKIP(1)
( 1.0 / 0.0 ) = ( -1.0 / 0.0 )
VIEW-AS ALERT-BOX.</
Output
Line 1,040 ⟶ 1,074:
=={{header|OxygenBasic}}==
Using double precision floats:
<
print 1.5e-400 '0
Line 1,061 ⟶ 1,095:
print f '#-INF
</syntaxhighlight>
=={{header|Oz}}==
<
PosInf = 1./0.
NegInf = ~1./0.
Line 1,076 ⟶ 1,110:
PosInf * PosInf = PosInf
PosInf * NegInf = NegInf
NegInf * NegInf = PosInf</
=={{header|PARI/GP}}==
{{works with|PARI/GP|version 2.8.0 and higher}}
<syntaxhighlight lang
{{works with|PARI/GP|version 2.2.9 to 2.7.0}}
<
[1] \\ Used for many functions like intnum
};</
=={{header|Pascal}}==
Line 1,092 ⟶ 1,126:
=={{header|Perl}}==
Positive infinity:
<
my $y = 0 + "+inf";</
Negative infinity:
<
my $y = 0 + "-inf";</
The "<code>0 + </code>..." is used here to make sure that the variable stores a value that is actually an infinitive number instead of just a string <code>"inf"</code> but in practice one can use just:
<
and <code>$x</code> while originally holding a string will get converted to an infinite number when it is first used as a number.
Some programmers use expressions that overflow the IEEE floating point numbers such as:
<
which is 10<sup>1000</sup> or googol<sup>10</sup> or even numbers like this one:
<
which is 10<sup>10000000000</sup> but it has to make some assumptions about the underlying hardware format and its size. Furthermore, using such literals in the scope of some pragmas such as <code>bigint</code>, <code>bignum</code> or <code>bigrat</code> would actually compute those numbers:
<
my $x = 1e1000;
my $y = 10**10**10; # N.B. this will consume vast quantities of RAM</
Here the <code>$x</code> and <code>$y</code> when printed would give 1001 and 10000000001-digit numbers respectively, the latter taking no less than 10GB of space to just output.
Under those pragmas, however, there is a simpler way to use infinite values, thanks to the <code>inf</code> symbol being exported into the namespace by default:
<
my $x = inf;
my $y = -inf;</
=={{header|Phix}}==
<!--<
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
<span style="color: #008080;">constant</span> <span style="color: #000000;">infinity</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: #000000;">infinity</span>
<!--</
{{out}}
desktop/Phix:
Line 1,134 ⟶ 1,168:
=={{header|Phixmonti}}==
<
=={{header|PHP}}==
This is how you get infinity:
<syntaxhighlight lang
Unfortunately, "1.0 / 0.0" doesn't evaluate to infinity; but instead seems to evaluate to False, which is more like 0 than infinity.
Line 1,149 ⟶ 1,183:
support (scaled bignum arithmetics), but some functions return 'T' for infinite
results.
<
: (exp 1000.0)
-> T</
=={{header|PL/I}}==
<syntaxhighlight lang="pl/i">
declare x float, y float (15), z float (18);
put skip list (huge(x), huge(y), huge(z));
</syntaxhighlight>
=={{header|PostScript}}==
<
=={{header|PowerShell}}==
A .NET floating-point number representing infinity is available.
<
[double]::PositiveInfinity
}</
=={{header|PureBasic}}==
Line 1,174 ⟶ 1,208:
PureBasic uses [[wp:IEEE_754-2008|IEEE 754]] coding for float types. PureBasic also includes the function <tt>Infinity()</tt> that return the positive value for infinity and the boolean function <tt>IsInfinite(value.f)</tt> that returns true if the floating point value is either positive or negative infinity.
<
Define.d a, b
b = 0
Line 1,193 ⟶ 1,227:
CloseConsole()
EndIf
</syntaxhighlight>
''Outputs''
Line 1,203 ⟶ 1,237:
=={{header|Python}}==
This is how you get infinity:
<
inf</
''Note: When passing in a string to float(), values for NaN and Infinity may be returned, depending on the underlying C library. The specific set of strings accepted which cause these values to be returned depends entirely on the underlying C library used to compile Python itself, and is known to vary.'' <br>
''The Decimal module explicitly supports +/-infinity Nan, +/-0.0, etc without exception.''
Floating-point division by 0 doesn't give you infinity, it raises an exception:
<
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ZeroDivisionError: float division</
If <tt>float('infinity')</tt> doesn't work on your platform, you could use this trick:
Line 1,220 ⟶ 1,254:
=={{header|QB64}}==
<
//save as inf.h
double inf(void){
return HUGE_VAL;
}</
<
Function inf#
End Declare
Print inf</
=={{header|QBasic}}==
{{works with|QBasic|1.1}}
<
ON ERROR GOTO TratoError
Line 1,259 ⟶ 1,293:
FUNCTION f!
f! = -1.5 / 0
END FUNCTION</
=={{header|R}}==
<
-Inf #negative infinity
.Machine$double.xmax # largest finite floating-point number
Line 1,273 ⟶ 1,307:
forcefinite(c(1, -1, 0, .Machine$double.xmax, -.Machine$double.xmax, Inf, -Inf))
# [1] 1.000000e+00 -1.000000e+00 0.000000e+00 1.797693e+308
# [5] -1.797693e+308 1.797693e+308 -1.797693e+308</
=={{header|Racket}}==
Line 1,279 ⟶ 1,313:
as in Scheme:
<
+inf.0 ; positive infinity
(define (finite? x) (< -inf.0 x +inf.0))
(define (infinite? x) (not (finite? x)))</
=={{header|Raku}}==
(formerly Perl 6)
Inf support is required by language spec on all abstract Numeric types (in the absence of subset constraints) including Num, Rat and Int types. Native integers cannot support Inf, so attempting to assign Inf will result in an exception; native floats are expected to follow IEEE standards including +/- Inf and NaN.
<syntaxhighlight lang="raku"
my $y = (1.5/0).Num; # assigns 'Inf'</
=={{header|REXX}}==
Line 1,330 ⟶ 1,364:
=={{header|RLaB}}==
<syntaxhighlight lang="rlab">
>> x = inf()
inf
Line 1,339 ⟶ 1,373:
>> -inf() > 10
0
</syntaxhighlight>
=={{header|RPL}}==
{{in}}
<pre>
MAXR →NUM
</pre>
{{out}}
<pre>
1: 1.7976931348E+308
</pre>
=={{header|Ruby}}==
Infinity is a Float value
<
a.finite? # => false
a.infinite? # => 1
Line 1,352 ⟶ 1,395:
a = Float::MAX # => 1.79769313486232e+308
a.finite? # => true
a.infinite? # => nil</
{{works with|Ruby|1.9.2+}}
<
=={{header|Rust}}==
Rust has builtin function for floating types which returns infinity. This program outputs 'inf'.
<
let inf = f32::INFINITY;
println!("{}", inf);
}</
=={{header|Scala}}==
Line 1,369 ⟶ 1,412:
* [[Extreme_floating_point_values#Scala]]
In order to be compliant with IEEE-754, Scala has all support for infinity on its floating-point types (<tt>float</tt>, <tt>double</tt>). You can get infinity from constants in the corresponding wrapper class; for example, <tt>Double</tt>:
<
inf.isInfinite; //true</
The largest possible number in Scala (without using the <tt>Big</tt> classes) is also in the <tt>Double</tt> class.
<
REPL session:
<
res2: Double = Infinity
Line 1,385 ⟶ 1,428:
scala> 1 / Double.NegativeInfinity
res5: Double = -0.0</
=={{header|Scheme}}==
<
(define (finite? x) (< -inf.0 x +inf.0))
(define (infinite? x) (not (finite? x)))</
=={{header|Seed7}}==
Line 1,396 ⟶ 1,439:
The library [http://seed7.sourceforge.net/libraries/float.htm float.s7i] defines
the constant [http://seed7.sourceforge.net/libraries/float.htm#Infinity Infinity] as:
<
Checks for infinity can be done by comparing with this constant.
=={{header|Sidef}}==
<
say a.is_inf # true
say a.is_pos # true
Line 1,410 ⟶ 1,453:
var inf = Inf
var ninf = -Inf
say (inf == -ninf) # true</
=={{header|Slate}}==
<syntaxhighlight lang
=={{header|Smalltalk}}==
Line 1,427 ⟶ 1,470:
{{works with|Smalltalk/X}}
The behavior is slightly different, in that an exception is raised if you divide by zero:
<
1.0 / 0.0 -> "ZeroDivide exception"</
but we can simulate the other behavior with:
<
1.0 / 0.0
] on: ZeroDivide do:[:ex |
ex proceedWith: (FloatD infinity)
]
-> INF</
=={{header|Standard ML}}==
<syntaxhighlight lang
<pre>
Line 1,449 ⟶ 1,492:
=={{header|Swift}}==
Swift's floating-point types (<tt>Float</tt>, <tt>Double</tt>, and any other type that conforms to the <tt>FloatingPointNumber</tt> protocol) all support infinity. You can get infinity from the <tt>infinity</tt> class property in the type:
<
inf.isInfinite //true</
As a function:
<
return Double.infinity
}</
=={{header|Tcl}}==
Line 1,460 ⟶ 1,503:
Tcl 8.5 has Infinite as a floating point value, not an integer value
<
expr {1.0 / 0} ;# ==> Inf
expr {-1.0 / 0} ;# ==> -Inf
expr {inf} ;# ==> Inf
expr {1 / 0} ;# ==> "divide by zero" error; Inf not part of range of integer division</
A maximal integer is not easy to find, as Tcl switches to unbounded integers when a 64-bit integer is about to roll over:
<
ffffffffffffffff
Line 1,478 ⟶ 1,521:
% incr ii
9223372036854775808 ;# silently upgrade to unbounded integer, still positive</
A theoretical MAXINT, though very impractical, could be
string repeat 9 [expr 2**32-1]
Line 1,484 ⟶ 1,527:
=={{header|TI-89 BASIC}}==
<syntaxhighlight lang
=={{header|TorqueScript}}==
<
{
return 1/0;
}</
=={{header|Trith}}==
The following functions are included as part of the core operators:
<
: inf 1.0 0.0 / ;
: -inf inf neg ;
: inf? abs inf = ;
</syntaxhighlight>
=={{header|Ursa}}==
Infinity is a defined value in Ursa.
<
set d Infinity</
=={{header|Ursala}}==
Line 1,512 ⟶ 1,555:
<
infinity = inf!</
=={{header|Visual Basic}}==
Line 1,524 ⟶ 1,567:
Positive infinity, negative infinity and indefinite number (usable as NaN) can be generated by deliberately dividing by zero under the influence of <code>On Error Resume Next</code>:
<
Private Declare Sub GetMem8 Lib "msvbvm60.dll" _
Line 1,550 ⟶ 1,593:
DoubleAsHex = Right$(String$(8, "0") & Hex$(l(1)), 8) _
& Right$(String$(8, "0") & Hex$(l(0)), 8)
End Function</
{{out}}<pre>PlusInfinity = 1,#INF (7FF0000000000000)
MinusInfinity = -1,#INF (FFF0000000000000)
Line 1,556 ⟶ 1,599:
</pre>
=={{header|V (Vlang)}}==
<syntaxhighlight lang="v (vlang)">import math
fn main() {
Line 1,565 ⟶ 1,608:
x = math.inf(1)
println('$x ${math.is_inf(x, 1)}') // demonstrate result
}</
=={{header|Wren}}==
Wren certainly supports infinity for floating point numbers as we already have a method ''Num.isInfinity'' to test for it.
<
var y = x / 0
System.print("x = %(x)")
System.print("y = %(y)")
System.print("'x' is infinite? %(x.isInfinity)")
System.print("'y' is infinite? %(y.isInfinity)")</
{{out}}
Line 1,586 ⟶ 1,629:
=={{header|XPL0}}==
The IEEE 754 floating point standard is used.
<
real X;
[Format(0, 15); \output in scientific notation
Line 1,593 ⟶ 1,636:
A(1):= $7FEF_FFFF;
RlOut(0, X); \display it
]</
{{out}}
Line 1,601 ⟶ 1,644:
=={{header|Yabasic}}==
<
if str$(infinity) = "inf" print "Infinity"</
=={{header|Zig}}==
'''Works with:''' 0.10.x, 0.11.x, 0.12.0-dev.1577+9ad03b628
Assumes that defaul float optimization mode was not changed via @setFloatMode (performed in Strict mode, not Optimized, latter is equivalent to -ffast-math).
<syntaxhighlight lang="zig">const std = @import("std");
const math = std.math;
test "infinity" {
const expect = std.testing.expect;
const float_types = [_]type{ f16, f32, f64, f80, f128, c_longdouble };
inline for (float_types) |T| {
const infinite_value: T = comptime std.math.inf(T);
try expect(!math.isFinite(infinite_value));
}
}</
{{out}}
<pre>
$ zig test src/infinity_float.zig
All 1 tests passed.
</pre>
=={{header|zkl}}==
zkl doesn't like INF, NaN, etc but sorta knows about them:
<syntaxhighlight lang
{{out}}
<pre>
Line 1,649 ⟶ 1,686:
=={{header|ZX Spectrum Basic}}==
ZX Spectrum BASIC has no infinity handling; <syntaxhighlight lang
A quick doubling loop will get you halfway to the maximum floating point value:
<
20 PRINT z
30 LET z=z*2
40 GO TO 20</
Output will end with:
|