Please Help me i write float 121.19987 and 121.6998
Posted: Wed Jan 21, 2004 3:18 am
Please Help me i write float 121.19987 and 121.6998 in eeprom address 0 and 4
in pic16f877 but when i read eeprom from address 0 and 1. i received value 121.199864 and 121.699797.
Can i read true value of this?????
below is sourcecode
=================================================
WRITE_FLOAT_EXT_EEPROM(long int n, float data)
{
int i;
for (i = 0; i < 4; i++)
{
write_eeprom(i + n, *(&data + i));
}
}
float READ_FLOAT_EXT_EEPROM(long int n)
{
int i;
float data;
for (i = 0; i < 4; i++)
{
*(&data + i) = read_eeprom(i + n);
}
return(data);
}
WRITE_FLOAT_EXT_EEPROM(0,121.19987);
WRITE_FLOAT_EXT_EEPROM(4,121.6998);
valuefloat=(float)READ_FLOAT_EXT_EEPROM(0);
printf("%f\n",valuefloat);
valuefloat=(float)READ_FLOAT_EXT_EEPROM(4);
printf("%f\n",valuefloat);
=================================================
Douglas Kennedy
Joined: 07 Sep 2003 Posts: 755 Location: Florida
Posted: Wed Jan 21, 2004 8:03 am
The short answer is no.
32 bit floating point has a 23 bit mantissa this only assures you of 6 decimal digits of precision. The CCS calculation is absolutely accurate in the binary system...the apparent error is introduced in conversion to the decimal system. EX .1 (10 to the power -1) cannot be accurately represented in the binary system. First approx is ( 2 to the power -4 or .0625) further negative powers of 2 are used to adjust closer but never ever can the number be anything but an approximation. This is not true of all decimal numbers Ex .5 can be expressed without error. However since most decimal numbers introduce errors your guaranteed precision is limited to 6 decimal digits.
To get high precision fixed point (custom integer arithmetics) is often used. The operands are kept in integer variables and the power of ten exponents are tracked separately with the decimal pt inserted for print display only.
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