View previous topic :: View next topic |
Author |
Message |
sraiderk
Joined: 09 Feb 2005 Posts: 48
|
float to byte or char array |
Posted: Wed May 27, 2009 8:18 am |
|
|
How can I convert float to byte or char array
For example
if x=(float)123456789345.545
then,
function find the following result for me...Where z is rhe array
z[0]=1;
z[1]=2;
z[2]=3;
z[3]=4;
z[4]=5;
z[5]=6;
z[6]=7;
z[7]=8;
z[8]=9;
z[9]=3;
z[10]=4;
z[11]=5;
z[12]='.';
z[13]=5;
z[14]=4;
z[15]=5; |
|
![](templates/subSilver/images/spacer.gif) |
Ttelmah Guest
|
|
Posted: Wed May 27, 2009 8:41 am |
|
|
1) Sprintf.Just the same as printing a value out to the serial etc., except you send the result to a character array.
2) However, you won't get what you expect. Remember a CCS float, is only a 23bit + 1 value. Equivalent to about 6.5 digits of 'useable' precision...
Best Wishes |
|
![](templates/subSilver/images/spacer.gif) |
sraiderk
Joined: 09 Feb 2005 Posts: 48
|
|
Posted: Thu May 28, 2009 2:49 am |
|
|
I want to learn the following:
float x;
x=256*256*256*256*256;
I using calculator it's calculate it 1099511627776
in ccs it 's calculated as 1099511697920 why this is different? |
|
![](templates/subSilver/images/spacer.gif) |
Ttelmah Guest
|
|
Posted: Thu May 28, 2009 3:27 am |
|
|
Look at what I have already posted. Only about 6.5 digits of 'real' precision. Count the number of digits that agree. The rest of the number is effectively 'garbage'.
If you have the PConvert tool (comes with the windows compilers), you can play with the numbers to see what is really stored.
Hex 53800000 gives 1.0995116E12. Change the Hex by just 1 to 53800001, and the decimal jumps to 1.095118E12. There is no possibility of storing a number between these values at all...
Do a web seqarch on numeric precision, and you will find a lot about this.
Best Wishes |
|
![](templates/subSilver/images/spacer.gif) |
|