printf problem

I’m working with floats for calculations. I’m trying to use a printf statement to display the value of various floats, but I keep having problems. When I use %d, I get a wacky number over 37,000 which makes no sense. When I use %f, I get no output of the number at all. Could somebody tell me what I’m doing wrong. Here’s an example of the format of how I’m doing my printf.

printf (“Value: %f /n”, (float)value_name);

I’ve tried it with %f and %d… as well as trying to use my variables as “double” as well. Same problems though. Any help would be very much appreciated. Thanks!

printf does not support printing of floating points :’(. sorry, it just doesn’t work. (see the comment in printf_lib.c)
What SHOULD work is using %d and then explicitly casting the float to an int.
IE: printf("%d
", (int)fFloat);
Unfortunately, for me, this ALWAYS prints 0, no matter how large fFloat is.
I’m still having trouble with how to convert floats to ints. I wrote a quick function in c that I’m going to test today, and I found assembly routines from 1997 (see product note AN575 on microchip’s page) that I will try.

Around here the consensus seems to be that floating points are more trouble than they are worth on this processor. In most cases where you would use floating point on the robot it’s possible to do the math in the integer domain by multiplying first then dividing down.

If you’re using the new compiler, I believe the printf() included in that [strike]works with floats[/strike] (check the documentation in .\mcc18\doc).

Ok so I am having the SAME problem. I can’t see if my float calculations are working or not. Did you see the README in the IFI released camera code:


www.InnovationFirst.com

1/8/2004
-   Removed printf_lib.c and printf_lib.h - now using <stdio.h>, compiler needs -nw=2066 to suppress warnings
-   Added user_camera.c and user_camera.h - to remove code from build -> remove the _USE_CMU_CAMERA macro
-   Replaced file named PicSerialDrv with user_serialdrv
-   Note:  when using a 'printf' use a '\r' rather than a '
' with the new compiler (v2.4) 

So I am not using print_f.lib. I am using stdio.h. I am doing the same thing as
miketwalker but I don’t get anything to the screen while trying to use floats.

Has anyone else got any input? I am using MPLAB, not CBOT, and the new 2.4 version complier…

I spent all day wondering if our float calculations are actually working.

Is float supported in this PIC?

Can I even do a calculation like this?

If WHEELBASE = 485.35 mm?



pos_mm_theta = (pos_mm_Left_Encoder_Count - pos_mm_Right_Encoder_Count)/WHEELBASE;
posx_mm = pos_mm_distance * sin(pos_mm_theta);
posy_mm = pos_mm_distance * cos(pos_mm_theta);


No.

Yes.

The PIC18 does not contain a floating point unit or floating point instructions. All floating point math is handled in software and handled inline by the compiler. This is why all floating point math takes a long time, and is highly discouraged.

However, from looking at the documentation, it appears that floats are not supported by printf() and related functions. See section 4.7 in .\mcc18\doc\MPLAB-C18-Libraries.pdf for the formats supported.

To print floats you can always use a little manipulation like:


#define ACCURACY 1000	//How many decimal places are important to you
float f;
int i, i2;   // Might need to be longs if you have a lot of significant digits
 
f = 245.56;
 
/* Print float value */ 
i = (int) f;
i2 = (int) ((f-i)*ACCURACY);
printf("f = %d.%d \r", i, i2); //e.g., f = 245.559

You’re going to want to be sure leading zeroes are printed for the fractional part. Does this printf implementation support “%03d”?

That’s very true. I neglected that.
We should verify that the leading zeros can be forced. I’ll call one of my kids later to test it on the controller at school to be sure.

[edit] That is supported (and has been tested), so the corrected printf would read:


printf("f = %d.%03d \r", i, i2);

Thanks Greg.