UKHAS Wiki

UK High Altitude Society

User Tools

Site Tools


code:rprintf

This is an old revision of the document!


floating point print function

This is based on the origional rprintf by Pascal Stang. however numDigits now refers to digits after decimal point, and the is no '+' character printed for +ive numbers.

void rprintfFloat(char numDigits, double x)
{
	unsigned char firstplace = FALSE;
	unsigned char negative;
	unsigned char i=255, digit;
	double place = 1.0;
 
	// save sign
	negative = (x<0);
	// convert to absolute value
	x = (x>0)?(x):(-x);
 
	// find starting digit place
	for(i=0; i<15; i++)
	{
		if((x/place) < 10.0)
			break;
		else
			place *= 10.0;
	}
	// print polarity character
	if(negative)
		rprintfChar('-');
	//else
	//	rprintfChar('+');
 
	// print digits
	for(;i;i--)
	{
		digit = (x/place);
 
		if(digit | firstplace | (place == 1.0))
		{
			firstplace = TRUE;
			rprintfChar(digit+0x30);
		}
		else
			rprintfChar(' ');
 
		if(place == 1.0)
		{
			rprintfChar('.');
			i=numDigits;
		}
 
		x -= (digit*place);
		place /= 10.0;
	}
}
code/rprintf.1225743515.txt.gz · Last modified: 2008/11/03 20:18 by laurenceb

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki