Concepts of Conversion of Base 10 Number to Base 2 Floating Point Binary Representation

Sean Rodby, Luke Snyder, Autar Kaw
University of South Florida
United States of America
kaw@eng.usf.edu
Website: http://numericalmethods.eng.usf.edu
Version: Mathematica 6; Last Revised: August 28, 2008

Initialization

Clearing the definitions of all symbols in the current context:

ClearAll[Evaluate[Context[]<>"*"]]

Introduction

The following worksheet illustrates how to convert a base-10 real number to a base-2 floating point binary representation using loops and various conditional statements. The user inputs a base-10 number in any format, the total number of bits, and the number of bits for the mantissa in the Input section of the program. The program will then convert the base-10 number into a floating point binary representation by first converting the given number into a number of the format 1.xxx * (2^m) where m represents the exponent of the number.

Section 1: Input Data

This is the only section where the user interacts with the program.

The generalized formula for floating point format in base-10 is given by y = σ m (10^e)

Enter a number to be converted to a floating point binary number

decnum=5.8946;

Enter the total number of bits to be used (1st bit will be used for the sign of the number, 2nd bit will
be used for the sign of the exponent.)

totbits=9;

Enter number of bits for mantissa (m)

mantbits=4;

This is the end of the user section. All information must be entered before proceeding to the next
section. RE-EVALUATE THE NOTEBOOK.

Section 2: Procedure

The number of bits to be used for the exponent will be the total number of bits minus the number of bits used for the mantissa, minus the bits used for the sign of the decimal number, and minus the bit used for the sign of the exponent.

expbits=totbits-mantbits-1-1

To find the sign of the decimal number (sigma), and the sign of the exponent, we test to see if the number is negative and if it is less than 1.

If[decnum>0,σ="0",σ="1"];
If[Abs[decnum]<1∧Abs[decnum]>0,expsign="1",expsign="0"];

Calculating the maximum possible value given the number of bits as specified by the user.

(*Calculating the maximum value for the exponent.*)
expsum=0;
Do[expsum=expsum+(2^i),{i,0,expbits-1}]

(*Calculating the maximum value for the mantissa.*)
mantsum=1;
Do[mantsum=mantsum+(2^(-i)),{i,1,mantbits}]

maxval=N[mantsum*(2^expsum),10]

minval=N[mantsum*(2^(-expsum)),10]

If the maximum value that can be produced given the user specified number of bits is smaller than the number to be represented, then more bits are needed. Similarly, if the minimum value that can be produced given the user specified number of bits is larger than the number to be represented, a change in bits is needed.

If[maxval<decnum,Print["Since |",decnum,"| > ",maxval ," the number of bits specified is not sufficient to represent this number in floating point representation. Either specify fewer mantissa bits, or more total bits for the worksheet."]]
If[minval>decnum,Print["Since ",minval," > |",decnum,"|, the number of bits specified is not sufficient to represent this number in floating point representation. Either specify fewer mantissa bits, or more total bits for the worksheet."]]
If[minval≤decnum∧maxval≥decnum,Print["Since ",minval," < |",decnum,"| < ",maxval," the base-10 number can be represented in floating point binary format."]]

We will convert the given variable decnum into some number 1.xxx * (2^m) , where m is the value for the exponent. First, we will find this value m for the exponent using the properties of logarithms. Since the exponent value is now known, it is now possible to find the fractional portion of the decimal number (1.xxx). To isolate the fractional portion, we simply subtract one from the mantval variable.

decnum=Abs[decnum];
expval=Floor[((Log[decnum])/(Log[2]))];
mantval=(decnum/(2^expval));
mantfrac=mantval-1;

Using loops to approximate the mantissa up to the specified number of mantissa bits.

Mantstr="";
Do[
newmantfrac=mantfrac*2;
(Subscript[Mant, i])=Floor[newmantfrac];
mantfrac=newmantfrac-(Subscript[Mant, i]);
(Subscript[Mantissa, i])=ToString[(Subscript[Mant, i])];
Mantstr=StringJoin[Mantstr,(Subscript[Mantissa, i]),"|"],
{i,mantbits}]

Approximating the exponent value based on the calculated value of m. Then approximating the floating point values for the exponent value m.

expval=Abs[expval];
Do[
newexpval=Floor[(expval/2)];
(Subscript[Binexp, i])=Ceiling[(expval/2)]-newexpval;
expval=newexpval,
{i,expbits}]

Using this method, the floating point values approximated for the exponent are in reverse order. This section of the program reverses this and creates a character array with the existing values as well.

Expstr="";
Do[(Subscript[Binaryexp, i])=ToString[(Subscript[Binexp, (expbits+1)-i])];
Expstr=StringJoin[Expstr,(Subscript[Binaryexp, i]),"|"],
{i,expbits}]

Concatenating all previously calculated binary components of the floating point binary number.

Mantissacomb="";
Do[Mantissacomb=StringJoin[Mantissacomb,(Subscript[Mantissa, i])],{i,mantbits}]
Binaryexpcomb="";
Do[Binaryexpcomb=StringJoin[Binaryexpcomb,(Subscript[Binaryexp, i])],{i,expbits}]
FinalForm=StringJoin["|",σ,"|"," |",expsign,"| |",Mantstr," |",Expstr];

Print["The sign of the number entry is ",σ,"."]
Print["The sign of the exponent entry is ",expsign,"."]
Print["The mantissa entry is ",Mantissacomb,"."]
Print["The exponent entry is ",Binaryexpcomb,"."]
Print["The final floating point form is ",FinalForm,"."]

Conclusion

This worksheet illustrates the use of Mathematica to convert a base-10 number to a floating point binary representation. Recall that floating point representation is used more often than fixed point representation due to two primary advantages: floating point representation supports a much larger range of values while maintaining a relative error of similar magnitude for all numbers.

References

Floating Point Representation:
See: http://numericalmethods.eng.usf.edu/nbm/gen/01aae/nbm_gen_aae_txt_floatingpoint.pdf

Legal Notice: The copyright for this application is owned by the author(s). Neither Wolfram Research nor the author(s) are responsible for any errors contained within and are not liable for any damages resulting from the use of this material. This application is intended for non-commercial, non-profit use only. Contact the author for permission if you wish to use this application in for-profit activities.