How do you print the EXACT value of a floating point number?

后端 未结 10 1275
自闭症患者
自闭症患者 2020-11-27 03:45

First of all, this is not a floating point newbie question. I know results of floating point arithmetic (not to mention transcendental functions) usually cannot be represent

10条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-11-27 04:40

    I see you’ve accepted an answer already, but here are a couple of open source implementations of this conversion you might want to look at:

    1. David Gay’s dtoa() function in dtoa.c: http://www.netlib.org/fp/dtoa.c.

    2. The function ___printf_fp() in the /stdio-common/printf_fp.c file in Glibc (http://ftp.gnu.org/gnu/glibc/glibc-2.11.2.tar.gz, for example).

    Both will print as many digits as you ask for in a %f-type printf, as I’ve written about at:

    • http://www.exploringbinary.com/print-precision-of-dyadic-fractions-varies-by-language/ and
    • http://www.exploringbinary.com/print-precision-of-floating-point-integers-varies-too/.

提交回复
热议问题