Infinity: Difference between revisions

23,437 bytes added ,  2 months ago
m
(117 intermediate revisions by 68 users not shown)
Line 1:
{{task|Basic language learning}}
{{task|Basic language learning}}[[Category:Discrete math]]Write a function which tests if infinity is supported for floating point numbers (this step should be omitted for languages where the language specification already demands the existence of infinity, e.g. by demanding [[IEEE]] numbers), and if so, returns positive infinity. Otherwise, return the largest possible positive floating point number.
[[Category:Discrete math]]
 
;Task:
For languages with several floating point types, use the type of the literal constant 1.5 as floating point type.
Write a function which tests if infinity is supported for floating point numbers (this step should be omitted for languages where the language specification already demands the existence of infinity, e.g. by demanding [[IEEE]] numbers), and if so, returns positive infinity.   Otherwise, return the largest possible positive floating point number.
 
For languages with several floating point types, use the type of the literal constant   '''1.5'''   as floating point type.
C.F. [[Extreme floating point values]]
 
 
;Related task:
*   [[Extreme floating point values]]
<br><br>
 
=={{header|11l}}==
<syntaxhighlight lang="11l">print(Float.infinity)</syntaxhighlight>
 
{{out}}
<pre>
inf
</pre>
 
=={{header|ActionScript}}==
ActionScript has the built in function isFinite() to test if a number is finite or not.
<langsyntaxhighlight lang="actionscript">trace(5 / 0); // outputs "Infinity"
trace(isFinite(5 / 0)); // outputs "false"</langsyntaxhighlight>
 
=={{header|Ada}}==
<langsyntaxhighlight lang="ada">with Ada.Text_IO; use Ada.Text_IO;
 
procedure Infinities is
Line 34 ⟶ 49:
Put_Line ("Supremum" & Float'Image (Sup));
Put_Line ("Infimum " & Float'Image (Inf));
end Infinities;</langsyntaxhighlight>
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 45 ⟶ 60:
 
Here is the code that should work for any type on any machine:
<langsyntaxhighlight lang="ada">with Ada.Text_IO; use Ada.Text_IO;
 
procedure Infinities is
Line 70 ⟶ 85:
Put_Line ("Supremum" & Real'Image (Sup));
Put_Line ("Infimum " & Real'Image (Inf));
end Infinities;</langsyntaxhighlight>
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 78 ⟶ 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:
<langsyntaxhighlight lang="ada">subtype Safe_Float is Float range Float'Range;</langsyntaxhighlight>
The subtype Safe_Float keeps all the range of Float, yet behaves properly upon overflow, underflow and zero-divide.
 
Line 92 ⟶ 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 &infin;, then it would be detected with a ''on value error'' while printing and if ''mended'' would appear as a field full of ''error char''.
 
<langsyntaxhighlight lang="algol68">printf(($"max int: "gl$,max int));
printf(($"long max int: "gl$,long max int));
printf(($"long long max int: "gl$,long long max int));
Line 98 ⟶ 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))</langsyntaxhighlight>
Output:
<pre>
Line 109 ⟶ 124:
error char: *
</pre>
 
=={{header|APL}}==
For built-in functions, reduction over an empty list returns the identity value for that function.
E.g., <code>+/⍬</code> gives <code>0</code>, and <code>×/⍬</code> gives 1.
 
The identity value for <code>⌊</code> (minimum) is the largest possible value. For APL implementations
that support infinity, this will be infinity. Otherwise, it will be some large, but finite value.
 
<syntaxhighlight lang="apl">inf ← {⌊/⍬}</syntaxhighlight>
{{out}}
 
[[GNU APL]]:
<pre>∞</pre>
 
[[Dyalog APL]]:
<pre>1.797693135E308</pre>
 
=={{header|Argile}}==
{{trans|C}} (simplified)
<langsyntaxhighlight Argilelang="argile">use std
printf "%f\n" atof "infinity" (: this prints "inf" :)
#extern :atof<text>: -> real</langsyntaxhighlight>
 
=={{header|AWKArturo}}==
 
<syntaxhighlight lang="rebol">print infinity
<lang AWK> BEGIN {
print neg infinity</syntaxhighlight>
 
{{out}}
 
<pre>∞
-∞</pre>
 
=={{header|AWK}}==
<syntaxhighlight lang="awk"> BEGIN {
k=1;
while (2^(k-1) < 2^k) k++;
INF = 2^k;
print INF;
}</langsyntaxhighlight>
 
This has been tested with GAWK 3.1.7 and MAWK, both return
<pre> inf </pre>
 
=={{header|BASIC}}==
==={{header|BASIC256}}===
<syntaxhighlight lang="basic256">onerror TratoError
infinity = 1e300*1e300
end
 
TratoError:
if lasterror = 29 then print lasterrormessage
return</syntaxhighlight>
 
==={{header|BBC BASIC}}===
{{works with|BBC BASIC for Windows}}
<syntaxhighlight lang="bbcbasic"> *FLOAT 64
PRINT FNinfinity
END
DEF FNinfinity
LOCAL supported%, maxpos, prev, inct
supported% = TRUE
ON ERROR LOCAL supported% = FALSE
IF supported% THEN = 1/0
RESTORE ERROR
inct = 1E10
REPEAT
prev = maxpos
inct *= 2
ON ERROR LOCAL inct /= 2
maxpos += inct
RESTORE ERROR
UNTIL maxpos = prev
= maxpos</syntaxhighlight>
Output:
<pre>
1.79769313E308
</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="BASIC">10 print 1/0</syntaxhighlight>
 
=={{header|BQN}}==
 
Positive infinity is just ∞:
 
<pre>
∞ + 1
∞ - 3
-∞
¯∞
∞ - ∞
NaN
</pre>
 
=={{header|C}}==
Line 133 ⟶ 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>.
 
<langsyntaxhighlight lang="c">#include <math.h> /* HUGE_VAL */
#include <stdio.h> /* printf() */
 
Line 143 ⟶ 240:
printf("%g\n", inf());
return 0;
}</langsyntaxhighlight>
 
The output from the above program might be "inf", "1.#INF", or something else.
Line 149 ⟶ 246:
C99 also has a macro for infinity:
 
<langsyntaxhighlight lang="c">#define _ISOC99_SOURCE
 
#include <math.h>
Line 157 ⟶ 254:
printf("%g\n", INFINITY);
return 0;
}</langsyntaxhighlight>
 
=={{header|C sharp|C#}}==
<langsyntaxhighlight lang="csharp">using System;
 
class Program
Line 173 ⟶ 270:
Console.WriteLine(PositiveInfinity());
}
}</langsyntaxhighlight>
Output:
<syntaxhighlight lang="text">Infinity</langsyntaxhighlight>
 
=={{header|C++}}==
 
<langsyntaxhighlight lang="cpp">#include <limits>
 
double inf()
Line 186 ⟶ 284:
else
return std::numeric_limits<double>::max();
}</langsyntaxhighlight>
 
