|
|
View previous topic :: View next topic |
Author |
Message |
a Guest
|
i just see this file " ieeefloat.c " driver of ccs |
Posted: Mon Jul 12, 2004 7:31 am |
|
|
i just see this file " ieeefloat.c " driver of ccs. What is it?
and how to solve problem about value 15.000(True value) but when printf is 14.9999.
Please explain about it. Thank you very much. And sorry about my poor english.
int32 f_PICtoIEEE(float f)
{
int16 c[2];
memcpy((int8*)c , ((int8*)&f)+3, 1);
memcpy((int8*)c+1, ((int8*)&f)+2, 1);
memcpy((int8*)c+2, ((int8*)&f)+1, 1);
memcpy((int8*)c+3, ((int8*)&f) , 1);
c[1] = ((c[1] >> 1) & 0x7F80) + (c[1] & 0x7F) + ((c[1] & 0x80) << 8);
return ((int32)c[1] << 16) | c[0];
}
float f_IEEEtoPIC(int32 f)
{
float ret;
int16 temp;
memcpy(&temp, ((int8*)&f)+2, 2);
temp = ((temp << 1) & 0xFF00) + (temp & 0xFF);
if(bit_test(f, 31)) // Test the sign bit
temp |= 0x0080;
else
temp &= 0xFF7F;
memcpy(((int8*)&ret)+3, ((int8*)&f) , 1);
memcpy(((int8*)&ret)+2, ((int8*)&f)+1 , 1);
memcpy(((int8*)&ret)+1, ((int8*)&temp) , 1);
memcpy(((int8*)&ret) , ((int8*)&temp)+1, 1);
return ret;
} |
|
|
Ttelmah Guest
|
Re: i just see this file " ieeefloat.c " driver of |
Posted: Mon Jul 12, 2004 11:01 am |
|
|
a wrote: | i just see this file " ieeefloat.c " driver of ccs. What is it?
and how to solve problem about value 15.000(True value) but when printf is 14.9999.
Please explain about it. Thank you very much. And sorry about my poor english.
int32 f_PICtoIEEE(float f)
{
int16 c[2];
memcpy((int8*)c , ((int8*)&f)+3, 1);
memcpy((int8*)c+1, ((int8*)&f)+2, 1);
memcpy((int8*)c+2, ((int8*)&f)+1, 1);
memcpy((int8*)c+3, ((int8*)&f) , 1);
c[1] = ((c[1] >> 1) & 0x7F80) + (c[1] & 0x7F) + ((c[1] & 0x80) << 8);
return ((int32)c[1] << 16) | c[0];
}
float f_IEEEtoPIC(int32 f)
{
float ret;
int16 temp;
memcpy(&temp, ((int8*)&f)+2, 2);
temp = ((temp << 1) & 0xFF00) + (temp & 0xFF);
if(bit_test(f, 31)) // Test the sign bit
temp |= 0x0080;
else
temp &= 0xFF7F;
memcpy(((int8*)&ret)+3, ((int8*)&f) , 1);
memcpy(((int8*)&ret)+2, ((int8*)&f)+1 , 1);
memcpy(((int8*)&ret)+1, ((int8*)&temp) , 1);
memcpy(((int8*)&ret) , ((int8*)&temp)+1, 1);
return ret;
} |
The file contains routines to convert to/from the IEEE format. This is necessary/useful, when sending raw data to other programs outside the PIC.
The 14.9999 problem is down to the basic accuracy of the CCS float, and the inability of binary based floats to represent some numbers accurately. Provided you are working to less digits than this, you can 'improve' the behaviour when truncating, with code like:
printf("%2.3f",val+0.0005);
where the constant added is beyond the last displayed decimal place, which gives effectively a 4/5 rounding, rather than the simple truncation that otherwise results.
Best Wishes |
|
|
|
|
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
|