From: cha
Subject: Round off in AutoLISP
Date: 
Message-ID: <8b18sm$o4e$1@news.inet.tele.dk>
Hej

Can anyone help my - I need a funktion to "round off" in autoLISP

Eksembel   1.38  return 1.25
                    1.55  return 1.50  etc.


Carsten H. Andersen

Denmark

········@post2.tele.dk

From: Coby Beck
Subject: Re: Round off in AutoLISP
Date: 
Message-ID: <mYXA4.4314$eh.553178@news.bc.tac.net>
cha <········@post2.tele.dk> wrote in message
·················@news.inet.tele.dk...
> Hej
>
> Can anyone help my - I need a funktion to "round off" in autoLISP
>
> Eksembel   1.38  return 1.25
>                     1.55  return 1.50  etc.
>

(defun round-to-nearest (n factor)
   (multiple-value-bind (quotient remainder)
       (round n factor)
      (values (* factor quotient) remainder)))

> (round-to-nearest 1.38 .25)
1.5
-0.120000005
> (round-to-nearest 1.55 .25)
1.5
0.049999952
>

But that didn't give your desired results...how about:


(defun truncate-to-nearest (n factor)
   (multiple-value-bind (quotient remainder)
       (truncate n factor)
      (values (* factor quotient) remainder)))

> (truncate-to-nearest 1.38 .25)
1.25
0.13
> (truncate-to-nearest 1.55 .25)
1.5
0.049999952
>

Coby

ps - If you know you'll *never* care about the second value, you can just
use:
(defun truncate-to-nearest (n factor)
    (* factor (truncate n factor)))
From: Marc Battyani
Subject: Re: Round off in AutoLISP
Date: 
Message-ID: <83A313015345D13C.03118260A37CCB8D.F77B9A6B96614C84@lp.airnews.net>
Coby Beck <·····@mercury.bc.ca> wrote in message
·························@news.bc.tac.net...
> (defun round-to-nearest (n factor)
>    (multiple-value-bind (quotient remainder)
>        (round n factor)
>       (values (* factor quotient) remainder)))

> (defun truncate-to-nearest (n factor)
>    (multiple-value-bind (quotient remainder)
>        (truncate n factor)
>       (values (* factor quotient) remainder)))

In Common Lisp, since it seems that you have floats arguments and want to
get back floats you should use FROUND and FTRUNCATE  instead of ROUND and
TRUNCATE.
This will avoid having an integer result that will be converted back to a
float when you multiply it.

CL-USER 2 > (* 0.25 (fround 1.38 0.25))
1.5

CL-USER 3 > (* 0.25 (ftruncate 1.38 0.25))
1.25

this can be important if the intermediate value does not fits in a fixnum.

Marc Battyani
From: Coby Beck
Subject: Re: Round off in AutoLISP
Date: 
Message-ID: <Ss9B4.4371$eh.564912@news.bc.tac.net>
Marc Battyani <·············@csi.com> wrote in message
·······················································@lp.airnews.net...
>
> Coby Beck <·····@mercury.bc.ca> wrote in message
> ·························@news.bc.tac.net...
> > (defun round-to-nearest (n factor)
> >    (multiple-value-bind (quotient remainder)
> >        (round n factor)
> >       (values (* factor quotient) remainder)))
>
> > (defun truncate-to-nearest (n factor)
> >    (multiple-value-bind (quotient remainder)
> >        (truncate n factor)
> >       (values (* factor quotient) remainder)))
>
> In Common Lisp, since it seems that you have floats arguments and want to
> get back floats you should use FROUND and FTRUNCATE  instead of ROUND and
> TRUNCATE.

Thanks for that advice!

