Wolfram Research, Inc.
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:= N[3/7, 30]
This takes the 30-digit number you have just generated, and reduces it to 20-digit precision.
In:= N[%, 20]
This converts the result back to a rational number.
The numerical value of is not "sufficiently close" to a rational number to be converted.
In:= Rationalize[ N[Pi] ]
If you give a specific tolerance, Rationalize will give you a rational number approximation accurate to within that tolerance.
In:= Rationalize[ N[Pi], 10^-5 ]
With a tolerance of 0, Rationalize yields the best possible rational approximation given the precision of your input.
In:= Rationalize[ N[Pi], 0 ]
Converting between numbers and lists of digits.
Here is the list of base 16 digits for an integer.
In:= IntegerDigits[1234135634, 16]
This gives a list of digits, together with the number of digits that appear to the left of the decimal point.
Here is the binary digit sequence for 56, padded with zeros so that it is of total length 8.
In:= IntegerDigits[56, 2, 8]
This reconstructs the original number from its binary digit sequence.
In:= 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.
This prints in base 2.
In:= BaseForm[37, 2]
Here is a number in base 16.
You can do computations with numbers in base 16. Here the result is given in base 10.
In:= 16^^fffaa2 + 16^^ff - 1
This gives the result in base 16.
In:= BaseForm[%, 16]
You can give approximate real numbers, as well as integers, in other bases.
Here are the first few digits of in octal.
In:= BaseForm[N[Sqrt, 30], 8]
This gives an explicit list of the first 15 octal digits.
In:= RealDigits[Sqrt, 8, 15]
This gives 15 octal digits starting with the coefficient of .
In:= RealDigits[Sqrt, 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:= MantissaExponent[3.45 10^125]