Wolfram Language Fast Introduction for Programmers
Get Started »

Numbers

The Wolfram Language by default does exact computation whenever it can:

In[1]:=
X
Out[1]=

Rational numbers are represented symbolically as reduced fractions in the Wolfram Language. Java does not have a built-in rational type, instead always returning numerical approximations by default.

Python also treats / as the division operator, but returns a numerical output. To compute with fractions in Python, you typically import the fractions package.

Use N to get (potentially faster) numerical results:

In[2]:=
X
Out[2]=

The Wolfram Language can handle numbers of any precision:

In[1]:=
X
Out[1]=

The language automatically tracks precision of results.

Use ` to explicitly indicate the precision to assume in a number:

In[2]:=
X
Out[2]=

Java code must explicitly use the BigInteger and BigDecimal types for arbitrary-precision integers and decimals.

While integers in Python have arbitrary precision, floats do not without relying on a library such as mpmath.

I represents for complex numbers:

In[1]:=
X
Out[1]=

Java has no internal representation for the imaginary number i. To compute with complex numbers in Java, you must import or create a package.

Notice that the Wolfram Language uses i to represent the imaginary number, while Python syntax uses j.

Matrices are lists of lists:

In[1]:=
X
Out[1]=

SparseArray gives sparse arrays.

Sparse arrays are not provided by default in Java, typically requiring constructs from third-party libraries.

Sparse arrays are not provided by default in Python, typically requiring constructs from third-party libraries and packages such as NumPy.

Check Your Understanding

In the Wolfram Language, what do you get when you enter 14/12?

7/6

Correct.

1.16667

Incorrect. Exact fractions are not turned into decimals.

1.16666

Incorrect. Fractions are not turned into decimals.

Using the Wolfram Language, how do you turn 1/2 into .5?

1/2

Incorrect. This will just stay the same.

Divide[1, 2]

Incorrect. This is still going to be 1/2.

N[1/2]

Correct. The function N will convert fractions into real numbers.

Which of these computes pi to 100 digits of precision?

N[Pi, 100]

Correct.

3.14`100

Incorrect. This only agrees with pi for three digits.

Pi*1.`50

Incorrect. This only has 50 digits.