Here's a more general solution to the original problem:
(defun round-to-nearest (num factor &optional (dir :nearest))
   "rounds off 'num' in increments of 'factor' as specified by optional dir
(:up, :down or :nearest)"
   (let ((rounding-function (cond  ((equal dir :up) 'fceiling)
                                                 ((equal dir :down)
'ftruncate)
                                                 (t 'fround))))
      (multiple-value-bind (quotient remainder)
          (funcall rounding-function num factor)
         (values (* factor quotient) remainder))))

> (round-to-nearest 1.38 .25)
1.5
-0.120000005
> (round-to-nearest 1.38 .25 :down)
1.25
0.13
> (round-to-nearest 1.01 .25 :up)
1.25
-0.24000001
>
Any other comments/advice is appreciated.

Coby
From: Marc Battyani
Subject: Re: Round off in AutoLISP
Date: 
Message-ID: <A77D52B7B39722C0.47FE0DCD3B54B2EE.9898C9ABC3649DCA@lp.airnews.net>
Coby Beck <·····@mercury.bc.ca> wrote in message news:Ss9B4.4371
>
> Here's a more general solution to the original problem:
> (defun round-to-nearest (num factor &optional (dir :nearest))
>    "rounds off 'num' in increments of 'factor' as specified by optional
dir
> (:up, :down or :nearest)"
>    (let ((rounding-function (cond  ((equal dir :up) 'fceiling)
>                                                  ((equal dir :down)
> 'ftruncate)
>                                                  (t 'fround))))
>       (multiple-value-bind (quotient remainder)
>           (funcall rounding-function num factor)
>          (values (* factor quotient) remainder))))
.../...
> Any other comments/advice is appreciated.

Here are some:
1 Don't use COND for this, use CASE as you want to select something based on
a constant values.
2 Don't use equal to compare keywords use eq
3 The usage in CL is the have an optional or keyed arg with a default value
for choosing the function to use:
(defun decimal-round (num factor &optional (round-func #'fround))
    (* factor (funcall round-func num factor))

Marc Battyani
From: Martti Halminen
Subject: Re: Round off in AutoLISP
Date: 
Message-ID: <38D6396E.201888A8@solibri.com>
> Coby Beck <·····@mercury.bc.ca> wrote in message news:Ss9B4.4371

> > Here's a more general solution to the original problem:
> > (defun round-to-nearest (num factor &optional (dir :nearest))
> >    "rounds off 'num' in increments of 'factor' as specified by optional
> dir
> > (:up, :down or :nearest)"
> >    (let ((rounding-function (cond  ((equal dir :up) 'fceiling)
> >                                                  ((equal dir :down)
> > 'ftruncate)
> >                                                  (t 'fround))))
> >       (multiple-value-bind (quotient remainder)
> >           (funcall rounding-function num factor)
> >          (values (* factor quotient) remainder))))
> .../...
> > Any other comments/advice is appreciated.





Just a note in passing for this thread: the original question
was about AutoLISP, which is quite different from Common Lisp:
the AutoLisp manual is about the size of CLtL2's index...
So, trying to run this stuff in AutoCAD might be a little
difficult.
- dynamic scope
- no optional parameters
- no multiple values
etc.

Generally, a better place for AutoLISP -specific questions would
be comp.cad.autocad,
the people there are usually more familiar with its
restrictions.







--
From: Friedrich Dominicus
Subject: Re: Round off in AutoLISP
Date: 
Message-ID: <38D475EB.D539F75F@inka.de>
cha wrote:
> 
> Hej
> 
> Can anyone help my - I need a funktion to "round off" in autoLISP
> 
> Eksembel   1.38  return 1.25
>                     1.55  return 1.50  etc.

what is that for a rounding? how can one round from 1.38 -> 1.25 and
from
1.55 -> 1,50

My wild guess is you want to have 0.25 steps downwards  then you have to
write it yourself I guess.

Regards
Friedrich
From: nirsul
Subject: Re: Round off in AutoLISP
Date: 
Message-ID: <ibQB4.2550$0o4.25371@iad-read.news.verio.net>
This is a multi-part message in MIME format.

------=_NextPart_000_0053_01BF937C.6B994DA0
Content-Type: text/plain;
	charset="windows-1255"
Content-Transfer-Encoding: quoted-printable

Go to our website , In the Programming Link you will find the FIXX =
function.

http://www.actcom.co.il/sysoft



Yours=20

Nir Sullam
anySOFT

------=_NextPart_000_0053_01BF937C.6B994DA0
Content-Type: text/html;
	charset="windows-1255"
Content-Transfer-Encoding: quoted-printable

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML><HEAD>
<META content=3D"text/html; charset=3Dwindows-1255" =
http-equiv=3DContent-Type>
<META content=3D"MSHTML 5.00.2014.210" name=3DGENERATOR>
<STYLE></STYLE>
</HEAD>
<BODY>
<DIV><FONT face=3D"Arial Narrow" size=3D2>Go to our website , In the =
Programming=20
Link you will find the FIXX function.</FONT></DIV>
<DIV>&nbsp;</DIV>
<DIV><FONT face=3D"Arial Narrow" size=3D2><A=20
href=3D"http://wwwsers.actcom.co.il/sysoft">http://www.actcom.co.il/sysof=
t</A></FONT></DIV>
<DIV>&nbsp;</DIV>
<DIV>&nbsp;</DIV>
<DIV>&nbsp;</DIV>
<DIV><FONT face=3D"Arial Narrow" size=3D2>Yours </FONT></DIV>
<DIV>&nbsp;</DIV>
<DIV><FONT face=3D"Arial Narrow" size=3D2>Nir Sullam</FONT></DIV>
<DIV><FONT face=3D"Arial Narrow" =
size=3D2>anySOFT</FONT></DIV></BODY></HTML>

------=_NextPart_000_0053_01BF937C.6B994DA0--