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 i=255, digit;
	double place = 1000000000000000.0;
 
	if(x<0)
	{
		rprintfChar('-');
		x=-x;							//convert to absolute value
	}
	for(;i;i--)
	{
		digit=x/place;
		if(digit | firstplace)			//digit>=1
		{
			firstplace = TRUE;			//we have reached the first 
			rprintfChar(digit+0x30);
		}
		if(place == 1.0)
		{
			if(!firstplace)
				rprintfChar('0');	//always zero before the decimal point
			rprintfChar('.');		//the decimal point
			firstplace = TRUE;		//we have to print after the decimal place
			i=numDigits;
		}
		x-=digit*place;
		place/=10.0;
	}
}
code/rprintf.1225757077.txt.gz · Last modified: 2008/11/04 00:04 by laurenceb

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki