Loïc Grenié on Sat, 14 Sep 2024 12:32:17 +0200


[Date Prev] [Date Next] [Thread Prev] [Thread Next] [Date Index] [Thread Index]

Re: A missing (?) function: exponentfp(x)


On Sat, 14 Sep, 2024 at 11:34, Bill Allombert wrote:
On Sat, Sep 14, 2024 at 02:09:09AM -0700, Ilya Zakharevich wrote:
> The function exponent(x) gives a very coarse value of log2(x).  The
> function log(x)/log(2) gives a very fine value of log2(x) (but is
> slow, especially if x has high precision).  But sometimes I prefer to
> know log2(x) with an intermediate precision; in fact, any sufficiently
> smooth monotonic function which coincides with log2 on powers of 2
> would be enough!
>
> In particular, exponent(x) + mantissa12(x) - 1 would be OK.  (Here
> mantissa12() is the (top word of) mantissa-scaled-to-[1,2].)  It
> should be very cheap to calculate.  It may be beneficial to have two
> flavors, returning a double, or a 1-word PARI float.
>
> Is there something like this?

Why not do
? x=Pi;log(bitprecision(x,64))/log(2)
%1 = 1.6514961294723187981

(In C you can do log2(rtodbl(x)))

     In gp you can cheat

? install(dbllog2,lG);
? install(dbltor,L);
? flog2(x)=dbltor(dbllog2(x));
? my(il2=1/log(2));setrand(1);for(i=1,10^6,log(random(1.)<<4)*il2)
cpu time = 3,868 ms, real time = 3,868 ms.
? setrand(1);for(i=1,10^6,flog2(random(1.)<<4))
cpu time = 655 ms, real time = 656 ms.

      Best,

           Loïc