Bill McCarthy wrote:
> On Mon 23-Jun-08 11:19am -0600, Ben Schmidt wrote:
>
> You gave the following example from my missing email:
>
>> :" Inaccurate due to underflow
>> :echo PG(1.0e-323)
>> 9.88131291682e-324
>> :" Negative zero incorrect
>> :echo PG(-0.0)
>> 0
>> :" Too many digits
>> :echo PG(999999999999.5)
>> 1000000000000
>
> You then described a new 150 line alternative which gave
> these results:
>
>> :echo PrintFloat(1.0e-323)
>> 0.1e-322
>> :echo PrintFloat(-0.0)
>> -0.0
>> :echo PrintFloat(999999999999.5)
>> 1.0e12
>
> I made a small change to my PG() function so it no longer
> relies on log10().
Mmm. That will help.
> It appears to work quite well. Nonetheless, two of your three examples
> don't work on my machine.
>
> 1.0e-323 is below DBL_MIN and evaluates to zero. Typing
> echo 1.0e-323 yields 0.0. However:
>
> :echo PG(2.225074e-308)
> 2.225074e-308
> :echo PG(2.225073e-308)
> 0
>
> Your compiler's print utilities are apparently using
> denormalized doubles.
Yes. And so they should. It's a bit odd that yours don't. Of course,
this means that in essence, the maximum precision you can get is 1e-307,
i.e. a bunch of those digits are bogus. Nasty. Not that we use such
numbers all that regularly, of course, but still...
I think I'm going to put back the 16 digit precision into mine, too (I
decided to take it out as I was writing it), and possibly add an option
to allow the extra digit of precision down in the 1e-324 area, too (or
perhaps just turn that on whenever the precision is 16). That final
digit is partially represented in a double, so I should let the user
choose whether they want to display it or not; then there is the option
to display truly maximum precision, not just maximum decimal precision,
which eliminates the problem of 'these floats look equal but == doesn't
say they are' if the user so desires.
Unfortunately, these technical system-dependent troubles add weight to
the argument to just use printf in Vim rather than a specialised routine.
Or something really simple, e.g. just use %.12g and remove zeros between
the decimal place and immediately before e or end of string.
substitute(printf("%.11g",0.003333e15),
\ '[EMAIL PROTECTED](e\|$\)','','')
On the other hand, I could pretty easily get my routine to work if we
add a test to ./configure to figure out what the floating point lower
limit truly is.
> Please let me know if the new PG()
> produces strange results for 1.0e-323.
:echo PG(1.0e-323)
9.88131291682e-324
> In Vim, I can't seem to get a -0.0 result. Likewise,
> :echo PG(-0.0)
> 0
O, at least that's an easy one. It's because you're ensuring you have a
float by doing a:x + 0.0, and -0.0 + 0.0 gives 0.0. You have to ensure
you do the float conversion only if it isn't a float already so you
don't lose this precious info.
> Now that I no longer using log10 to determine magnitudes:
>
> :echo PG(999999999999.5)
> 1e+12
> :echo PG(99999999999.5)
> 99,999,999,999.5
>
> The updated version of PG.vim is attached. Hopefully it
> will post this time :-)
>
> I plan to spend some time with your attachment this weekend.
Cool. It is a bit of a monster, I'm afraid, particularly as I took a
C-like approach rather than using regexes. But that's also one of its
advantages: it can be easily translated into C and work efficiently. It
also does quite a lot more than yours does in terms of being able to
display truly accurate scientific notation, and handle the (well, near-)
underflow case, so that contributes to the length.
It would be interesting to get some timings on them. In C mine would
kill yours, but in Vim I suspect yours will outpace mine due to the
overhead of parsing the Vimscript line by line.
Yours is easier to maintain and add features to, as well, and with a
quick test anyway, I can't seem to spot any additional deficiencies.
I would like to play with it more, but that will have to wait until the
weekend.
Cheers,
Ben.
--~--~---------~--~----~------------~-------~--~----~
You received this message from the "vim_dev" maillist.
For more information, visit http://www.vim.org/maillist.php
-~----------~----~----~----~------~----~------~--~---