This is documentation for Mathematica 4, which was
based on an earlier version of the Wolfram Language.
View current documentation (Version 11.2)
Wolfram Research, Inc.

Numeric QuantitiesNumerical Precision

3.1.3 Converting between Different Forms of Numbers

Functions that convert between different types of numbers.

This gives a 30-digit real number approximation to 3/7.

In[1]:= N[3/7, 30]


This takes the 30-digit number you have just generated, and reduces it to 20-digit precision.

In[2]:= N[%, 20]


This converts the result back to a rational number.

In[3]:= Rationalize[%]


The numerical value of is not "sufficiently close" to a rational number to be converted.

In[4]:= Rationalize[ N[Pi] ]


If you give a specific tolerance, Rationalize will give you a rational number approximation accurate to within that tolerance.

In[5]:= Rationalize[ N[Pi], 10^-5 ]


With a tolerance of 0, Rationalize yields the best possible rational approximation given the precision of your input.

In[6]:= Rationalize[ N[Pi], 0 ]


Converting between numbers and lists of digits.

Here is the list of base 16 digits for an integer.

In[7]:= IntegerDigits[1234135634, 16]


This gives a list of digits, together with the number of digits that appear to the left of the decimal point.

In[8]:= RealDigits[123.456789012345678901234567]


Here is the binary digit sequence for 56, padded with zeros so that it is of total length 8.

In[9]:= IntegerDigits[56, 2, 8]


This reconstructs the original number from its binary digit sequence.

In[10]:= FromDigits[%, 2]


Numbers in other bases.

When the base is larger than 10, extra digits are represented by letters a-z.

The number in base 2 is in base 10.

In[11]:= 2^^100101


This prints in base 2.

In[12]:= BaseForm[37, 2]


Here is a number in base 16.

In[13]:= 16^^ffffaa00


You can do computations with numbers in base 16. Here the result is given in base 10.

In[14]:= 16^^fffaa2 + 16^^ff - 1


This gives the result in base 16.

In[15]:= BaseForm[%, 16]


You can give approximate real numbers, as well as integers, in other bases.

In[16]:= 2^^101.100101


Here are the first few digits of in octal.

In[17]:= BaseForm[N[Sqrt[2], 30], 8]


This gives an explicit list of the first 15 octal digits.

In[18]:= RealDigits[Sqrt[2], 8, 15]


This gives 15 octal digits starting with the coefficient of .

In[19]:= RealDigits[Sqrt[2], 8, 15, -10]


Section 2.8.7 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.

Separating the mantissa and exponent of numbers.

This gives a list in which the mantissa and exponent of the number are separated.

In[20]:= MantissaExponent[3.45 10^125]


Numeric QuantitiesNumerical Precision