[GHC] #13682: When printf "%g" with trailing 0 number, decial point be printed.
#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 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: ----------------------------------------+--------------------------------- C lang's printf function doesn't print decimal point if only trailing 0s. But GHC does. {{{ Prelude Text.Printf> :m + Text.Printf Prelude Text.Printf> printf "%g\n" 2.0 2.0 }}} {{{ #include <stdio.h> int main() { printf("%g\n", 2.0); return 0; } // output: 2 }}} Is this bug? Should printf be compatible with C? -- Ticket URL: <http://ghc.haskell.org/trac/ghc/ticket/13682> GHC <http://www.haskell.org/ghc/> The Glasgow Haskell Compiler
#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 bgamari): I don't believe we make any claim that `Text.Printf.printf` will reflect the behavior of C's `printf`. -- Ticket URL: <http://ghc.haskell.org/trac/ghc/ticket/13682#comment:1> GHC <http://www.haskell.org/ghc/> The Glasgow Haskell Compiler
#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... 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
#13682: When printf "%g" with trailing 0 number, decial point be printed. -------------------------------------+------------------------------------- Reporter: nakaji_dayo | Owner: (none) Type: bug | Status: closed Priority: normal | Milestone: Component: libraries/base | Version: 8.0.1 Resolution: invalid | 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: | -------------------------------------+------------------------------------- Changes (by nakaji_dayo): * status: new => closed * resolution: => invalid Comment: Thank you for reply. Apologies for the noise. I had overlooked the document. I close this. -- Ticket URL: <http://ghc.haskell.org/trac/ghc/ticket/13682#comment:3> GHC <http://www.haskell.org/ghc/> The Glasgow Haskell Compiler
participants (1)
-
GHC