(log (fac 1000) 10)
On SBCL, this returns properly:
2567.6047
On LispWorks (which is my Lisp of choice), I sadly get returned:
+1F++0 #| +1F++0 is single-float plus-infinity |#
Just wondering if anyone is aware of either (a) another way to get
this (been a long time since college) or (b) some way of getting
LispWorks to not choke? ;-)
Thanks!
Jeff M.
On Jun 13, 3:34 pm, "Jeff M." <·······@gmail.com> wrote:
> (log (fac 1000) 10)
> On SBCL, this returns properly:
> 2567.6047
> On LispWorks (which is my Lisp of choice), I sadly get returned:
> +1F++0 #| +1F++0 is single-float plus-infinity |#
> Just wondering if anyone is aware of either (a) another way to get
> this (been a long time since college)
Well, you can also just calculate it by using the fact that log ab =
log a + log b:
* (loop :for x :from 1d0 :to 1000d0 :summing (log x 10))
2567.6046442221304d0
This has the advantage of being pretty accurate (not perfect because
of the vagaries of adding floating point numbers, but pretty
accurate).
If you can tolerate a little less accuracy, there's also Stirling's
approximation, which will do a good job for ln (n!) if n >> 1.
http://mathworld.wolfram.com/StirlingsApproximation.html
* (defun stirling (x)
(+ (* (+ x 0.5) (log x)) (- x) (/ (log (* 2 pi)) 2)))
STIRLING
* (stirling 1000)
5912.128411189455d0
* (/ * (log 10))
2567.60470962732d0
Cheers,
Pillsy
"Jeff M." <·······@gmail.com> writes:
> (log (fac 1000) 10)
>
> On SBCL, this returns properly:
> 2567.6047
>
> On LispWorks (which is my Lisp of choice), I sadly get returned:
> +1F++0 #| +1F++0 is single-float plus-infinity |#
>
> Just wondering if anyone is aware of either (a) another way to get
> this (been a long time since college)
integer-length = ceiling log2
so (/ (1- (integer-length x)) (log 10 2)) <= (log x 10) < (/ (integer-length x) (log 10 2))
(/ (integer-length (ext:! 1000)) (log 10 2)) --> 2567.786
And since I don't see any reason to get the log10 of a number other
than to know the number of base-ten digits needed to display it, the
above expression must be precise enough for this purpose.
> (b) some way of getting LispWorks to not choke? ;-)
--
__Pascal Bourguignon__