CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to CCS Technical Support

Please Help me i write float 121.19987 and 121.6998

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
a
Guest







Please Help me i write float 121.19987 and 121.6998
PostPosted: Wed Jan 21, 2004 3:18 am     Reply with quote

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

View user's profile Send private message AIM Address

PostPosted: Wed Jan 21, 2004 8:03 am     Reply with quote

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.
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
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