|
|
View previous topic :: View next topic |
Author |
Message |
Calamar
Joined: 07 Sep 2003 Posts: 60 Location: Buenos Aires (Argentina)
|
newbie in float |
Posted: Sat Mar 26, 2005 10:30 am |
|
|
Why this code:
#include <16f877.h>
#use delay(clock=4000000)
#include <c:\codigo\floowmeet4000\lcd.c>
#fuses HS, NOWDT, PUT, BROWNOUT, NOPROTECT
#use rs232(baud=9600,parity=N,xmit=PIN_C6,rcv=PIN_C7,bits=9)
#include <stdlib.h>
main() {
float k = 2345.67;
float k_min = 0;
float incremento = 0.01;
k_min = k /100;
printf("\n\r%f\n\r%f",k,k_min);
k_min =0;
while(true){
delay_ms(1000);
k_min += incremento;
printf("\n\r%f",k_min);
}
}
return this:
2345.669850 23.456699
.009999
.019999
.029999
.039999
I would like work with 3 decimal digits,
I was reading the topic (ccs compiler help):
What is the format of floating point numbers?
and I think that something is worng because
I expect recive:
2345.67
23.4567
.01
.02
.03
.04 _________________ Best Regards
Daniel H. Sagarra
La Plata (Argentina) |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Sat Mar 26, 2005 12:41 pm |
|
|
Not all decimal fractions can be represented exactly with the 32-bit
Microchip floating point format. Here is a program which will let you
experiment with this:
Go here:
http://www.piclist.com/techref/microchip/math/fpconvert.htm
Download this file:
floatconv10.zip
Open it with WinZip and drag the Floatconv.exe file onto your desktop.
Run it.
1. On the left side, select the Format: Microchip 32bit
2. In the top box for Float, enter: .01
3. On the right, click the button for Convert To Byte.
Notice that it fills in the values for BYTE1, BYTE2, BYTE3, BYTE4.
4. Press button for Convert To Float.
Notice that the top box now says: 0.00999999977648
This is the closest number to .01 that you can get with the Microchip
32-bit format.
You can test this. Go back, and put .01 in the top box. Press
Convert to Byte. Now go to the BYTE4 box. It has an 'A' in it.
Change it to a 'B' and press Convert to Float. The top box now says:
0.0100000007078
Compare those numbers:
A -- 0.00999999977648
B -- 0.0100000007078
The 'A' value is closer to .01, so it is the best approximation.
When you use this program, make sure that you always select
the proper format, which is: Microchip 32bit |
|
|
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|