|
|
View previous topic :: View next topic |
Author |
Message |
Kimi
Joined: 30 Jan 2005 Posts: 23 Location: Argentina
|
Bugs with Float? |
Posted: Thu Feb 03, 2005 1:56 pm |
|
|
Hello
I have a piece of code, which involves making very complex float maths. I have to calculate the distance from two near points which are given by a GPS. I have the formula to calculate this, but I have found a extrange behaviour when adding two floats values. This is the Piece of Code, and I will tell what the printf shows
#include <16F877.h>
#include <math.h>
#use delay(clock=16000000,RESTART_WDT)
#fuses NOWDT,HS, NOPUT, NOPROTECT, NOBROWNOUT, NOLVP, NOCPD, NOWRT, NODEBUG
#use rs232(baud=57600,parity=N,xmit=PIN_D2,rcv=PIN_D3,bits=8,stream=PC,errors)
float dist1;
float temp1;
void tracking (float lat1,lon1,lat,lon) {
dist1 = lat1;
dist1 = dist1 * 12.44;
dist1 = dist1 + 110574;
temp1 = lat1;
temp1 = temp1 - lat;
}
void main () {
float a1,a2,x1,x2;
a1 = -34.61084;
a2 = -58.43049;
x1 = -34.60900;
x2 = -58.42851;
tracking (a1,a2,x1,2);
fprintf(PC,"dist1: %4.4f temp1: %4.4f",dist1,temp1);
while(1);
}
The printf shows dist1: 110143.4387 temp1: -166.6108
Shouldn't temp1 be: -0.00184?
Thanks
Kimi |
|
|
C-H Wu Guest
|
float bug in function definition ? |
Posted: Thu Feb 03, 2005 8:41 pm |
|
|
Kimi:
I use PCWH 3.187, 3.212, 3.216, 3.218 to compile your code, bug confirmed for both F877 with PCM and 18F4620 with PCH!
void tracking (float lat1,lon1,lat,lon)
Code: | 038-03B tracking.lat1 <-- float, 4 bytes
03C tracking.lon1 <-- become int8, only 1 byte ! bug ?
03D tracking.lat <-- become int8 ! bug ?
03E tracking.lon <-- become int8 ! bug ?
|
workaround :
void tracking (float lat1,float lon1,float lat,float lon) {
Code: |
03C-03F tracking.lat1
040-043 tracking.lon1 <-- no more bug
044-047 tracking.lat
048-04B tracking.lon
|
However, there is no such bug with the following syntax
Code: |
void main () {
float a1,a2,x1,x2;
|
I never notice this CCS feature , thanks a lot for your information.
You should file a bug report to CCS !
Best wishes
C-H Wu |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Thu Feb 03, 2005 9:24 pm |
|
|
Quote: |
void tracking (float lat1,lon1,lat,lon)
038-03B tracking.lat1 <-- float, 4 bytes
03C tracking.lon1 <-- become int8, only 1 byte ! bug ?
03D tracking.lat <-- become int8 ! bug ?
03E tracking.lon <-- become int8 ! bug ?
|
It's a bug, but in the sense that the compiler should have given
an error, instead of defaulting to int8.
If I look in the help file for MSVC++ 6.0
and look under "Function Parameters", it says this:
Code: | Each identifier in parameter-type-list must be preceded by its appropriate type specifier, as shown in this example:
void new( double x, double y, double z )
{
/* Function body here */
} |
CCS is based on K&R, and maybe somewhere in K&R it says the
same as above, but I'm not going to spend any more time looking for it. |
|
|
Mark
Joined: 07 Sep 2003 Posts: 2838 Location: Atlanta, GA
|
|
Posted: Thu Feb 03, 2005 10:10 pm |
|
|
Quote: |
In the ANSI standard:
Definitions must be coded in the same order in which arguments are passed.
Only one identifier for each type specifier can be coded in an ANSI
parameter definition.
The comma ( , ) now separates the definitions between the parentheses.
There is no semicolon to terminate a definition.
|
The default data type for CCS is int8 thus what you see. |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Thu Feb 03, 2005 10:49 pm |
|
|
The key part is here in these two sentences:
Quote: |
Only one identifier for each type specifier can be coded in an ANSI
parameter definition.
The comma ( , ) now separates the definitions between the parentheses. |
That means you can't do this:
Code: | void func(float a, b, c, d); |
You must do it this way:
Code: | void func(float a, float b, float c, float d); |
Because CCS doesn't give an error for the first declaration above,
it's in violation of the ANSI standard. But CCS doesn't say that
they are in accord with ANSI.
If I try to find some compiler on my system that might be K&R
compatible, I have Quick C for Windows vs. 1.0, copyright 1991.
If I run the following code,
Code: | void my_func(float a, b, c, d)
{
} |
then I get this error message:
C:\QCWIN\GENERAL\TEST.C(6) : error C2061: syntax error : identifier 'b'
TEST.EXE - 1 error(s), 0 warning(s)
I have to do this to avoid an error:
Code: | void my_func(float a, float b, float c, float d)
{
} |
So CCS is in violation of ANSI and probably K&R as well.
They shouldn't default to int8 for parameters that are missing a
type-definition. They should give an error. |
|
|
|
|
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
|