Advertisement

high precission numbers?

Started by October 20, 2001 04:44 AM
0 comments, last by avianRR 23 years, 3 months ago
I''m trying to find a way to work with high precission numbers. Specifically, I need to be able to calculate a movement of 1 inch at a range of a million miles from the origin of the coordinate system. I''m working on a class to hold the value. but I''m haveing the following problem, how do I convert a number to text. i.e. Exponant = 1 Mantasia = 1b the output should be 1 Exponant = 2 Mantasia = 1b the output should be 4 I''ve already created the class object and I''m working on the math functions but I have no way to check the results because I can''t figure out how to convert it to a text display. I''m currently working with a 32 bit Exponant and a 16384 bit Mantasia but I''m planning on upping the size of the Mantasia as soon as I figure this out so I can see if the code even works.
------------------------------Piggies, I need more piggies![pig][pig][pig][pig][pig][pig]------------------------------Do not invoke the wrath of the Irken elite. [flaming]
If you already have the +-/* routines, you can print the number using your FP routines. This is equivalent to using floating point arithmetic to print the number instead of decomposing it using integer instructions.

The floating point printing algorithm goes like this:

	public StringBuffer append(double f) {		// check for zero		if (f == 0.0) {			append("0.0");			return this;		}		// init exponent		int exp = 0;		// get number into scientific notation		if (f > 10.0) {			while (f > 10.0) {				f /= 10.0;				exp++;			}		}		else if (f < 1.0) {			while (f < 1.0) {				f *= 10.0;				exp--;			}		}		// append number		int digit = (int) f;		// get non fractional part		append((char) (digit + '0'));		append('.');		// do 13 digit fractional precision		for (int i = 0; i < 13; i++) {			f -= (double) digit;		// subtract digit from number			f *= 10.0;		// shift number by 10			digit = (int) f;		// get non fractional part			append((char) (digit + '0'));	// append digit to string buffer		}		// append exponent		append('E');		if (exp >= 0)			append('+');		append(exp);		// return string buffer		return this;	}  


Edited by - bpj1138 on October 20, 2001 6:37:17 AM
--bart

This topic is closed to new replies.

Advertisement