Google
 

Converting Decimal Real Numbers  

This tool is provided as an implementation of conversion algorithms based on division-multiplication method modeled as Java Scripts. It converts real decimal numbers to their equivalent in Binary, Octal, and Hexadecimal.



The equivalent of the Real number:

(the number must contain the fraction part; example 2007.21)

IS:
(division-multiplication method) .
.
.

The Check Processor button activate a floating-point computation to check if the processor of your computer have the "Pentium floating-point bug" discovered in 1994 by a mathematician.

 

The generalized division-multiplication algorithm for converting decimal real numbers in other bases is:

Given a real number N expressed in a base b, denoted by N(b). The number can be expressed as a sum of an integral part NI(b) and a fraction one NF(b), N(b)=NI(b) + NF(b). If we intend to convert the number in a new base q (q>1), we must first isolate the integral part from the fraction part.

The integral part will be converted by using the algorithm described in the page Integer Numbers and will not be reiterated here.

Below is a conversion algorithm for the fraction part of the real number.

Fraction Part. Given a fraction number NF(b) it can be represented in a base q as:

NF(b)=a′-1*q-1+a′-2*q-2+...+a′-m*q-m, where a-j are digits in the new base q.

            We must determine the coefficients a′-j, and that is to do by multiplying repeatedly (the original number first and the fraction part of each cycle latter) with the new base:   

NF(b)*q=a′-1+a′-2*q-2+1+...+a′-m*q-m+1 ⇒ a′-1 the most significant digit in base q.
            By this process we obtain a new fraction part: a′-2*q-1+...+a′-m*q-m+1 that mean N'F(b). The process of multiplying the fraction numbers by q is ended in the moment where desired precision is obtained. In order to realize the check for precision you must realize the difference between two consecutive values for the result and to compare the difference with the desired precision (for example, the precision for money is at two decimals that mean the value to check can be 0.009, typically four decimals for quantities and so on).

            The generalized algorithm for this method is:


Step1.

The fraction part is multiplied by the new base and we obtain an integral part and a fraction part. The integral part is a digit in the new base;

Step2.

If the fraction part is 0
    Then
the conversion process stops;
    Else
take in account the new fraction part as a new number and execute Step1;

            The coefficients are written in the order they obtained and the number is the equivalent in the new base.

Implementation comments

The implementation of the algorithm converts decimal reals to Binary, Octal, and Hexadecimal bases. (NewBase-1).

/* The memory variable Digits contains the digits for decimal, binary, octal and hexadecimal numbering systems */

var Digits="0123456789abcdef";
/* The variable Precision defined and initialized. It is defined as the number of digits in the new base to be determined before ending the algorithm if this do not stops naturally.*/
var Precision=8;
/* The name of a specific conversion function is fdec-to-NewBaseName (example: fdec2bin, meaning floating-decimal-to-binary)
decimalNumber - contains the decimal integer passed in as argument
The variable Base contains the new base in decimal (example: 16 for hexadecimal, 8 for octal, 2 for binary etc) */

function fdec2NewBase(decimalNumber)
{

/* Defines a variable i to number the cycles and a variable id to store the integral part of the number*/
var i=0;
var id=0;
/* Defines a variable to store the converted number and initializes that to "empty string" (null)*/
var convertedNumber = "";
/* If the fraction part is not 0 and the the number of digits for the new number not yet computed execute a new cycle otherwise go to return the result*/
while(decimalNumber !=0 & i<=Precision) {
/* Multiply the fraction part by the new base*/
decimalNumber *=Base;
/* Insulate the integral part of the number previously obtained. This is a new digit of the number in the new base */
id = Math.floor(d);
/* Compute the new fraction part as difference between the number and his integral part. JavaScript do not allows a function to insulate only the fraction or integer, as the Int() function in Visual Basic, for example */ decimalNumber=decimalNumber-id;
/* Pick up the digit in the new base corresponding to the integral value previously obtained and place that before the ones previously obtained */ convertedNumber = convertedNumber+Digits.substr(id&(Base-1),1);
/* Increase the cycle counter to be able to check the precision and go to a new cycle */ i++; }
/* Return the converted number to the caller */
return convertedNumber;
}



*)The theory in that page is made by excerpts from the books:
- Vasile Avram, Gheorghe Dodescu: Informatics: Computer Hardware and Programming in Visual Basic, Ed. Economică, Bucureşti, 2003
- Vasile Avram, Gheorghe Dodescu: General Informatics, Ed. Economica, 1997

Copyright © 2006-2008 Vasile Avram

Valid CSS! Valid HTML 4.01 Transitional Cynthia Tested!