Re: Fast conversion of a float to character string ( C language )

From: Paul Lutus (nospam_at_nosite.zzz)
Date: 09/09/04


Date: Thu, 09 Sep 2004 09:08:46 -0700

flair wrote:

/ ...

> Thank you for your response.
>
> Actually, I have to display float numbers that always have one of the
> following formats:
> "8888.88"
> "888.88"
> "88.88"
> "8.88"
> "-8888.88"
> "-888.88"
> "-88.88"
> "-8.88"
> It looks simple.

Those are not formats per se, they represent properties of the data. I mean,
8.88 looks different than 888.88, not because of a formatting choice, but
because it is a different number.

> I have written my own function for the conversion. It improved the
> speed of my program but I am sure I can find a better algorithm.

I am willing to bet your algorithm cannot handle an entire class of rounding
problems that come up with conversion from binary to decimal. If your
algorithm is faster than sprintf(), it does it by taking shortcuts that
don't matter in 99% of cases.

Example. Show your algorithm's output with different numbers of decimal
places (or absolute magnitude) for a double variable loaded with the
results for 4.0/9.0 and 5.0/9.0 respectively.

On the other hand, if your program must format currency values and that is
its only purpose, you may be better off using a method that converts the
doubles to a properly rounded long that is aligned with pennies (e.g. it
resesents an integer number of pennies). In this arrangement, after the
conversion, formatting and printing the numbers in decimal form is
deterministic and reliable.

-- 
Paul Lutus
http://www.arachnoid.com


Relevant Pages

  • Re: Fast conversion of a float to character string ( C language )
    ... |> Those are not formats per se, they represent properties of the data. ... |>> I have written my own function for the conversion. ... |>> speed of my program but I am sure I can find a better algorithm. ... |> pennies. ...
    (comp.programming)
  • Re: Fast conversion of a float to character string ( C language )
    ... ||> Those are not formats per se, they represent properties of the data. ... ||>> speed of my program but I am sure I can find a better algorithm. ... ||> pennies. ...
    (comp.programming)
  • Re: How to preview nv12(yuv420) format video
    ... Any suggestion for an algorithm? ... filter. ... It's nothing more than rearranging bytes. ... formats, yes? ...
    (microsoft.public.win32.programmer.directx.video)
  • Re: Check for mobile device?
    ... place to see what formats the user-agent likes. ... It's even pounded with a quality ratio. ... Apache's algorithm is a good example of clever content negotiation: ...
    (comp.infosystems.www.authoring.html)
  • Re: RfD: IEEE-FP
    ... > You don't have to define new words for all the IEEE 754 operations. ... >> IEEE 754-2008 defines three basic binary fp formats, binary32, ... See IEEE 5.4.2, "Conversion operations ... >> binary32 or binary64 interchange formats, ...
    (comp.lang.forth)