Page 1 of 1

PROstart - MakeitC printf problem

Posted: Sun Mar 10, 2013 12:06 pm
by Pompey2
I can't figure out how to get printf to print floats when using C on a PROstart. Basic has been overwritten
I'm using the the "case 60 printf" demo from Coridium sample file Csample.c dated 2/7/2013 as my test case.

USE_FLOAT is defined in the code. i is integer, x is float.
I changed %d to %f or 1.3f in a couple of places, and replaced i with x.
Here's the changed section

Code: Select all

#ifdef USE_FLOAT
			case 60:
				x =1.23456*1000;
				i = x * 1000;
				//printf("1.234*1000 %d\n",i);
				printf("1.234*1000 is: %f\n",x);
				printf("1.234*1000 is: %1.3f\n",x);
				i = 100*sinf(00.01);
				printf("sin %d\n",i);
				i = 100*cosf(00.01);
				printf("cos %d\n",i);
				//i = 100*sqrtf(1000);
				x = sqrtf(1000);
				printf("sqrt of 1000 is:  %f\n",x);
				i = 100*log10f(2000);
				printf("log10 %d\n",i);
				printf("pi = %d\n",i);
				// how to do printf with floats
				printf("%d.%03d",(int) floor(x), (int) (1000* (x-floor(x))));
				break;
	#endif	
Here's what I get:

enter option:1.234*1000 is:
1.234*1000 is: 3f

sin 0
cos 99
sqrt of 1000 is:
log10 330
pi = 330
31.622

The last couple of lines of sample code would suggest that printf doesn't handle floats, but Coridium says it does.

Re: PROstart - MakeitC printf problem

Posted: Sun Mar 10, 2013 2:02 pm
by basicchip
USE_FLOAT has to be defined when printf is compiled as well

You can do that in MakeItC under the menu item User Compile options

-DUSE_FLOAT

From MakeItC you can find all instances of USE_FLOAT in the search window, which would show you that there is an #ifdef USE_FLOAT in printf.c

So printf does handle float, it is just not the default case. It could be made that way, just for most of the code we write floats are seldom used.

Re: PROstart - MakeitC printf problem

Posted: Sun Mar 10, 2013 2:15 pm
by basicchip
A quick check enabling USE_FLOAT just for printf adds almost 3.9K to a C program even if floats are not used.

This is a big deal for the smaller parts, so integer handling will remain the default.