=={{header|Clojure}}==
{{trans|Java}}
Java's floating-point types (float, double) all support infinity. YouClojure canhas get infinity from constants in the corresponding wrapper class;literals for example, Doubleinfinity:
<syntaxhighlight lang="clojure">##Inf ;; same as Double/POSITIVE_INFINITY
(def infinity Double/POSITIVE_INFINITY) ; defined as 1.0/0.0
##-Inf ;; same as Double/NEGATIVE_INFINITY
(Double/isInfinite infinity) ; true
(Double/isInfinite ##Inf) ;; true</syntaxhighlight>
 
The largest possible number in Java (without using the Big classes) is also in the Double class.
(def biggestNumber Double/MAX_VALUE). Its value is (1+(1-2^(-52)))*2^1023 or 1.7976931348623157*10^308 (a.k.a. "big"). Other number classes (Integer, Long, Float, Byte, and Short) have maximum values that can be accessed in the same way.
(def biggestNumber Double/MAX_VALUE)
Its value is (2-2-52)*21023 or 1.7976931348623157*10308 (a.k.a. "big"). Other number classes (Integer, Long, Float, Byte, and Short) have maximum values that can be accessed in the same way.
 
=={{header|CoffeeScript}}==
Line 202 ⟶ 301:
 
JavaScript has a special global property called "Infinity":
<syntaxhighlight lang ="coffeescript">Infinity</langsyntaxhighlight>
as well as constants in the Number class:
<langsyntaxhighlight lang="coffeescript">Number.POSITIVE_INFINITY
Number.NEGATIVE_INFINITY</langsyntaxhighlight>
 
The global isFinite function tests for finiteness:
<syntaxhighlight lang ="coffeescript">isFinite x</langsyntaxhighlight>
 
=={{header|Common Lisp}}==
Line 216 ⟶ 315:
{{works with|LispWorks}} 5.1.2, Intel, OS X, 32-bit
 
<langsyntaxhighlight lang="lisp">> (apropos "MOST-POSITIVE" :cl)
MOST-POSITIVE-LONG-FLOAT, value: 1.7976931348623158D308
MOST-POSITIVE-SHORT-FLOAT, value: 3.4028172S38
Line 228 ⟶ 327:
MOST-NEGATIVE-SHORT-FLOAT, value: -3.4028172S38
MOST-NEGATIVE-DOUBLE-FLOAT, value: -1.7976931348623158D308
MOST-NEGATIVE-FIXNUM, value: -536870912</langsyntaxhighlight>
 
=={{header|Component Pascal}}==
BlackBox Component Builder
<syntaxhighlight lang="oberon2">
MODULE Infinity;
IMPORT StdLog;
 
PROCEDURE Do*;
VAR
x: REAL;
BEGIN
x := 1 / 0;
StdLog.String("x:> ");StdLog.Real(x);StdLog.Ln
END Do;
 
</syntaxhighlight>
Execute: ^Q Infinity.Do<br/>
Output:
<pre>
x:> inf
</pre>
 
=={{header|D}}==
 
<langsyntaxhighlight lang="d">auto inf() {
return typeof(1.5).infinity;
}
 
void main() {}</langsyntaxhighlight>
 
=={{header|Delphi}}==
 
Delphi defines the following constants in Math:
<langsyntaxhighlight Delphilang="delphi"> Infinity = 1.0 / 0.0;
NegInfinity = -1.0 / 0.0;</langsyntaxhighlight>
Test for infinite value using:
<syntaxhighlight lang Delphi="delphi">Math.IsInfinite()</langsyntaxhighlight>
 
=={{header|Dyalect}}==
 
Dyalect floating point number support positive infinity:
 
<syntaxhighlight lang="dyalect">func infinityTask() => Float.Infinity</syntaxhighlight>
 
=={{header|E}}==
 
<langsyntaxhighlight lang="e">def infinityTask() {
return Infinity # predefined variable holding positive infinity
}</langsyntaxhighlight>
 
=={{header|EasyLang}}==
<syntaxhighlight>
print number "inf"
# or
print 1 / 0
 
</syntaxhighlight>
 
=={{header|Eiffel}}==
<langsyntaxhighlight lang="eiffel">
class
APPLICATION
Line 273 ⟶ 407:
end
end
</syntaxhighlight>
</lang>
 
Output:
Line 280 ⟶ 414:
True
</pre>
 
=={{header|Erlang}}==
 
No infinity available. Largest floating point number is supposed to be 1.80e308 (IEEE 754-1985 double precision 64 bits) but that did not work. However 1.79e308 is fine, so max float is somewhere close to 1.80e308.
 
=={{header|ERRE}}==
Every type has its "infinity" constant: MAXINT for 16-bit integer, MAXREAL for single precision
floating and MAXLONGREAL for double precision floating. An infinity test can be achieved with
an EXCEPTION:
<syntaxhighlight lang="erre">
PROGRAM INFINITY
 
EXCEPTION
PRINT("INFINITY")
ESCI%=TRUE
END EXCEPTION
 
BEGIN
ESCI%=FALSE
K=1
WHILE 2^K>0 DO
EXIT IF ESCI%
K+=1
END WHILE
END PROGRAM
</syntaxhighlight>
 
=={{header|Euphoria}}==
 
<langsyntaxhighlight Euphorialang="euphoria">constant infinity = 1E400
 
? infinity -- outputs "inf"</langsyntaxhighlight>
 
=={{header|F_Sharp|F#}}==
<syntaxhighlight lang="fsharp">
printfn "%f" (1.0/0.0)
</syntaxhighlight>
{{out}}
<pre>
Infinity
</pre>
 
=={{header|Factor}}==
<syntaxhighlight lang ="factor">1/0.</langsyntaxhighlight>
 
=={{header|Fantom}}==
Line 294 ⟶ 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>.
 
<langsyntaxhighlight lang="fantom">
class Main
{
Line 300 ⟶ 469:
public static Void main () { echo (getInfinity ()) }
}
</syntaxhighlight>
</lang>
 
=={{header|Forth}}==
<langsyntaxhighlight lang="forth">: inf ( -- f ) 1e 0e f/ ;
inf f. \ implementation specific. GNU Forth will output "inf"
 
Line 309 ⟶ 478:
\ IEEE infinity is the only value for which this will return true
 
: has-inf ( -- ? ) ['] inf catch if false else inf? then ;</langsyntaxhighlight>
 
=={{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.
<langsyntaxhighlight lang="fortran">program to_f_the_ineffable
use, intrinsic :: ieee_arithmetic
integer :: i
Line 330 ⟶ 499:
end if
end do
end program to_f_the_ineffable</langsyntaxhighlight>
 
ISO Fortran 90 or later supports a HUGE intrinsic which returns the largest value supported by the data type of the number given.
<langsyntaxhighlight lang="fortran">real :: x
real :: huge_real = huge(x)</langsyntaxhighlight>
 
=={{header|FreeBASIC}}==
<syntaxhighlight lang="freebasic">' FB 1.05.0 Win64
 
#Include "crt/math.bi"
#Print Typeof(1.5) ' Prints DOUBLE at compile time
 
Dim d As Typeof(1.5) = INFINITY
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}}==
<langsyntaxhighlight lang="gap"># Floating point infinity
inf := FLOAT_INT(1) / FLOAT_INT(0);
 
Line 345 ⟶ 559:
# GAP has also a formal ''infinity'' value
infinity in Cyclotomics;
# true</langsyntaxhighlight>
 
=={{header|Go}}==
<langsyntaxhighlight lang="go">package main
 
import (
Line 366 ⟶ 580:
x = posInf() // test function
fmt.Println(x, math.IsInf(x, 1)) // demonstrate result
}</langsyntaxhighlight>
Output:
<pre>
Line 374 ⟶ 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.
<langsyntaxhighlight lang="groovy">def biggest = { Double.POSITIVE_INFINITY }</langsyntaxhighlight>
 
Test program:
<langsyntaxhighlight lang="groovy">println biggest()
printf ( "0x%xL \n", Double.doubleToLongBits(biggest()) )</langsyntaxhighlight>
 
Output:
Line 390 ⟶ 604:
Nevertheless, the following may come close to the task description:
 
<langsyntaxhighlight lang="haskell">maxRealFloat :: RealFloat a => a -> a
maxRealFloat x = encodeFloat b (e-1) `asTypeOf` x where
b = floatRadix x - 1
Line 397 ⟶ 611:
infinity :: RealFloat a => a
infinity = if isInfinite inf then inf else maxRealFloat 1.0 where
inf = 1/0</langsyntaxhighlight>
 
Test for the two standard floating point types:
 
<langsyntaxhighlight lang="haskell">*Main> infinity :: Float
Infinity
*Main> infinity :: Double
Infinity</langsyntaxhighlight>
 
Or you can simply use division by 0:
<langsyntaxhighlight lang="haskell">Prelude> 1 / 0 :: Float
Infinity
Prelude> 1 / 0 :: Double
Infinity</langsyntaxhighlight>
 
Or use "read" to read the string representation:
<langsyntaxhighlight lang="haskell">Prelude> read "Infinity" :: Float
Infinity
Prelude> read "Infinity" :: Double
Infinity</langsyntaxhighlight>
 
=={{header|Icon}} and {{header|Unicon}}==
Line 427 ⟶ 641:
IDL provides the standard IEEE values for _inf and _NaN in the !Values system structure:
 
<langsyntaxhighlight lang="idl">print, !Values.f_infinity ;; for normal floats or
print, !Values.D_infinity ;; for doubles</langsyntaxhighlight>
 
=={{header|Io}}==
<langsyntaxhighlight lang="io">inf := 1/0</langsyntaxhighlight>
 
or
 
<syntaxhighlight lang ="io">Number constants inf</langsyntaxhighlight>
 
=={{header|IS-BASIC}}==
<syntaxhighlight lang="is-basic">PRINT INF</syntaxhighlight>
Output:
<pre>
9.999999999E62
</pre>
 
=={{header|J}}==
Line 442 ⟶ 663:
 
Example:
<syntaxhighlight lang="j">
<lang j>
_ * 5 NB. multiplying infinity to 5 results in infinity
_
Line 449 ⟶ 670:
5 % 0 NB. dividing 5 by 0 results in infinity
_
</syntaxhighlight>
</lang>
 
=={{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>:
<langsyntaxhighlight lang="java">double infinity = Double.POSITIVE_INFINITY; //defined as 1.0/0.0
Double.isInfinite(infinity); //true</langsyntaxhighlight>
As a function:
<langsyntaxhighlight lang="java">public static double getInf(){
return Double.POSITIVE_INFINITY;
}</langsyntaxhighlight>
The largest possible number in Java (without using the <tt>Big</tt> classes) is also in the <tt>Double</tt> class.
<langsyntaxhighlight lang="java">double biggestNumber = Double.MAX_VALUE;</langsyntaxhighlight>
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 ="javascript">Infinity</langsyntaxhighlight>
as well as constants in the Number class:
<langsyntaxhighlight lang="javascript">Number.POSITIVE_INFINITY
Number.NEGATIVE_INFINITY</langsyntaxhighlight>
 
The global isFinite() function tests for finiteness:
<syntaxhighlight lang ="javascript">isFinite(x)</langsyntaxhighlight>
 
=={{header|Lua}}==
=={{header|Joy}}==
<lang lua>
<syntaxhighlight lang="joy">1 1024 ldexp dup neg stack.</syntaxhighlight>
function infinity()
{{out}}
return 1/0 --lua uses unboxed C floats for all numbers
<pre>[-inf inf]</pre>
end
 
</lang>
=={{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`.
 
The C implementation of jq uses IEEE 754 64-bit floating-point arithmetic, and very large real number literals, e.g. 1e1000, are evaluated as IEEE 754 infinity, so if your version of jq does not include `infinite` as a built-in, you could therefore define it as follows:
 
<syntaxhighlight lang="jq">def infinite: 1e1000;</syntaxhighlight>
 
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}}
<langsyntaxhighlight Klang="k"> / Integer infinities
/ 0I is just 2147483647
/ -0I is just -2147483647
Line 530 ⟶ 767:
/ but
0.0%0.0
0.0</langsyntaxhighlight>
 
=={{header|MathematicaKlingphix}}==
<syntaxhighlight lang="klingphix">1e300 dup mult tostr "inf" equal ["Infinity" print] if
 
" " input</syntaxhighlight>
 
=={{header|Kotlin}}==
<syntaxhighlight lang="scala">fun main(args: Array<String>) {
val p = Double.POSITIVE_INFINITY // +∞
println(p.isInfinite()) // true
println(p.isFinite()) // false
println("${p < 0} ${p > 0}") // false true
 
val n = Double.NEGATIVE_INFINITY // -∞
println(n.isInfinite()) // true
println(n.isFinite()) // false
println("${n < 0} ${n > 0}") // true false
}</syntaxhighlight>
{{out}}
<pre>true
false
false true
true
false
true false</pre>
 
=={{header|Lambdatalk}}==
Lambdatalk is built on Javascript and can inherit lots of its capabilities. For instance:
<syntaxhighlight lang="scheme">
{/ 1 0}
-> Infinity
{/ 1 Infinity}
-> 0
{< {pow 10 100} Infinity}
-> true
{< {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.
<syntaxhighlight lang="lasso">infinity
'<br />'
infinity -> type</syntaxhighlight>
-> inf
 
decimal
 
=={{header|Lingo}}==
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.
<syntaxhighlight lang="lingo">x = (1-power(2, -53)) * power(2, 1023) * 2
put ilk(x), x
-- #float 1.79769313486232e308
 
x = (1-power(2, -53)) * power(2, 1023) * 3
put ilk(x), x, -x
-- #float INF -INF</syntaxhighlight>
 
=={{header|Lua}}==
<syntaxhighlight lang="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 {
Form 66,40
Cls 5
Pen 14
\\ Ensure True/False for Print boolean (else -1/0)
\\ from m2000 console use statement Switches without Set.
\\ use Monitor statement to see all switches.
Set Switches "+SBL"
IF version<9.4 then exit
IF version=9.4 and revision<25 then exit
Function Infinity(positive=True) {
buffer clear inf as byte*8
m=0x7F
if not positive then m+=128
return inf, 7:=m, 6:=0xF0
=eval(inf, 0 as double)
}
K=Infinity(false)
L=Infinity()
Function TestNegativeInfinity(k) {
=str$(k, 1033) = "-1.#INF"
}
Function TestPositiveInfinity(k) {
=str$(k, 1033) = "1.#INF"
}
Function TestInvalid {
=str$(Number, 1033) = "-1.#IND"
}
Pen 11 {Print " True True"}
Print TestNegativeInfinity(K), TestPositiveInfinity(L)
Pen 11 {Print " -1.#INF 1.#INF -1.#INF 1.#INF -1.#INF 1.#INF"}
Print K, L, K*100, L*100, K+K, L+L
M=K/L
Pen 11 {Print " -1.#IND -1.#IND True True" }
Print K/L, L/K, TestInvalid(M), TestInvalid(K/L)
M=K+L
Pen 11 {Print " -1.#IND -1.#IND -1.#IND True True"}
Print M, K+L, L+K, TestInvalid(M), TestInvalid(K+L)
Pen 11 {Print " -1.#INF 1.#INF"}
Print 1+K+2, 1+L+2
Pen 11 {Print " -1.#INF"}
Print K-L
Pen 11 {Print " 1.#INF"}
Print L-K
}
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:
<langsyntaxhighlight Mathematicalang="mathematica">Sum[1/n^2,{n,Infinity}]
1/Infinity
Integrate[Exp[-x^2], {x, -Infinity, Infinity}]
10^100 < Infinity</langsyntaxhighlight>
gives back:
<pre>Pi^2/6
Line 544 ⟶ 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]]:
<langsyntaxhighlight Mathematicalang="mathematica">Integrate[Exp[-x^2]/(x^2 - 1), {x, 0, DirectedInfinity[Exp[I Pi/4]]}]</langsyntaxhighlight>
gives back:
<pre>-((Pi (I+Erfi[1]))/(2 E))</pre>
Line 551 ⟶ 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.
 
<langsyntaxhighlight Matlablang="matlab">a = +Inf;
isinf(a)
</syntaxhighlight>
</lang>
 
Returns:
Line 562 ⟶ 927:
 
=={{header|Maxima}}==
<langsyntaxhighlight lang="maxima">/* Maxima has inf (positive infinity) and minf (negative infinity) */
 
declare(x, real)$
Line 575 ⟶ 940:
1.0/0.0;
/* expt: undefined: 0 to a negative exponent.
-- an error. To debug this try: debugmode(true); */</langsyntaxhighlight>
 
=={{header|Metafont}}==
Line 581 ⟶ 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
 
<langsyntaxhighlight lang="metafont">infinity := 4095.99998;</langsyntaxhighlight>
 
=={{header|MiniScript}}==
MiniScript uses IEEE numerics, so:
 
<syntaxhighlight lang="miniscript">posInfinity = 1/0
print posInfinity</syntaxhighlight>
{{out}}
<pre>INF</pre>
 
=={{header|Modula-2}}==
<langsyntaxhighlight Modulalang="modula-2">MODULE inf;
 
IMPORT InOut;
Line 591 ⟶ 964:
InOut.WriteReal (1.0 / 0.0, 12, 12);
InOut.WriteLn
END inf.</langsyntaxhighlight>
Producing
<langsyntaxhighlight Modulalang="modula-2">jan@Beryllium:~/modula/rosetta$ inf
 
**** RUNTIME ERROR bound check error
Floating point exception</langsyntaxhighlight>
 
=={{header|Modula-3}}==
Line 602 ⟶ 975:
 
If the implementation doesn't support IEEE floats, the program prints arbitrary values (Critical Mass Modula-3 implementation does support IEEE floats).
<langsyntaxhighlight lang="modula3">MODULE Inf EXPORTS Main;
 
IMPORT IO, IEEESpecial;
Line 609 ⟶ 982:
IO.PutReal(IEEESpecial.RealPosInf);
IO.Put("\n");
END Inf.</langsyntaxhighlight>
 
Output:
Line 618 ⟶ 991:
=={{header|Nemerle}}==
Both single and double precision floating point numbers support PositiveInfinity, NegativeInfinity and NaN.
<langsyntaxhighlight Nemerlelang="nemerle">def posinf = double.PositiveInfinity;
def a = IsInfinity(posinf); // a = true
def b = IsNegativeInfinity(posinf); // b = false
def c = IsPositiveInfinity(posinf); // c = true</langsyntaxhighlight>
 
=={{header|Nim}}==
<syntaxhighlight lang="nim">Inf</syntaxhighlight>
is a predefined constant in Nim:
<syntaxhighlight lang="nim">var f = Inf
echo f</syntaxhighlight>
 
=={{header|NS-HUBASIC}}==
<syntaxhighlight lang="ns-hubasic">10 PRINT 1/0</syntaxhighlight>
{{out}}
?DZ ERROR is a division by zero error in NS-HUBASIC.
<pre>
?DZ ERROR IN 10
</pre>
 
=={{header|OCaml}}==
<syntaxhighlight lang ="ocaml">infinity</langsyntaxhighlight>
is already a pre-defined value in OCaml.
 
Line 633 ⟶ 1,020:
- : float = infinity
</pre>
 
=={{header|Oforth}}==
 
<syntaxhighlight lang="oforth">10 1000.0 powf dup println dup neg println 1 swap / println</syntaxhighlight>
 
{{out}}
<pre>
1.#INF
-1.#INF
0
</pre>
 
=={{header|Ol}}==
 
Inexact numbers support can be disabled during recompilation using "-DOLVM_INEXACTS=0" command line argument. Inexact numbers in Ol demands the existence of infinity, by demanding IEEE numbers. There are two signed infinity numbers (as constants) in Ol:
+inf.0 ; positive infinity
-inf.0 ; negative infinity
 
<syntaxhighlight lang="scheme">
(define (infinite? x) (or (equal? x +inf.0) (equal? x -inf.0)))
 
(infinite? +inf.0) ==> #true
(infinite? -inf.0) ==> #true
(infinite? +nan.0) ==> #false
(infinite? 123456) ==> #false
(infinite? 1/3456) ==> #false
(infinite? 17+28i) ==> #false
</syntaxhighlight>
 
=={{header|OpenEdge/Progress}}==
Line 638 ⟶ 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.
 
<langsyntaxhighlight lang="progress">MESSAGE
1.0 / 0.0 SKIP
-1.0 / 0.0 SKIP(1)
( 1.0 / 0.0 ) = ( -1.0 / 0.0 )
VIEW-AS ALERT-BOX.</langsyntaxhighlight>
 
Output
Line 659 ⟶ 1,074:
=={{header|OxygenBasic}}==
Using double precision floats:
<langsyntaxhighlight lang="oxygenbasic">
print 1.5e-400 '0
 
Line 680 ⟶ 1,095:
 
print f '#-INF
</syntaxhighlight>
</lang>
 
=={{header|Oz}}==
<langsyntaxhighlight lang="oz">declare
PosInf = 1./0.
NegInf = ~1./0.
Line 695 ⟶ 1,110:
PosInf * PosInf = PosInf
PosInf * NegInf = NegInf
NegInf * NegInf = PosInf</langsyntaxhighlight>
 
=={{header|PARI/GP}}==
{{works with|PARI/GP|version 2.8.0 and higher}}
<lang parigp>infty()={
<syntaxhighlight lang="parigp">+oo</syntaxhighlight>
 
{{works with|PARI/GP|version 2.2.9 to 2.7.0}}
<syntaxhighlight lang="parigp">infty()={
[1] \\ Used for many functions like intnum
};</langsyntaxhighlight>
 
=={{header|Pascal}}==
Line 707 ⟶ 1,126:
=={{header|Perl}}==
Positive infinity:
<langsyntaxhighlight lang="perl">my $x = 0 + "inf";
my $y = 0 + "+inf";</langsyntaxhighlight>
Negative infinity:
<langsyntaxhighlight lang="perl">my $x = 0 - "inf";
my $y = 0 + "-inf";</langsyntaxhighlight>
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:
<langsyntaxhighlight lang="perl">my $x = "inf";</langsyntaxhighlight>
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:
<langsyntaxhighlight lang="perl">my $x = 1e1000;</langsyntaxhighlight>
which is 10<sup>1000</sup> or googol<sup>10</sup> or even numbers like this one:
<langsyntaxhighlight lang="perl">my $y = 10**10**10;</langsyntaxhighlight>
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:
 
<langsyntaxhighlight lang="perl">use bigint;
my $x = 1e1000;
my $y = 10**10**10; # N.B. this will consume vast quantities of RAM</langsyntaxhighlight>
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:
<langsyntaxhighlight lang="perl">use bigint;
my $x = inf;
my $y = -inf;</langsyntaxhighlight>
 
=={{header|Perl 6Phix}}==
<!--<syntaxhighlight lang="phix">(phixonline)-->
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.
<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>
<!--</syntaxhighlight>-->
{{out}}
desktop/Phix:
<pre>
inf
</pre>
pwa/p2js:
<pre>
Infinity
</pre>
 
=={{header|Phixmonti}}==
<syntaxhighlight lang="phixmonti">1e300 dup * tostr "inf" == if "Infinity" print endif</syntaxhighlight>
 
=={{header|PHP}}==
This is how you get infinity:
<syntaxhighlight lang ="php">INF</langsyntaxhighlight>
Unfortunately, "1.0 / 0.0" doesn't evaluate to infinity; but instead seems to evaluate to False, which is more like 0 than infinity.
 
PHP has functions is_finite() and is_infinite() to test for infiniteness.
 
=={{header|PL/I}}==
<lang PL/I>
declare x float, y float (15), z float (18);
 
put skip list (huge(x), huge(y), huge(z));
</lang>
 
=={{header|PicoLisp}}==
Line 755 ⟶ 1,183:
support (scaled bignum arithmetics), but some functions return 'T' for infinite
results.
<langsyntaxhighlight PicoLisplang="picolisp">(load "@lib/math.l")
 
: (exp 1000.0)
-> T</langsyntaxhighlight>
 
=={{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}}==
<langsyntaxhighlight lang="postscript">/infinity { 9 99 exp } def</langsyntaxhighlight>
 
=={{header|PowerShell}}==
Since PowerShell is built onA .NET, floating-point numbersnumber arerepresenting alwaysinfinity capableis of representing infinityavailable.
<langsyntaxhighlight lang="powershell">function infinity {
[double]::PositiveInfinity
}</langsyntaxhighlight>
 
=={{header|PureBasic}}==
 
PureBasic uses [[wp:IEEE_754-2008|IEEE 754]] coding for float types,. result 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 aseither positive or negative infinity.
<lang PureBasic>Define.d a,b
 
<syntaxhighlight lang="purebasic">If OpenConsole()
b=0
Define.d a, b
a=1.0
b = 0
Debug a/b
 
;positive infinity
a=-1.0
PrintN(StrD(Infinity())) ;returns the value for positive infinity from builtin function
Debug a/b</lang>
a = 1.0
PrintN(StrD(a / b)) ;calculation results in the value of positive infinity
;negative infinity
PrintN(StrD(-Infinity())) ;returns the value for negative infinity from builtin function
a = -1.0
PrintN(StrD(a / b)) ;calculation results in the value of negative infinity
Print(#crlf$ + #crlf$ + "Press ENTER to exit"): Input()
CloseConsole()
EndIf
</syntaxhighlight>
 
''Outputs''
+Infinity
+Infinity
-Infinity
-Infinity
 
=={{header|Python}}==
This is how you get infinity:
<langsyntaxhighlight lang="python">>>> float('infinity')
inf</langsyntaxhighlight>
''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:
<langsyntaxhighlight lang="python">>>> 1.0 / 0.0
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ZeroDivisionError: float division</langsyntaxhighlight>
 
If <tt>float('infinity')</tt> doesn't work on your platform, you could use this trick:
Line 803 ⟶ 1,252:
1.#INF</pre>
It works by trying to create a float bigger than the machine can handle.
 
=={{header|QB64}}==
<syntaxhighlight lang="c++">#include<math.h>
//save as inf.h
double inf(void){
return HUGE_VAL;
}</syntaxhighlight>
<syntaxhighlight lang="vb">Declare CustomType Library "inf"
Function inf#
End Declare
 
Print inf</syntaxhighlight>
 
=={{header|QBasic}}==
{{works with|QBasic|1.1}}
<syntaxhighlight lang="qbasic">DECLARE FUNCTION f! ()
 
ON ERROR GOTO TratoError
PRINT 0!
PRINT 0 / -1.5
PRINT 1.5 / 0
PRINT 0 / 0
PRINT f
END
 
TratoError:
PRINT "Error "; ERR; " on line "; ERL; CHR$(9); " --> ";
SELECT CASE ERR
CASE 6
PRINT "Overflow"
RESUME NEXT
CASE 11
PRINT "Division by zero"
RESUME NEXT
CASE ELSE
PRINT "Unexpected error, ending program."
END
END SELECT
 
FUNCTION f!
f! = -1.5 / 0
END FUNCTION</syntaxhighlight>
 
 
=={{header|R}}==
<langsyntaxhighlight Rlang="r"> Inf #positive infinity
-Inf #negative infinity
.Machine$double.xmax # largest finite floating-point number
Line 815 ⟶ 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</langsyntaxhighlight>
 
=={{header|RLaBRacket}}==
<lang RLaB>
>> x = inf()
inf
>> isinf(x)
1
>> inf() > 10
1
>> -inf() > 10
0
</lang>
 
as in Scheme:
=={{header|REXX}}==
The language specifications for REXX are rather open-ended when it comes
to language limits.
<br><br>
Limits on numbers are expressed as: The REXX interpreter has to at '''least''' handle
exponents up to nine (decimal) digits.
<br><br>
So it's up to the writers of the REXX interpreter to decide what limits
are to be implemented or enforced.
 
<syntaxhighlight lang="racket">#lang racket
<pre style="height:35ex;overflow:scroll">
 
+inf.0 ; positive infinity
(define (finite? x) (< -inf.0 x +inf.0))
(define (infinite? x) (not (finite? x)))</syntaxhighlight>
 
=={{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" line>my $x = 1.5/0; # Failure: catchable error, if evaluated will return: "Attempt to divide by zero ...
my $y = (1.5/0).Num; # assigns 'Inf'</syntaxhighlight>
 
=={{header|REXX}}==
The language specifications for REXX are rather open-ended when it comes to language limits.
<br><br>Limits on numbers are expressed as: The REXX interpreter has to at '''least''' handle exponents up to nine (decimal) digits.
<br><br>So it's up to the writers of the REXX interpreter to decide what limits are to be implemented or enforced.
<pre style="overflow:scroll">
For the default setting of
 
Line 848 ⟶ 1,338:
.999999999e+999999999
</pre>
<pre style="height:50ex;overflow:scroll">
For a setting of
 
Line 869 ⟶ 1,359:
... and so on with larger NUMERIC DIGITS
</pre>
For the most REXX interpreters, the maximum number of digits is only limited by virtual storage,
<br>but the pratical limit would be a little less than half of available virtual storage,
<br>which would (realistically) be around one billion digits. Other interpreters have a limitation of roughly 8 million digits.<br><br>
<br><br>
 
=={{header|RLaB}}==
<syntaxhighlight lang="rlab">
>> x = inf()
inf
>> isinf(x)
1
>> inf() > 10
1
>> -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
<langsyntaxhighlight lang="ruby">a = 1.0/0 # => Infinity
a.finite? # => false
a.infinite? # => 1
Line 885 ⟶ 1,395:
a = Float::MAX # => 1.79769313486232e+308
a.finite? # => true
a.infinite? # => nil</langsyntaxhighlight>
{{works with|Ruby|1.9.2+}}
<langsyntaxhighlight lang="ruby">a = Float::INFINITY # => Infinity</langsyntaxhighlight>
 
=={{header|Rust}}==
Rust has builtin function for floating types which returns infinity. This program outputs 'inf'.
<syntaxhighlight lang="rust">fn main() {
let inf = f32::INFINITY;
println!("{}", inf);
}</syntaxhighlight>
 
=={{header|Scala}}==
{{libheader|Scala}}
<lang scala>scala> 1 / 0.
 
'''See also'''
* [[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>:
<syntaxhighlight lang="scala">val inf = Double.PositiveInfinity //defined as 1.0/0.0
inf.isInfinite; //true</syntaxhighlight>
The largest possible number in Scala (without using the <tt>Big</tt> classes) is also in the <tt>Double</tt> class.
<syntaxhighlight lang="scala">val biggestNumber = Double.MaxValue</syntaxhighlight>
 
REPL session:
<syntaxhighlight lang="scala">scala> 1 / 0.
res2: Double = Infinity
 
Line 900 ⟶ 1,428:
 
scala> 1 / Double.NegativeInfinity
res5: Double = -0.0</langsyntaxhighlight>
 
=={{header|Scheme}}==
<langsyntaxhighlight lang="scheme">+inf.0 ; positive infinity
(define (finite? x) (< -inf.0 x +inf.0))
(define (infinite? x) (not (finite? x)))</langsyntaxhighlight>
 
=={{header|Seed7}}==
Line 911 ⟶ 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:
<langsyntaxhighlight lang="seed7">const float: Infinity is 1.0 / 0.0;</langsyntaxhighlight>
Checks for infinity can be done by comparing with this constant.
 
=={{header|Sidef}}==
<syntaxhighlight lang="ruby">var a = 1.5/0 # Inf
say a.is_inf # true
say a.is_pos # true
 
var b = -1.5/0 # -Inf
say b.is_ninf # true
say b.is_neg # true
 
var inf = Inf
var ninf = -Inf
say (inf == -ninf) # true</syntaxhighlight>
 
=={{header|Slate}}==
 
<syntaxhighlight lang ="slate">PositiveInfinity</langsyntaxhighlight>
 
=={{header|Smalltalk}}==
Line 927 ⟶ 1,468:
Inf
</pre>
{{works with|Smalltalk/X}}
The behavior is slightly different, in that an exception is raised if you divide by zero:
<syntaxhighlight lang="smalltalk">FloatD infinity -> INF
1.0 / 0.0 -> "ZeroDivide exception"</syntaxhighlight>
but we can simulate the other behavior with:
<syntaxhighlight lang="smalltalk">[
1.0 / 0.0
] on: ZeroDivide do:[:ex |
ex proceedWith: (FloatD infinity)
]
-> INF</syntaxhighlight>
 
=={{header|Standard ML}}==
<syntaxhighlight lang ="sml">Real.posInf</langsyntaxhighlight>
 
<pre>
Line 937 ⟶ 1,489:
val it = inf : real
</pre>
 
=={{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:
<syntaxhighlight lang="swift">let inf = Double.infinity
inf.isInfinite //true</syntaxhighlight>
As a function:
<syntaxhighlight lang="swift">func getInf() -> Double {
return Double.infinity
}</syntaxhighlight>
 
=={{header|Tcl}}==
Line 942 ⟶ 1,503:
 
Tcl 8.5 has Infinite as a floating point value, not an integer value
<langsyntaxhighlight lang="tcl">package require Tcl 8.5
 
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</langsyntaxhighlight>
 
A maximal integer is not easy to find, as Tcl switches to unbounded integers when a 64-bit integer is about to roll over:
<langsyntaxhighlight Tcllang="tcl">% format %lx -1 ;# all bits set
ffffffffffffffff
 
Line 960 ⟶ 1,521:
 
% incr ii
9223372036854775808 ;# silently upgrade to unbounded integer, still positive</langsyntaxhighlight>
A theoretical MAXINT, though very impractical, could be
string repeat 9 [expr 2**32-1]
Line 966 ⟶ 1,527:
=={{header|TI-89 BASIC}}==
 
<syntaxhighlight lang ="ti89b">∞</langsyntaxhighlight>
 
=={{header|TorqueScript}}==
<langsyntaxhighlight TorqueScriptlang="torquescript">function infinity()
{
return 1/0;
}</langsyntaxhighlight>
 
=={{header|Trith}}==
The following functions are included as part of the core operators:
<langsyntaxhighlight lang="trith">
: inf 1.0 0.0 / ;
: -inf inf neg ;
: inf? abs inf = ;
</syntaxhighlight>
</lang>
 
=={{header|Ursa}}==
Infinity is a defined value in Ursa.
<syntaxhighlight lang="ursa">decl double d
set d Infinity</syntaxhighlight>
 
=={{header|Ursala}}==
Line 989 ⟶ 1,555:
 
 
<langsyntaxhighlight Ursalalang="ursala">#import flo
 
infinity = inf!</langsyntaxhighlight>
 
=={{header|Visual Basic}}==
{{works with|Visual Basic|5}}
{{works with|Visual Basic|6}}
{{works with|VBA|Access 97}}
<!-- Does this NOT work with VB < 6 ? -- Erik Siers, 1 Mar '11 -->
{{works with|VBA|6.5}}
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>:
{{works with|VBA|7.1}}
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>:
 
<syntaxhighlight lang="vb">Option Explicit
<lang vb>Dim PlusInfinity as Double
Dim MinusInfinity as Double
Dim IndefiniteNumber as Double
On Error Resume Next
PlusInfinity = 1 / 0
MinusInfinity = -1 / 0
IndefiniteNumber = 0 / 0</lang>
 
Private Declare Sub GetMem8 Lib "msvbvm60.dll" _
The results of the above are stored internally as:
(ByVal SrcAddr As Long, ByVal TarAddr As Long)
1.#INF
-1.#INF
-1.#IND
 
Sub Main()
Dim PlusInfinity As Double
Dim MinusInfinity As Double
Dim IndefiniteNumber As Double
On Error Resume Next
PlusInfinity = 1 / 0
MinusInfinity = -1 / 0
IndefiniteNumber = 0 / 0
Debug.Print "PlusInfinity = " & CStr(PlusInfinity) _
& " (" & DoubleAsHex(PlusInfinity) & ")"
Debug.Print "MinusInfinity = " & CStr(MinusInfinity) _
& " (" & DoubleAsHex(MinusInfinity) & ")"
Debug.Print "IndefiniteNumber = " & CStr(IndefiniteNumber) _
& " (" & DoubleAsHex(IndefiniteNumber) & ")"
End Sub
 
Function DoubleAsHex(ByVal d As Double) As String
Dim l(0 To 1) As Long
GetMem8 VarPtr(d), VarPtr(l(0))
DoubleAsHex = Right$(String$(8, "0") & Hex$(l(1)), 8) _
& Right$(String$(8, "0") & Hex$(l(0)), 8)
End Function</syntaxhighlight>
{{out}}<pre>PlusInfinity = 1,#INF (7FF0000000000000)
MinusInfinity = -1,#INF (FFF0000000000000)
IndefiniteNumber = -1,#IND (FFF8000000000000)
</pre>
 
=={{header|V (Vlang)}}==
<syntaxhighlight lang="v (vlang)">import math
fn main() {
mut x := 1.5 // type of x determined by literal
// that this compiles demonstrates that PosInf returns same type as x,
// the type specified by the task.
x = math.inf(1)
println('$x ${math.is_inf(x, 1)}') // demonstrate result
}</syntaxhighlight>
 
=={{header|Wren}}==
Wren certainly supports infinity for floating point numbers as we already have a method ''Num.isInfinity'' to test for it.
<syntaxhighlight lang="wren">var x = 1.5
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)")</syntaxhighlight>
 
{{out}}
<pre>
x = 1.5
y = infinity
'x' is infinite? false
'y' is infinite? true
</pre>
 
=={{header|XPL0}}==
The IEEE 754 floating point standard is used.
<syntaxhighlight lang="xpl0">int A;
real X;
[Format(0, 15); \output in scientific notation
A:= addr X; \get address of (pointer to) X
A(0):= $FFFF_FFFF; \stuff in largest possible value
A(1):= $7FEF_FFFF;
RlOut(0, X); \display it
]</syntaxhighlight>
 
{{out}}
<pre>
1.797693134862320E+308
</pre>
 
=={{header|Yabasic}}==
<syntaxhighlight lang="yabasic">infinity = 1e300*1e300
if str$(infinity) = "inf" print "Infinity"</syntaxhighlight>
 
=={{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.isInf(infinite_value));
try expect(math.isPositiveInf(infinite_value));
try expect(!math.isNegativeInf(infinite_value));
try expect(!math.isFinite(infinite_value));
}
}</syntaxhighlight>
 
{{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="zkl">1.5/0</syntaxhighlight>
{{out}}
<pre>
Exception thrown: MathError(INF (number is infinite))
</pre>
 
=={{header|ZX Spectrum Basic}}==
ZX Spectrum BASIC has no infinity handling; <syntaxhighlight lang="zxbasic">PRINT 1/0</syntaxhighlight> will be met with <pre>6 Number too big, 0:1</pre>
A quick doubling loop will get you halfway to the maximum floating point value:
<syntaxhighlight lang="zxbasic">10 LET z=1
20 PRINT z
30 LET z=z*2
40 GO TO 20</syntaxhighlight>
 
Output will end with:
<pre>
4.2535296E+37
8.5070592E+37
 
6 Number too big, 30:1
</pre>
Precision has been lost by this stage through the loop, but one more manual double and subtract 1 will get you the true displayable maximum of 1.7014118E+38 (or 2^127-1).
 
{{omit from|6502 Assembly|Has no dedicated floating point hardware}}
{{omit from|8080 Assembly|Has no dedicated floating point hardware}}
{{omit from|bc|No infinity. Numbers have unlimited precision, so no largest possible value.}}
{{omit from|Brainf***}}
{{omit from|Computer/zero Assembly|Has no dedicated floating point hardware}}
{{omit from|dc|No infinity. Numbers have unlimited precision, so no largest possible value.}}
{{omit from|Integer BASIC}}
Line 1,019 ⟶ 1,713:
{{omit from|VBScript}}
{{omit from|UNIX Shell}}
{{omit from|Z80 Assembly|Has no dedicated floating point hardware}}
 
[[Category:Irrational numbers]]
2,120

edits