Lisp HUG Maillist Archive

RE: another newbie question

> -----Ursprüngliche Nachricht-----
> Von: owner-lisp-hug@lispworks.com
> [mailto:owner-lisp-hug@lispworks.com] Im Auftrag von Jacek Generowicz
> Gesendet: Freitag, 22. Juli 2005 10:14
> An: Carlos Bruguera
> Cc: lisp-hug@lispworks.com
> Betreff: Re: another newbie question
> 
> 
> 
> On Jul 22, 2005, at 8:56 AM, Carlos Bruguera wrote:
> 
> > Hello guys... I need to finish my genethic algorithm today
> and i found
> > some little problem, I'm doing:
> >
> > (+ (* 2 0.0001) 4.1)
> >
> > my interpreter prints this:
> > 4.1001997
> >
> > Why??? shouldn't it be just 4.1002 ?
> 
> The accuracy of floating point numbers is limited. For example, when
> you evaluate 0.0001 you get a number which is close to 0.0001, but  
> not exacly 0.0001.
> 
> Google for
> 
>      floating point computation
> 
> or
> 
>      what every computer scientist should know about floating point
> 

I second this.

Perhaps something like

(format nil "~8,4f" (+ (* 2 1.0001) 4.1))

is helpful.

Ooops, I just wandered about the use of lisps ROUND? Perhaps anybody can
give a hint on the idiomatic use of ROUND.

You can use the reader to force a certain number of digits:

(read-from-string (format nil "~8,4f" (+ (* 2 1.0001) 4.1)))

You can use this to define a helping function

(defun dround (num n)
  "round num to n digits"
  (let ((format-string (concatenate 'string "~," (format nil "~a" n)
"f")))
    (read-from-string (format nil format-string num))))

not very elegant, just a fast hack :))


Andreas




Re: another newbie question

"Andreas Thiele" <andreas@atp-media.de> writes:

> (defun dround (num n)
>   "round num to n digits"
>   (let ((format-string (concatenate 'string "~," (format nil "~a" n)
> "f")))
>     (read-from-string (format nil format-string num))))
> 
> not very elegant, just a fast hack :))
> 
> 
> Andreas

or 

(defun dround (num x)
  (let ((by (expt 10 x)))
    (float (/ (floor num (/ 1 by)) by)))) 

* (dround (+ (* 2 1.0001) 4.1) 2)
=> 6.1



Sean

-- 
"My doctor says that I have a malformed public-duty gland and a
 natural  deficiency in moral fibre," he muttered to himself, "and
 that I am therefore excused from saving Universes."
 - Life, the Universe, and Everything     Douglas Adams.


 Confidentiality Notice:
 The above message and all attachments may contain privileged and confidential information intended only for the
 person or entity to which it is addressed. Any review, retransmission, dissemination, copy or other use of, or
 taking of any action in reliance upon this information by persons or entities other than the intended recipient is
 prohibited. If you received this message in error, please notify the sender immediately by e-mail, facsimile or
 telephone and thereafter delete the material from your computer. Any views expressed in this message are those of
 the individual sender, except where the sender specifically states them to be the view of the entity transmitting
 the message.UCS Group Limited and all of its subsidiary companies hereby distance themselves from and accept no
 liability in respect of the unauthorised use of its e-mail facility or the sending of e-mail communications for
 other than strictly business purposes


Updated at: 2020-12-10 08:51 UTC