Now the operations. Suppose you only have the data type for signed integer. So you can use two integer variables to store the result of a floating point operation. Something like this:
int digits,point
digits = 123450
point = 3
(that is number 123.456)
A fuction for division (the sintax is incorrect. but that is the idea):
fuction divide (int numerator,denominator) {
const maxprecision = 10;
int digits,point,i;
for (i=0,i<maxprecision,i++) {
digits = (numerator*10^i) / denominator;
if (digits*denominator = numerator*10^i) {
/* exact division, no more decimal places */
break; /* or the correct statement to exit the for loop*/
}
}
point = i;
return digits,point; /*that is incorrect, you must use some kind of structure/record to output the two values*/
}
Got it?
lets check for numerator = 1 and denominator = 4 (0.25)
i = 0
digits = (numerator*10^i) / denominator;
digits = (1*1) / 4 = 0
if (digits*denominator = numerator*10^i) {
if (0*4=1*1) - false, loop continues
i = 1
digits = (1*10) / 4 = 3 (or 2, i dont know if the language rounds 2.5 to 2 or 3)
if ( (3*4) = 1*10 ) false
in the other case where the language makes 2.5 => 2
if ( (2*4) = 1*10 ) false anyway
loop continues
i = 2
digits = (1*100) / 4 = 25
if (25*4) = 1*100 (true, loop stops)
digits = 25
point = i = 2
so the answer is digits*10^-point = 0.25
if we have too many decimal places, the loop will stop at maxprecision.
Main Topics
Browse All Topics





by: acerolaPosted on 2002-07-20 at 14:47:32ID: 7167167
Floating point variables have three parts.
1-A bit to specify the sign.
2-digits
3-position of the point.
Like this:
0.25 = + 25 *10^-2
-123.456 = - 1233456 *10^-3
25000 = + 25000 *10^0
The first part stores the sign (+ or -). The second store the digits (25 or 123456 or 25000). The third stores the power of ten the number is multiplied by (-2 or -3 or 0)