[GHC] #13682: When printf "%g" with trailing 0 number, decial point be printed.

GHC ghc-devs at haskell.org
Thu May 11 14:46:00 UTC 2017


#13682: When printf "%g" with trailing 0 number, decial point be printed.
-------------------------------------+-------------------------------------
        Reporter:  nakaji_dayo       |                Owner:  (none)
            Type:  bug               |               Status:  new
        Priority:  normal            |            Milestone:
       Component:  libraries/base    |              Version:  8.0.1
      Resolution:                    |             Keywords:  printf
Operating System:  MacOS X           |         Architecture:
                                     |  Unknown/Multiple
 Type of failure:  None/Unknown      |            Test Case:
      Blocked By:                    |             Blocking:
 Related Tickets:                    |  Differential Rev(s):
       Wiki Page:                    |
-------------------------------------+-------------------------------------

Comment (by RyanGlScott):

 Indeed, we currently note in the documentation that the identifiers for
 `RealFloat` types differs from how C treats them. Quote
 [http://git.haskell.org/ghc.git/blob/2316ee1c42d7f4dc229295a5b5426dde40944dc1:/libraries/base/Text/Printf.hs#l226
 the docs],

 > Note that the formatting for `RealFloat` types is currently a bit
 different from that of C `printf(3)`, conforming instead to `showEFloat`,
 `showFFloat` and **`showGFloat`** (and their alternate versions
 `showFFloatAlt` and **`showGFloatAlt`**). This is hard to fix: the fixed
 versions would format in a backward-incompatible way. **In any case the
 Haskell behavior is generally more sensible than the C behavior.** A brief
 summary of some key differences:
 >
 > * Haskell `printf` never uses the default "6-digit" precision used by C
 printf.
 > * Haskell `printf` treats the "precision" specifier as indicating the
 number of digits after the decimal point.
 > * Haskell `printf` prints the exponent of e-format numbers without a
 gratuitous plus sign, and with the minimum possible number of digits.
 > * **Haskell `printf` will place a zero after a decimal point when
 possible.**

 (Emphasis is mine.)

 So the `showGFloat(Alt)` functions explain the difference in behavior.
 Note that you can prevent Haskell `printf` from printing out trailing
 decimal points by using `%.0g` instead of `%g`:

 {{{
 λ> import Text.Printf
 λ> printf "%.0g\n" 2.0
 2
 }}}

 Is this explanation satisfactory? If so, feel free to close the ticket.

--
Ticket URL: <http://ghc.haskell.org/trac/ghc/ticket/13682#comment:2>
GHC <http://www.haskell.org/ghc/>
The Glasgow Haskell Compiler


More information about the ghc-tickets mailing list