Hi all,
My name is George Erick Tasso I am new to lisp as well as this list.
Could anyone kindly tell me how to round a number to any number of
decimal places ?
5.123452 - > round to 2 decimal place would be 5.12
many thanks
<······@rbv.gov.vu> wrote in message
····························@80g2000cwy.googlegroups.com...
> Hi all,
> My name is George Erick Tasso I am new to lisp as well as this list.
> Could anyone kindly tell me how to round a number to any number of
> decimal places ?
>
> 5.123452 - > round to 2 decimal place would be 5.12
Lisp is alive and well in Vanuatu!
Here are two functions to ponder.
Carl Taylor
CL-USER 1 >
(defun round-float (f d)
(nth-value 0
(read-from-string
(format nil "~25,V,,,F" d f))))
ROUND-FLOAT
CL-USER 2 > (round-float 5.123452 2)
5.12
Or another way with some argument edit checks,
CL-USER 3 >
(defun roundup (inx n)
(assert (typep inx 'real) (inx)
"First argument <~S> s/b a real number." inx)
(assert (typep n '(integer 0 14)) (n)
"Second argument <~S> s/b an integer in {0..14}." n)
(let* ((m-factor (expt 10 n))
(a-factor (float (/ 5 (expt 10 (1+ n)))))
(result (* (signum inx)
(/ (truncate (* m-factor (+ a-factor (abs
inx))))
m-factor)))
(truncated-result (truncate result)))
(if (= result truncated-result)
truncated-result
result)))
ROUNDUP
CL-USER 4 > (compile *)
ROUNDUP
CL-USER 5 > (roundup 5.123452 2)
5.12
Carl Taylor wrote:
> Lisp is alive and well in Vanuatu!
I have checked around it seemed as if i am the only one doing LISP in
the whole country :)
>
> Here are two functions to ponder.
>
> Carl Taylor
>
>
> CL-USER 1 >
> (defun round-float (f d)
> (nth-value 0
> (read-from-string
> (format nil "~25,V,,,F" d f))))
> ROUND-FLOAT
I implemented this in my program as its easy at this stage to wrap my
head around.
Thanks
George E Tasso
<······@rbv.gov.vu> schrieb im Newsbeitrag ·····························@l12g2000cwl.googlegroups.com...
>
> Carl Taylor wrote:
>> Lisp is alive and well in Vanuatu!
>
> I have checked around it seemed as if i am the only one doing LISP in
> the whole country :)
Sometimes I think I'm one of the very few on c.l.l all over the world, but I can reassure you the dark figure is high :))
Andreas
In article <······················@bgtnsc05-news.ops.worldnet.att.net>,
"Carl Taylor" <··········@att.net> wrote:
> (defun roundup (inx n)
> (assert (typep inx 'real) (inx)
> "First argument <~S> s/b a real number." inx)
> (assert (typep n '(integer 0 14)) (n)
> "Second argument <~S> s/b an integer in {0..14}." n)
> (let* ((m-factor (expt 10 n))
> (a-factor (float (/ 5 (expt 10 (1+ n)))))
> (result (* (signum inx)
> (/ (truncate (* m-factor (+ a-factor (abs
> inx))))
> m-factor)))
> (truncated-result (truncate result)))
> (if (= result truncated-result)
> truncated-result
> result)))
Is there a reason you add A-FACTOR and then use TRUNCATE, rather than
just using ROUND in the first place?
--
Barry Margolin, ······@alum.mit.edu
Arlington, MA
*** PLEASE post questions in newsgroups, not directly to me ***
*** PLEASE don't copy me on replies, I'll read them in the group ***
On Wed, 06 Dec 2006 03:27:46 +0100, Barry Margolin <······@alum.mit.edu>
wrote:
> In article <······················@bgtnsc05-news.ops.worldnet.att.net>,
> "Carl Taylor" <··········@att.net> wrote:
>
>> (defun roundup (inx n)
>> (assert (typep inx 'real) (inx)
>> "First argument <~S> s/b a real number." inx)
>> (assert (typep n '(integer 0 14)) (n)
>> "Second argument <~S> s/b an integer in {0..14}." n)
>> (let* ((m-factor (expt 10 n))
>> (a-factor (float (/ 5 (expt 10 (1+ n)))))
>> (result (* (signum inx)
>> (/ (truncate (* m-factor (+ a-factor (abs
>> inx))))
>> m-factor)))
>> (truncated-result (truncate result)))
>> (if (= result truncated-result)
>> truncated-result
>> result)))
>
> Is there a reason you add A-FACTOR and then use TRUNCATE, rather than
> just using ROUND in the first place?
>
round and fround are a bankers round. That is if the nymber if at .5
it is rounded to the neares even number.
(round 0.5) -> 0
(round 1,5) -> 2
probaly not what you want here.
--
Using Opera's revolutionary e-mail client: http://www.opera.com/mail/
In article <·················@pandora.upc.no>,
"John Thingstad" <··············@chello.no> wrote:
> On Wed, 06 Dec 2006 03:27:46 +0100, Barry Margolin <······@alum.mit.edu>
> wrote:
>
> > In article <······················@bgtnsc05-news.ops.worldnet.att.net>,
> > "Carl Taylor" <··········@att.net> wrote:
> >
> >> (defun roundup (inx n)
> >> (assert (typep inx 'real) (inx)
> >> "First argument <~S> s/b a real number." inx)
> >> (assert (typep n '(integer 0 14)) (n)
> >> "Second argument <~S> s/b an integer in {0..14}." n)
> >> (let* ((m-factor (expt 10 n))
> >> (a-factor (float (/ 5 (expt 10 (1+ n)))))
> >> (result (* (signum inx)
> >> (/ (truncate (* m-factor (+ a-factor (abs
> >> inx))))
> >> m-factor)))
> >> (truncated-result (truncate result)))
> >> (if (= result truncated-result)
> >> truncated-result
> >> result)))
> >
> > Is there a reason you add A-FACTOR and then use TRUNCATE, rather than
> > just using ROUND in the first place?
> >
>
> round and fround are a bankers round. That is if the nymber if at .5
> it is rounded to the neares even number.
>
> (round 0.5) -> 0
> (round 1,5) -> 2
>
> probaly not what you want here.
Why not? You call this "a banker's round", but it's what I learned as
the proper way to round in grade school.
--
Barry Margolin, ······@alum.mit.edu
Arlington, MA
*** PLEASE post questions in newsgroups, not directly to me ***
*** PLEASE don't copy me on replies, I'll read them in the group ***
On Wed, 06 Dec 2006 04:06:42 +0100, Barry Margolin <······@alum.mit.edu>
wrote:
> In article <·················@pandora.upc.no>,
> "John Thingstad" <··············@chello.no> wrote:
>
>> On Wed, 06 Dec 2006 03:27:46 +0100, Barry Margolin <······@alum.mit.edu>
>> wrote:
>>
>> > In article
>> <······················@bgtnsc05-news.ops.worldnet.att.net>,
>> > "Carl Taylor" <··········@att.net> wrote:
>> >
>> >> (defun roundup (inx n)
>> >> (assert (typep inx 'real) (inx)
>> >> "First argument <~S> s/b a real number." inx)
>> >> (assert (typep n '(integer 0 14)) (n)
>> >> "Second argument <~S> s/b an integer in {0..14}." n)
>> >> (let* ((m-factor (expt 10 n))
>> >> (a-factor (float (/ 5 (expt 10 (1+ n)))))
>> >> (result (* (signum inx)
>> >> (/ (truncate (* m-factor (+ a-factor (abs
>> >> inx))))
>> >> m-factor)))
>> >> (truncated-result (truncate result)))
>> >> (if (= result truncated-result)
>> >> truncated-result
>> >> result)))
>> >
>> > Is there a reason you add A-FACTOR and then use TRUNCATE, rather than
>> > just using ROUND in the first place?
>> >
>>
>> round and fround are a bankers round. That is if the nymber if at .5
>> it is rounded to the neares even number.
>>
>> (round 0.5) -> 0
>> (round 1,5) -> 2
>>
>> probaly not what you want here.
>
> Why not? You call this "a banker's round", but it's what I learned as
> the proper way to round in grade school.
>
Well in science you round .5 and up to the next whole number.
--
Using Opera's revolutionary e-mail client: http://www.opera.com/mail/
"Barry Margolin" <······@alum.mit.edu> wrote in message
·································@comcast.dca.giganews.com...
> In article
> <······················@bgtnsc05-news.ops.worldnet.att.net>,
> "Carl Taylor" <··········@att.net> wrote:
>
>> (defun roundup (inx n)
>> (assert (typep inx 'real) (inx)
>> "First argument <~S> s/b a real number." inx)
>> (assert (typep n '(integer 0 14)) (n)
>> "Second argument <~S> s/b an integer in {0..14}." n)
>> (let* ((m-factor (expt 10 n))
>> (a-factor (float (/ 5 (expt 10 (1+ n)))))
>> (result (* (signum inx)
>> (/ (truncate (* m-factor (+ a-factor (abs
>> inx))))
>> m-factor)))
>> (truncated-result (truncate result)))
>> (if (= result truncated-result)
>> truncated-result
>> result)))
>
> Is there a reason you add A-FACTOR and then use TRUNCATE, rather than
> just using ROUND in the first place?
I wanted to assure that a 5 one decimal place beyond the desired number
of places will cause rounding up.
IIRC this was done consistently in my science/physics classes way back
when. Also this is the way the LW ~F format directive works. Does LW
differ from other Lisps in this regard?
CL-USER 3 > (format nil "~25,V,,,F" 3 5.1235452)
" 5.124"
Carl Taylor
"Carl Taylor" <··········@att.net> writes:
>
> IIRC this was done consistently in my science/physics classes way back
> when. Also this is the way the LW ~F format directive works. Does LW
> differ from other Lisps in this regard?
>
>
> CL-USER 3 > (format nil "~25,V,,,F" 3 5.1235452)
> " 5.124"
Um, this is the correct behavior because the extra decimal places are
greater than .0005.
What do you get from (format nil "~25,V,,,F" 3 5.1235000d0)?
On ACL 5.0.1 I get 5.123
--
Thomas A. Russ, USC/Information Sciences Institute
On 2006-12-06 14:41:19 -0500, ···@sevak.isi.edu (Thomas A. Russ) said:
> What do you get from (format nil "~25,V,,,F" 3 5.1235000d0)?
>
> On ACL 5.0.1 I get 5.123
Lispworks 5.0 gives:
CL-USER 2 > (format nil "~25,V,,,F" 3 5.1235000d0)
" 5.124"
On Dec 7, 4:17 am, Raffael Cavallaro
<················@pas-d'espam-s'il-vous-plait-mac.com> wrote:
> On 2006-12-06 14:41:19 -0500, ····@sevak.isi.edu (Thomas A. Russ) said:
>
> > What do you get from (format nil "~25,V,,,F" 3 5.1235000d0)?
>
> > On ACL 5.0.1 I get 5.123Lispworks 5.0 gives:
>
> CL-USER 2 > (format nil "~25,V,,,F" 3 5.1235000d0)
> " 5.124"
Which is kind of interesting as Lispworks 4.4.6 gives:
CL-USER 1 > (format nil "~25,V,,,F" 3 5.1235000d0)
" 5.123"
--
-asbjxrn
······@rbv.gov.vu writes:
> Hi all,
> My name is George Erick Tasso I am new to lisp as well as this list.
> Could anyone kindly tell me how to round a number to any number of
> decimal places ?
>
> 5.123452 - > round to 2 decimal place would be 5.12
(let ((x 5.123452)
(p 2))
(let ((e (expt 10 (- p))))
(* e (ftruncate x e))))
You can also choose fround, fceiling, or ffloor instead of ftruncate.
--
__Pascal Bourguignon__ http://www.informatimago.com/
The rule for today:
Touch my tail, I shred your hand.
New rule tomorrow.
Pascal Bourguignon wrote:
> ······@rbv.gov.vu writes:
>
> > Hi all,
> > My name is George Erick Tasso I am new to lisp as well as this list.
> > Could anyone kindly tell me how to round a number to any number of
> > decimal places ?
> >
> > 5.123452 - > round to 2 decimal place would be 5.12
>
>
> (let ((x 5.123452)
> (p 2))
> (let ((e (expt 10 (- p))))
> (* e (ftruncate x e))))
>
> You can also choose fround, fceiling, or ffloor instead of ftruncate.
>
>
> --
> __Pascal Bourguignon__ http://www.informatimago.com/
> The rule for today:
> Touch my tail, I shred your hand.
> New rule tomorrow.
To eliminate the objection above about bankers' round for
both positive and negative numbers this could be modified to:
(let ((x -5.125)
(e (expt 10 2)))
(/ (ftruncate (+ (* 0.5 (signum x)) (* e x))) e))
-- S
······@rbv.gov.vu writes:
> Hi all,
> My name is George Erick Tasso I am new to lisp as well as this list.
> Could anyone kindly tell me how to round a number to any number of
> decimal places ?
>
> 5.123452 - > round to 2 decimal place would be 5.12
Well, it depends a bit on when you want to do this.
If it is just for output purposes, then look at FORMAT and the
directives ~F ~$ ~E ~G
If you want the rounding inside calculations, you can just supply the
additional argument to ROUND (or FLOOR, CEILING, TRUNCATE) and scale
appropriately. Note that this may entail some floating point
imprecision.
Simple case: (* 0.01 (round X 0.01))
Extending this to have a simpler interface is left as an exercise...
--
Thomas A. Russ, USC/Information Sciences Institute
······@rbv.gov.vu wrote:
> Hi all,
> My name is George Erick Tasso I am new to lisp as well as this list.
> Could anyone kindly tell me how to round a number to any number of
> decimal places ?
>
> 5.123452 - > round to 2 decimal place would be 5.12
>
> many thanks
>
(defun froundn (float n)
(multiple-value-bind (rounded frac)
(ftruncate (* float (expt 10 n)))
(/ (+ rounded (if (>= frac 0.5) 1.0 0.0))
(expt 10 n))))
CL-USER 1 > (froundn 5.123452 2)
5.12
CL-USER 2 > (froundn 5.123452 4)
5.1235
CL-USER 3 >
W
Wade Humeniuk wrote:
> ······@rbv.gov.vu wrote:
> > Hi all,
> > My name is George Erick Tasso I am new to lisp as well as this list.
> > Could anyone kindly tell me how to round a number to any number of
> > decimal places ?
> >
> > 5.123452 - > round to 2 decimal place would be 5.12
> >
> > many thanks
> >
>
> (defun froundn (float n)
> (multiple-value-bind (rounded frac)
> (ftruncate (* float (expt 10 n)))
> (/ (+ rounded (if (>= frac 0.5) 1.0 0.0))
> (expt 10 n))))
>
> CL-USER 1 > (froundn 5.123452 2)
> 5.12
>
> CL-USER 2 > (froundn 5.123452 4)
> 5.1235
>
> CL-USER 3 >
>
> W
(froundn -5.1288 2)
-5.12
-- S