n accuracy
- From: fireblade <slobodan.blazeski@xxxxxxxxx>
- Date: 30 May 2007 00:19:57 -0700
Yesterday I was lisping some statistics so I needed e, considering I
couldn't find it at hyperspec (too much coffein I guess) so I used
(exp 1) => 2.7182818 but author of the stat book seemed to have
different idea about some digits. So I've done this to calculate
accuracy of e , actually it's power n:
;;;; Exponent of e
;;; e is the base of the natural logarithms
;;; 1 n
;;; calculated as e =(1 + - )
;;; n
;;; or in lisp
(defun calculate-e (n)
(expt (+ 1.0 (/ 1.0 n)) n))
;;; value of e of your lisp
(defvar *implementation-e* (exp 1))
;;; Calculate the n of your lisp implementation
(defun trial (start step implementation-e)
(let ((n start)
(e 0))
(loop
(setq e (calculate-e n))
(cond ((< e implementation-e)
(incf n step))
((= e implementation-e)
(return (values n nil)))
(t
(return (values (- n step) (/ step 2))))))))
(defun find-n (implementation-e)
(let ((start 1)
(stop 1))
(loop
(multiple-value-bind (n s)
(trial start stop implementation-e)
(if (null s)
(return (* 1.0 n))
(setq start n
stop s))))))
(find-n (exp 1))
=>3017.9849
The problem is that I'm using single-float here so I'm using accuracy
very quickly (around n =1000) how to tell lisp that my numbers are
long-floats or made my program use bignums.
so I could do calculate n of numbers like 2.71828182845904523536
.
- Follow-Ups:
- Re: n accuracy
- From: Mark Hoemmen
- Re: n accuracy
- From: Edi Weitz
- Re: n accuracy
- Prev by Date: Re: how to create a binary output stream in SBCL
- Next by Date: Re: n accuracy
- Previous by thread: Macro Help: 1 macro => many sexp
- Next by thread: Re: n accuracy
- Index(es):