On Sun 22-Jun-08 2:49pm -0600, Tony Mechelynck wrote:

> On 21/06/08 13:56, Bram Moolenaar wrote:

>> Ben Schmidt wrote:

>>> - Furthermore, on investigation, it seems there is an interesting
>>>     definition of 'significant figures' in the library docs, and %.15g is
>>>     the correct value, not %.16g. It might be worth noting at the printf()
>>>     docs, or the Float docs where precision is mentioned, or both, that
>>>     maximum precision output can be obtained with %.15g.
>>
>> I don't see this, for me %.16g does produce a longer result.
> [...]
>
> A higher number prints additional digits, but starting at some point,
> the additional digits are bogus. Example:
>
>         :echo printf('%.25g', 1.0/3.0)
> 0.3333333333333333148296163
>
> Anything after the row of threes is bogus.
>
>         :echo printf('%.25g', 10.0/9.0)
> 1.1111111111111111604543567
>
> Here anything after the ones is bogus. And so on. These examples seem to
> imply that, in these cases at least, %.16g is the maximum valid precision.

Yes, the only way to have as many as, say, 15 significant
digits but as few as possible more than 1 is to use a
function:

Bram, I would like to see something like this representation
be the default for :echo float_expression.

==========================================
" Do a printf with %g printing as many as 15 significant
" digits but as few as possible more than 1.

function! PG(x)
  let x = a:x + 0.0
  let y = x < 0 ? -x : x " No fabs() in official release
  let n = x == 0.0 ? 0 : float2nr(floor(log10(y)))+1

  if n < -1 || n > 15
    return substitute(substitute(printf("%0.14e",x),
      \'\.\=0*e','e',''),'\([eE][+-]\)0*\([0-9]\+\)','\1\2','')
  else
    return substitute(substitute(printf("%0.*f", 15-n, x),
      \'\([^.]*\..\{-}\)0\+$','\1',''),'\.$','','')
  endif
endfunction
==========================================

Examples:

    :echo PG(0.012)
    0.012
    :echo PG(-0.00123)
    -1.23e-3
    :echo PG(7/4.0)
    1.75
    :echo PG(123456789012345.0)
    123456789012345
    :echo PG(1.0e20)
    1e+20

-- 
Best regards,
Bill


--~--~---------~--~----~------------~-------~--~----~
You received this message from the "vim_dev" maillist.
For more information, visit http://www.vim.org/maillist.php
-~----------~----~----~----~------~----~------~--~---

Reply via email to