|
|
|
![]() |
|
|||||||
|
||||||||
![]() |
|
|
Thread Tools | Rate Thread | Display Modes |
|
|
|
#1
|
|||
|
|||
|
problem with printing float
We're having a problem with printing any floats in our program. We are performing a division of an integer casted to a float and another integer. Even if we assign a float varaible a constant 0.12345 it won't print to the screen. Any help would be greatly appreciated! The file is attached.
One thing we have noticed is that our MPLAB compiler is configured for the PIC18F8722 and successfully compiles. However if we configure it to the PIC18F8520, it gives the following error. MPLINK 3.90, Linker Copyright (c) 2004 Microchip Technology Inc. Error - processor types do not agree across all input files. Errors : 1 BUILD FAILED: Mon Feb 11 18:40:58 2008 We are suspecting that this is the source of the problem. Thanks, Windell #2477 |
|
#2
|
|||
|
|||
|
Re: problem with printing float
printf does not support %f.
Something like printf("%ld", (long)(ratio * 1000) ); might work, though you're likely to run into a lot of problems with floats. You can avoid floats entirely by shifting the number up to store your decimal places, but in an integer type. Then in calculations, shift it back down. For example: Code:
long ratio;
ratio = (((long)p1_x)<<8 ) / S_PWM_SWING;
if(ratio>128 && magnitude_sum>MAX_SINGLE_MOT_MAG){
RIGHT_WHEEL_PWM=NEUTRAL+MAX_SINGLE_MOT_MAG;
LEFT_WHEEL_PWM=NEUTRAL-(((1<<8 - ratio) * MAX_SINGLE_MOT_MAG)>>8);
}
(In case you don't already know, x<<8 = x*(2^8) and x>>8 = x/(2^8).) |
|
#3
|
|||
|
|||
|
Re: problem with printing float
I posted a floating point print routine last year. Available at http://www.chiefdelphi.com/forums/sh...ad.php?t=55105
|
|
#4
|
|||||
|
|||||
|
Re: problem with printing float
Here's an another alternative with just a little manipulation. You can turn something such as this into a utility function.
Code:
#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.%03d \r", i, i2); //e.g., f = 245.559
|
![]() |
| Thread Tools | |
| Display Modes | Rate This Thread |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| printing FLOAT data type on the terminal window | razer | Programming | 14 | 05-02-2007 08:55 |
| Robots that float | boy_scout72688 | Rules/Strategy | 31 | 15-01-2007 22:18 |
| Float Design | i like dirt | Technical Discussion | 3 | 13-09-2006 00:21 |
| Float in MPLAB | capenga | Programming | 1 | 18-02-2005 06:44 |
| Problem with communicating with STAMP through serial port | Skabana159 | Technical Discussion | 2 | 06-02-2003 21:10 |