[proxy] web.archive.org← back | site home | direct (HTTPS) ↗ | proxy home | ◑ dark◐ light

Numbers—Wolfram Language Documentation

Types of NumbersArbitraryPrecision Calculations
Complex NumbersArbitraryPrecision Numbers
Numeric QuantitiesMachinePrecision Numbers
Digits in NumbersInterval Arithmetic
Exact and Approximate ResultsIndeterminate and Infinite Results
Numerical PrecisionControlling Numerical Evaluation
Integer
arbitrarylength exact integer
Rational
integer/integer in lowest terms
Real
approximate real number, with any specified precision
Complex
complex number of the form number+number I
123
an exact integer
123.
an approximate real number
123.0000000000000
an approximate real number with a certain precision
123.+0.I
a complex number with approximate real number components
The object 123 is taken to be an exact integer, with head Integer:
The presence of an explicit decimal point makes the Wolfram System treat 123. as an approximate real number, with head Real:
NumberQ[x]
test whether x is any kind of number
IntegerQ[x]
test whether x is an integer
EvenQ[x]
test whether x is even
OddQ[x]
test whether x is odd
PrimeQ[x]
test whether x is a prime integer
Head[x]===type
test the type of a number
NumberQ[x] tests for any kind of number:
If you use complex numbers extensively, there is one subtlety you should be aware of. When you enter a number like 123., the Wolfram System treats it as an approximate real number, but assumes that its imaginary part is exactly zero. Sometimes you may want to enter approximate complex numbers with imaginary parts that are zero, but only to a certain precision.
When the imaginary part is the exact integer 0, the Wolfram System simplifies complex numbers to real ones:
One way to find out the type of a number in Wolfram System is just to pick out its head using Head[expr]. For many purposes, however, it is better to use functions like IntegerQ that explicitly test for particular types. Functions like this are set up to return True if their argument is manifestly of the required type, and to return False otherwise. As a result, IntegerQ[x] will give False unless x has an explicit integer value.
You can enter complex numbers in the Wolfram Language just by including the constant I, equal to . Make sure that you type a capital I.
This gives the imaginary number result :
x+I y
the complex number
Re[z]
real part
Im[z]
imaginary part
Conjugate[z]
complex conjugate or
Abs[z]
absolute value
Arg[z]
the argument in
NumberQ[expr]
test whether expr is explicitly a number
NumericQ[expr]
test whether expr has a numerical value
Pi is a symbol, so Pi+3 is not explicitly a number:
This finds the explicit numerical value of Pi+3:
The Wolfram Language knows that constants such as Pi are numeric quantities. It also knows that standard mathematical functions such as Log and Sin have numerical values when their arguments are numerical.
Log[2+x] contains x, and is therefore not a numeric quantity:
In general, the Wolfram Language assumes that any function which has the attribute NumericFunction will yield numerical values when its arguments are numerical. All standard mathematical functions in the Wolfram Language already have this attribute. But when you define your own functions, you can explicitly set the attribute to tell the Wolfram Language to assume that these functions will have numerical values when their arguments are numerical.
IntegerDigits[n]
a list of the decimal digits in the integer n
IntegerDigits[n,b]
the digits of n in base b
IntegerDigits[n,b,len]
the list of digits padded on the left with zeros to give total length len
IntegerLength[n]
the number of decimal digits in n
IntegerLength[n,b]
the number of base b digits in n
IntegerExponent[n,b]
the number of zeros at the end of n in base b
RealDigits[x]
a list of the decimal digits in the approximate real number x, together with the number of digits to the left of the decimal point
RealDigits[x,b]
the digits of x in base b
RealDigits[x,b,len]
the first len digits of x in base b
RealDigits[x,b,len,n]
the first len digits starting with the coefficient of bn
FromDigits[list]
construct a number from its decimal digit sequence
FromDigits[list,b]
construct a number from its digit sequence in base b
FromDigits["string"]
construct an integer from a string of digits
FromDigits["string",b]
construct an integer from a string of digits in base b
IntegerString[n]
a string of the decimal digits in the integer n
IntegerString[n,b]
a string of the digits of n in base b
b^^nnnn
a number in base b
BaseForm[x,b]
print with x in base b
IntegerString[n,b]
a string representing n in base b
When the base is larger than 10, extra digits are represented by letters az.
Here are the first few digits of in octal:
This gives 15 octal digits starting with the coefficient of :
"Output Formats for Numbers" describes how to print numbers in various formats. If you want to create your own formats, you will often need to use MantissaExponent to separate the pieces of real numbers.
MantissaExponent[x]
give a list containing the mantissa and exponent of x
MantissaExponent[x,b]
give the mantissa and exponent in base b
The Wolfram Language gives an exact result for , even though it has 31 decimal digits:
You can tell the Wolfram Language to give you an approximate numerical result, just as a calculator would, by ending your input with //N. The N stands for "numerical". It must be a capital letter. "Special Ways to Input Expressions" will explain what the // means.
//N always gives the approximate numerical result:
expr//N
give an approximate numerical value for expr
When you type in an integer like 7, the Wolfram Language assumes that it is exact. If you type in a number like 4.5, with an explicit decimal point, the Wolfram Language assumes that it is accurate only to a fixed number of decimal places.
Precision[x]
the total number of significant decimal digits in x
Accuracy[x]
the number of significant decimal digits to the right of the decimal point in x
This generates a number with 30digit precision:
Precision[x]
Accuracy[x]
N[expr,n]
evaluate expr to ndigit precision using arbitraryprecision numbers
N[expr]
evaluate expr numerically using machineprecision numbers
Numerical evaluation with arbitraryprecision and machineprecision numbers.
The Wolfram Language distinguishes two kinds of approximate real numbers: arbitraryprecision numbers, and machineprecision numbers or machine numbers. Arbitraryprecision numbers can contain any number of digits, and maintain information on their precision. Machine numbers, on the other hand, always contain the same number of digits, and maintain no information on their precision.
Here is a machinenumber approximation to :
These are both arbitraryprecision numbers:
As discussed in more detail below, machine numbers work by making direct use of the numerical capabilities of your underlying computer system. As a result, computations with them can often be done more quickly. They are however much less flexible than arbitraryprecision numbers, and difficult numerical analysis can be needed to determine whether results obtained with them are correct.
MachinePrecision
the precision specification used to indicate machine numbers
$MachinePrecision
the effective precision for machine numbers on your computer system
MachineNumberQ[x]
test whether x is a machine number
When you enter an approximate real number, the Wolfram Language has to decide whether to treat it as a machine number or an arbitraryprecision number. Unless you specify otherwise, if you give less than $MachinePrecision digits, the Wolfram Language will treat the number as machine precision, and if you give more digits, it will treat the number as arbitrary precision.
123.4
a machineprecision number
123.45678901234567890
an arbitraryprecision number on some computer systems
123.45678901234567890`
a machineprecision number on all computer systems
123.456`200
an arbitraryprecision number with 200 digits of precision
123.456``200
an arbitraryprecision number with 200 digits of accuracy
1.234*^6
a machineprecision number in scientific notation (1.234×106)
1.234`200*^6
a number in scientific notation with 200 digits of precision
2^^101.111`200
a number in base 2 with 200 binary digits of precision
2^^101.111`200*^6
a number in base 2 scientific notation (101.1112×26)
Here is an arbitraryprecision number in standard output form:
InputForm[expr,NumberMarks->True]
use ` marks in all approximate numbers
InputForm[expr,NumberMarks->Automatic]
use ` only in arbitraryprecision numbers
InputForm[expr,NumberMarks->False]
never use ` marks
The default setting for the NumberMarks option, both in InputForm and in functions such as ToString and OpenWrite is given by the value of $NumberMarks. By resetting $NumberMarks, therefore, you can globally change the way that numbers are printed in InputForm.
Even a machineprecision number is now printed with an explicit number mark:
Even with no number marks, InputForm still uses *^ for scientific notation:
In doing numerical computations, it is inevitable that you will sometimes end up with results that are less precise than you want. Particularly when you get numerical results that are very close to zero, you may well want to assume that the results should be exactly zero. The function Chop allows you to replace approximate real numbers that are close to zero by the exact integer 0.
Chop[expr]
replace all approximate real numbers in expr with magnitude less than 10-10 by 0
Chop[expr,dx]
replace numbers with magnitude less than dx by 0
You can get rid of the imaginary part using Chop:
ArbitraryPrecision Calculations
When you use //N to get a numerical result, the Wolfram Language does what a standard calculator would do: it gives you a result to a fixed number of significant figures. You can also tell the Wolfram Language exactly how many significant figures to keep in a particular calculation. This allows you to get numerical results in the Wolfram Language to any degree of precision.
expr//N or N[expr]
approximate numerical value of expr
N[expr,n]
numerical value of expr calculated with ndigit precision
This gives the numerical value of to a fixed number of significant digits. Typing N[Pi] is exactly equivalent to Pi//N:
This gives to 40 digits:
Here is to 30 digits:
The quantity turns out to be very close to an integer. To check that the result is not, in fact, an integer, you have to use sufficient numerical precision:
ArbitraryPrecision Numbers
When you do calculations with arbitraryprecision numbers, the Wolfram Language keeps track of precision at all points. In general, the Wolfram Language tries to give you results which have the highest possible precision, given the precision of the input you provided.
The Wolfram Language treats arbitraryprecision numbers as representing the values of quantities where a certain number of digits are known, and the rest are unknown. In general, an arbitraryprecision number x is taken to have Precision[x] digits which are known exactly, followed by an infinite number of digits which are completely unknown.
This computes to 10digit precision:
This evaluates to 30digit precision:
If you give input only to a few digits of precision, the Wolfram Language cannot give you such highprecision output:
If you want the Wolfram Language to assume that the argument is exactly 142/1000, then you have to say so explicitly:
In many computations, the precision of the results you get progressively degrades as a result of "roundoff error". A typical case of this occurs if you subtract two numbers that are close together. The result you get depends on highorder digits in each number, and typically has far fewer digits of precision than either of the original numbers.
Here is evaluated to 20digit precision:
The result you get by adding the exact integer 1 has a higher precision:
Here is a 40digit number that is close to 1:
Adding 1 to it gives another 40digit number:
This way of computing 1+x loses precision:
The internal algorithms that the Wolfram Language uses to evaluate mathematical functions are set up to maintain as much precision as possible. In most cases, builtin Wolfram Language functions will give you results that have as much precision as can be justified on the basis of your input. In some cases, however, it is simply impractical to do this, and the Wolfram Language will give you results that have lower precision. If you give higherprecision input, the Wolfram Language will use higher precision in its internal calculations, and you will usually be able to get a higherprecision result.
N[expr]
evaluate expr numerically to machine precision
N[expr,n]
evaluate expr numerically trying to get a result with n digits of precision
If you start with an expression that contains only integers and other exact numeric quantities, then N[expr,n] will in almost all cases succeed in giving you a result to n digits of precision. You should realize, however, that to do this the Wolfram Language sometimes has to perform internal intermediate calculations to much higher precision.
The global variable $MaxExtraPrecision specifies how many additional digits should be allowed in such intermediate calculations.
variable
default value
$MaxExtraPrecision50
maximum additional precision to use
Using the default setting $MaxExtraPrecision=50, the Wolfram Language cannot get the correct answer here:
Even when you are doing computations that give exact results, the Wolfram Language still occasionally uses approximate numbers for some of its internal calculations, so that the value of $MaxExtraPrecision can thus have an effect.
In doing calculations that degrade precision, it is possible to end up with numbers that have no significant digits at all. But even in such cases, the Wolfram Language still maintains information on the accuracy of the numbers. Given a number with no significant digits, but accuracy a, the Wolfram Language can then still tell that the actual value of the number must be in the range {-10-a,+10-a}/2. The Wolfram Language by default prints such numbers in the form 0.×10e.
Here is a number with 20digit precision:
Adding the result to an exact 1 gives a number with quite high precision:
N[expr,p]
evaluate expr to precision p
N[expr,{p,a}]
evaluate expr to at most precision p and accuracy a
N[expr,{Infinity,a}]
evaluate expr to any precision but to accuracy a
N cannot guarantee to get a result to precision 20:
When the Wolfram Language works out the potential effect of unknown digits in arbitraryprecision numbers, it assumes by default that these digits are completely independent in different numbers. While this assumption will never yield too high a precision in a result, it may lead to unnecessary loss of precision.
Here is a number computed to 20digit precision:
The quantity 1+d has about 34digit precision:
This quantity has lower precision, since the Wolfram Language assumes that the unknown digits in each number d are independent:
SetPrecision[x,n]
create a number with n decimal digits of precision, padding with base2 zeros if necessary
SetAccuracy[x,n]
create a number with n decimal digits of accuracy
This introduces 10 more digits in d:
SetPrecision works by adding digits which are zero in base 2. Sometimes, the Wolfram Language stores slightly more digits in an arbitraryprecision number than it displays, and in such cases, SetPrecision will use these extra digits before introducing zeros.
variable
default value
$MaxPrecisionInfinity
maximum total precision to be used
$MinPrecision0
minimum precision to be used
By making the global assignment $MinPrecision=n, you can effectively apply SetPrecision[expr,n] at every step in a computation. This means that even when the number of correct digits in an arbitraryprecision number drops below n, the number will always be padded to have n digits.
If you set $MaxPrecision=n as well as $MinPrecision=n, then you can force all arbitraryprecision numbers to have a fixed precision of n digits. In effect, what this does is to make the Wolfram Language treat arbitraryprecision numbers in much the same way as it treats machine numbersbut with more digits of precision.
Fixedprecision computation can make some calculations more efficient, but without careful analysis you can never be sure how many digits are correct in the results you get.
Here is a small number with 20digit precision:
With the Wolfram Languages usual arithmetic, this works fine:
This tells the Wolfram Language to use fixedprecision arithmetic:
MachinePrecision Numbers
Whenever machineprecision numbers appear in a calculation, the whole calculation is typically done in machine precision. The Wolfram Language will then give machineprecision numbers as the result.
Whenever the input contains any machineprecision numbers, the Wolfram Language does the computation to machine precision:
Zeta[5.6] yields a machineprecision result, so the N is irrelevant:
This gives a higherprecision result:
When you do calculations with arbitraryprecision numbers, as discussed in "ArbitraryPrecision Numbers", the Wolfram Language always keeps track of the precision of your results, and gives only those digits which are known to be correct, given the precision of your input. When you do calculations with machineprecision numbers, however, the Wolfram Language always gives you a machineprecision result, whether or not all the digits in the result can, in fact, be determined to be correct on the basis of your input.
This subtracts two machineprecision numbers:
The fact that you can get spurious digits in machineprecision numerical calculations with the Wolfram Language is in many respects quite unsatisfactory. The ultimate reason, however, that the Wolfram Language uses fixed precision for these calculations is a matter of computational efficiency.
The Wolfram Language is usually set up to insulate you as much as possible from the details of the computer system you are using. In dealing with machineprecision numbers, you would lose too much, however, if the Wolfram Language did not make use of some specific features of your computer.
The important point is that almost all computers have special hardware or microcode for doing floatingpoint calculations to a particular fixed precision. The Wolfram Language makes use of these features when doing machineprecision numerical calculations.
The typical arrangement is that all machineprecision numbers in the Wolfram Language are represented as "doubleprecision floatingpoint numbers" in the underlying computer system. On most current computers, such numbers contain a total of 64 binary bits, typically yielding 16 decimal digits of mantissa.
The main advantage of using the builtin floatingpoint capabilities of your computer is speed. Arbitraryprecision numerical calculations, which do not make such direct use of these capabilities, are usually many times slower than machineprecision calculations.
There are several disadvantages of using builtin floatingpoint capabilities. One already mentioned is that it forces all numbers to have a fixed precision, independent of what precision can be justified for them.
A second disadvantage is that the treatment of machineprecision numbers can vary slightly from one computer system to another. In working with machineprecision numbers, the Wolfram Language is at the mercy of the floatingpoint arithmetic system of each particular computer. If floatingpoint arithmetic is done differently on two computers, you may get slightly different results for machineprecision Wolfram Language calculations on those computers.
$MachinePrecision
the number of decimal digits of precision
$MachineEpsilon
the minimum positive machineprecision number which can be added to 1.0 to give a result distinguishable from 1.0
$MaxMachineNumber
the maximum machineprecision number
$MinMachineNumber
the minimum positive machineprecision number that can be represented in normalized form
$MaxNumber
the maximum magnitude of an arbitraryprecision number
$MinNumber
the minimum magnitude of a positive arbitraryprecision number
Since machineprecision numbers on any particular computer system are represented by a definite number of binary bits, numbers that are too close together will have the same bit pattern, and so cannot be distinguished. The parameter $MachineEpsilon gives the distance between 1.0 and the closest number that has a distinct binary representation.
This gives the value of $MachineEpsilon for the computer system on which these examples are run:
Although this prints as 1., the Wolfram Language knows that the result is larger than 1:
InputForm reveals that the result is not exactly 1:
This prints as 1. also:
In this case, however, the result is not distinguished from 1. to machine precision:
Machine numbers have not only limited precision, but also limited magnitude. If you generate a number that is larger in magnitude than $MaxMachineNumber, the Wolfram Language will automatically convert the number to arbitraryprecision form.
This is the maximum machineprecision number that can be handled on the computer system used for this example:
The Wolfram Language automatically converts any result larger than $MaxMachineNumber to arbitrary precision:
As of Version 11.3, if a machine-precision computation generates a number that is smaller in magnitude than $MinMachineNumber, the Wolfram Language will issue a warning. However, it will retain the result as a subnormal machine number that has leading zeros in its binary significand.
This is the minimum machineprecision number that can be put into normal form on the computer system used for this example:
The Wolfram Language issues warnings about computations that produce numbers smaller than $MinMachineNumber, but it does not automatically convert to arbitrary-precision arithmetic:
Converting to arbitrary precision using SetPrecision can avoid this underflow:
Interval[{min,max}]
the interval from min to max
Interval[{min1,max1},{min2,max2},]
the union of intervals from min1 to max1, min2 to max2,
Abs folds the intervals back together again:
IntervalUnion[interval1,interval2,]
find the union of several intervals
IntervalIntersection[interval1,interval2,]
find the intersection of several intervals
IntervalMemberQ[interval,x]
test whether the point x lies within an interval
IntervalMemberQ[interval1,interval2]
test whether interval2 lies completely within interval1
You can use Max and Min to find the end points of intervals:
You can use intervals not only with exact quantities but also with approximate numbers. Even with machineprecision numbers, the Wolfram Language always tries to do rounding in such a way as to preserve the validity of results.
This shows explicitly the interval treated by the Wolfram Language as the machineprecision number 0.
This shows the corresponding interval around 100., shifted back to zero:
With ordinary machineprecision arithmetic, this computation gives an incorrect result:
If you type in an expression like 0/0, the Wolfram Language prints a message, and returns the result Indeterminate:
Whenever an indeterminate result is produced in an arithmetic computation, the Wolfram Language prints a warning message, and then returns Indeterminate as the result of the computation. If you ever try to use Indeterminate in an arithmetic computation, you always get the result Indeterminate. A single indeterminate expression effectively "poisons" any arithmetic computation. (The symbol Indeterminate plays a role in the Wolfram Language similar to the "not a number" object in the IEEE Floating Point Standard.)
The usual laws of arithmetic simplification are suspended in the case of Indeterminate:
Indeterminate "poisons" any arithmetic computation, and leads to an indeterminate result:
When you do arithmetic computations inside Wolfram Language programs, it is often important to be able to tell whether indeterminate results were generated in the computations. You can do this by using the function Check discussed in "Messages" to test whether any warning messages associated with indeterminate results were produced.
You can use Check inside a program to test whether warning messages are generated in a computation:
Indeterminate
an indeterminate numerical result
Infinity
a positive infinite quantity
-Infinity
a negative infinite quantity ( DirectedInfinity[-1] )
DirectedInfinity[r]
an infinite quantity with complex direction r
ComplexInfinity
an infinite quantity with an undetermined direction
DirectedInfinity[]
equivalent to ComplexInfinity
There are many situations where it is convenient to be able to do calculations with infinite quantities. The symbol Infinity in the Wolfram Language represents a positive infinite quantity. You can use it to specify such things as limits of sums and integrals. You can also do some arithmetic calculations with it.
The Wolfram Language knows that :
In the Wolfram Language, infinite quantities can have a "direction", specified by a complex number. When you type in the symbol Infinity, representing a positive infinite quantity, this is converted internally to the form DirectedInfinity[1], which represents an infinite quantity in the direction. Similarly, -Infinity becomes DirectedInfinity[-1], and IInfinity becomes DirectedInfinity[I]. Although the DirectedInfinity form is always used internally, the standard output format for DirectedInfinity[r] is r Infinity.
Although the notion of a "directed infinity" is often useful, it is not always available. If you type in 1/0, you get an infinite result, but there is no way to determine the "direction" of the infinity. The Wolfram Language represents the result of 1/0 as DirectedInfinity[]. In standard output form, this undirected infinity is printed out as ComplexInfinity.
1/0 gives an undirected form of infinity:
NHoldAll
prevent any arguments of a function from being affected by N
NHoldFirst
prevent the first argument from being affected
NHoldRest
prevent all but the first argument from being affected
Usually N goes inside functions and gets applied to each of their arguments:
This tells the Wolfram Language not to apply N to the first argument of f:
Now the first argument of f is left in its exact form: