Re: converting float to ascii w/o printf
- From: "Wilco Dijkstra" <Wilco_dot_Dijkstra@xxxxxxxxxxxx>
- Date: Wed, 28 Mar 2007 14:38:22 GMT
"Thad Smith" <ThadSmith@xxxxxxx> wrote in message
news:4609b2ec$0$47168$892e0abb@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
Wilco Dijkstra wrote:
"Ron Blancarte" <ron@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx> wrote in message
news:c0ui03dctcb8t4s04fmrs65v837jimars1@xxxxxxxxxx
Clearly, the entered value is being stored correctly and the displayed
value, while close, is just slightly off. So is there a way to do
this without this error induced by my multiplications by 10 (and still
not using sprintf()?
The correct way of doing this is to only do one normalizing multiply
or divide by a power of 10, so you only get one roundoff error. Powers
of 10 are exact in floats up to 10^10, for a wider dynamic range you
get multiple roundoff errors unless you use more precision. After
normalization you use integer arithmetic to extract the digits.
Agreed.
My advice would be to use integer-only arithmetic for normalization,
this way you get less roundoff error over much larger ranges.
Hmmm, I don't understand the recommendation here. I would say multiply or divide by the
proper (exact) power of 10 to get a number in the range -9999999..9999999, add a
rounding factor (-0.5 or +0.5), convert to a 32-bit integer, convert that to a 7-digit
character string, then format.
What I mean is that if you do the normalization multiply/divide using
integer arithmetic, you get more precision, eg. 32 bits rathern than 24
when using float. This gives you larger powers of 10 that can be
represented exactly and a more accurate result after normalization
(and more control over rounding). Integer arithmetic makes even more
sense if you use emulated floating point.
Wilco
.
- Follow-Ups:
- Re: converting float to ascii w/o printf
- From: Everett M. Greene
- Re: converting float to ascii w/o printf
- References:
- converting float to ascii w/o printf
- From: Ron Blancarte
- Re: converting float to ascii w/o printf
- From: Wilco Dijkstra
- Re: converting float to ascii w/o printf
- From: Thad Smith
- converting float to ascii w/o printf
- Prev by Date: Re: converting float to ascii w/o printf
- Next by Date: Re: Nvram write modes and problems
- Previous by thread: Re: converting float to ascii w/o printf
- Next by thread: Re: converting float to ascii w/o printf
- Index(es):
Relevant Pages
|