![]() |
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! |
Re: printf problem
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\n", (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. |
Re: printf problem
If you're using the new compiler, I believe the printf() included in that
|
Re: printf problem
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:
Code:
www.InnovationFirst.comSo 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? Code:
Quote:
|
Re: printf problem
Quote:
Quote:
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. |
Re: printf problem
To print floats you can always use a little manipulation like:
Code:
#define ACCURACY 1000 //How many decimal places are important to you |
Re: printf problem
Quote:
|
Re: printf problem
Quote:
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: Code:
printf("f = %d.%03d \r", i, i2); |
| All times are GMT -5. The time now is 02:46. |
Powered by vBulletin® Version 3.6.4
Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.
Copyright © Chief Delphi