unofficial mirror of bug-guile@gnu.org 
 help / color / mirror / Atom feed
* bug#16987: Bad default mantissa width for ~g format string
@ 2014-03-11 13:40 David Kastrup
  2014-03-11 15:21 ` Mark H Weaver
  0 siblings, 1 reply; 2+ messages in thread
From: David Kastrup @ 2014-03-11 13:40 UTC (permalink / raw)
  To: 16987


scheme@(guile-user)> (format #f "~g" (/ 1e100 3))
$19 = "3333333333333333000000000000000000000000000000000000000000000000000000000000000000000000000000000000.0    "

That, frankly, is pretty ludicrous.  The reserved mantissa width should
at least be constrained to the actually available precision of the data
type.  If it isn't, there is no useful way to output a variable with
minimal width.  Using ~S instead results in the much more reasonable
"3.333333333333333e99" but it will not convert fractions to a floating
point representation:

scheme@(guile-user)> (format #f "~g" (/ 10000000000000000000000000000000 3))
$22 = "3333333333333333600000000000000.0    "
scheme@(guile-user)> (format #f "~S" (/ 10000000000000000000000000000000 3))
$23 = "10000000000000000000000000000000/3"
scheme@(guile-user)> 

-- 
David Kastrup





^ permalink raw reply	[flat|nested] 2+ messages in thread

* bug#16987: Bad default mantissa width for ~g format string
  2014-03-11 13:40 bug#16987: Bad default mantissa width for ~g format string David Kastrup
@ 2014-03-11 15:21 ` Mark H Weaver
  0 siblings, 0 replies; 2+ messages in thread
From: Mark H Weaver @ 2014-03-11 15:21 UTC (permalink / raw)
  To: David Kastrup; +Cc: 16987

David Kastrup <dak@gnu.org> writes:

> scheme@(guile-user)> (format #f "~g" (/ 1e100 3))
> $19 = "3333333333333333000000000000000000000000000000000000000000000000000000000000000000000000000000000000.0    "
>
> That, frankly, is pretty ludicrous.  The reserved mantissa width should
> at least be constrained to the actually available precision of the data
> type.

Agreed.

> If it isn't, there is no useful way to output a variable with
> minimal width.  Using ~S instead results in the much more reasonable
> "3.333333333333333e99" but it will not convert fractions to a floating
> point representation:
>
> scheme@(guile-user)> (format #f "~g" (/ 10000000000000000000000000000000 3))
> $22 = "3333333333333333600000000000000.0    "
> scheme@(guile-user)> (format #f "~S" (/ 10000000000000000000000000000000 3))
> $23 = "10000000000000000000000000000000/3"
> scheme@(guile-user)> 

As a workaround, you can force decimal output with ~S by converting the
exact fraction to an inexact floating point number before passing it to
'format', using 'exact->inexact':

  scheme@(guile-user)> (format #f "~S" (exact->inexact (/ 10000000000000000000000000000000 3)))
  $4 = "3.3333333333333336e30"

Note that 'exact->inexact' works even if the number passed to it is
already inexact, in which case it's a no-op.

At some point we'll make ~g do the right thing for exact fractions, so
that the last digit printed is a 3, not 6.  For now, it can't be helped
because the value is being rounded twice: once in the conversion to
inexact binary, and again when converting from binary to decimal.

    Thanks,
      Mark





^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2014-03-11 15:21 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2014-03-11 13:40 bug#16987: Bad default mantissa width for ~g format string David Kastrup
2014-03-11 15:21 ` Mark H Weaver

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).