I have an embedded project that is close to driving me batty.
Also not floating point here refers to single precision float only. double precision is not something you use on systems you have to scape for 1K of RAM (LOL).
So here is it's I need to print formatted floating point data (float to asc) without using C libraries.
Now I've actually done quite a bit of the work already there are just a few nasty problems. Unfortunately ACM documents aren't available at work (more on what that means in a bit).
First I split the float into it's integer and fractional components. For those who are floating point clueless this is not as easy as it sounds. (If someone wants the long explanation I can give one later).
Now the data needs to be printed with a fixed field width and precision (amazingly similar to the printf format codes "%#.#f" where the first # is the field width and the second # is the precision (IE decimal places). THIS is where things get very tricky.
This function CANNOT work like the typical sprintf luaded by many generations of clueless people. Why? It's not memory safe, which for what I am working on is quite an important feature.
So litterally I cannot print greater than fieldwidth characters no matter how convenient or easy it would be.
Now the issues with the numbers.
OK so I start with the integer part. To get the digits I use the following
field width prevents from over running the buffer and also keeps track of how many digits have been pasted. I preadjust the buffer pointer so that it points to the end of the number (this is calculable). I also account for the minus symbol and a few other things. The problem comes between the fractional and integer parts (figures?) Let's say we have the number
1.3
Now that innocent looking number is ... not 1.3 in floating point no no it's 1.299999938 or something instead. So ... what happens is that if you do
in regular C library you would get
1.3 however with my function you get 1.2... round off error (sigh).
But wait there is MORE let's talk annoying numbers for example 999999.9 you think "oh that's easy" well guess what it's EVIL. 999999.875 is the ACTUALY number in floating point AND if you do something like
in normal C code you get 999999.9 (correct) but I get 999999.8 (LOL).
These aren't easy to correct either. since parts of the data are fractions. I suppose I could do rounding (via addition.
Hmmm anyhow for those of you who want to aspire to computer science that is something I'm sure you will find .. annoying. References?
1996 quickly and accurately
1990 accurately <not full text you can't get it due to ACM requirements>
However I believe I have found a solution but it will still be a PAIN to do. (sigh)
Cyb
Also not floating point here refers to single precision float only. double precision is not something you use on systems you have to scape for 1K of RAM (LOL).
So here is it's I need to print formatted floating point data (float to asc) without using C libraries.
Now I've actually done quite a bit of the work already there are just a few nasty problems. Unfortunately ACM documents aren't available at work (more on what that means in a bit).
First I split the float into it's integer and fractional components. For those who are floating point clueless this is not as easy as it sounds. (If someone wants the long explanation I can give one later).
Now the data needs to be printed with a fixed field width and precision (amazingly similar to the printf format codes "%#.#f" where the first # is the field width and the second # is the precision (IE decimal places). THIS is where things get very tricky.
This function CANNOT work like the typical sprintf luaded by many generations of clueless people. Why? It's not memory safe, which for what I am working on is quite an important feature.
So litterally I cannot print greater than fieldwidth characters no matter how convenient or easy it would be.
Now the issues with the numbers.
OK so I start with the integer part. To get the digits I use the following
Code:
if(int_component)
{
while((int_component)&&
(field_width))
{
store_to_buffer_dec(int_component % 10 + '0');
int_component /= 10;
field_width--;
}
}
1.3
Now that innocent looking number is ... not 1.3 in floating point no no it's 1.299999938 or something instead. So ... what happens is that if you do
Code:
printf("%5.1f", 1.3);
1.3 however with my function you get 1.2... round off error (sigh).
But wait there is MORE let's talk annoying numbers for example 999999.9 you think "oh that's easy" well guess what it's EVIL. 999999.875 is the ACTUALY number in floating point AND if you do something like
Code:
printf("%10.3f", 999999.9);
These aren't easy to correct either. since parts of the data are fractions. I suppose I could do rounding (via addition.
Hmmm anyhow for those of you who want to aspire to computer science that is something I'm sure you will find .. annoying. References?
1996 quickly and accurately
1990 accurately <not full text you can't get it due to ACM requirements>
However I believe I have found a solution but it will still be a PAIN to do. (sigh)
Cyb