Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Round to the nearest whole number?

140 views
Skip to first unread message

Sungwoo, Lim

unread,
Nov 27, 2000, 3:00:00 AM11/27/00
to

Hello, I finally back. =)

Is there any function or macro which can "round to the nearest whole
number"?
For example, I want to round from the first decimals as
3.51 -> 4
3.4445 -> 3

Thanks for your help.

Sungwoo

Rainer Joswig

unread,
Nov 27, 2000, 3:00:00 AM11/27/00
to
In article <271120001453264352%sun...@cad.strath.ac.uk>, "Sungwoo,
Lim" <sun...@cad.strath.ac.uk> wrote:

? (ROUND 3.51)
4
-0.4900000000000002
? (ROUND 3.4445)
3
0.4445000000000001

--
Rainer Joswig, Hamburg, Germany
Email: mailto:jos...@corporate-world.lisp.de
Web: http://corporate-world.lisp.de/

Sungwoo, Lim

unread,
Nov 27, 2000, 3:00:00 AM11/27/00
to
In article <joswig-F9A72B....@news.is-europe.net>, Rainer
Joswig <jos...@corporate-world.lisp.de> wrote:

> In article <271120001453264352%sun...@cad.strath.ac.uk>, "Sungwoo,
> Lim" <sun...@cad.strath.ac.uk> wrote:
>
> > Hello, I finally back. =)
> >
> > Is there any function or macro which can "round to the nearest whole
> > number"?
> > For example, I want to round from the first decimals as
> > 3.51 -> 4
> > 3.4445 -> 3
> >
> > Thanks for your help.
> >
> > Sungwoo
>
> ? (ROUND 3.51)
> 4
> -0.4900000000000002
> ? (ROUND 3.4445)
> 3
> 0.4445000000000001


Oops, @.@;;;;
The answer was in my question!
Thanks, =)

Sungwoo

Andy Haywood

unread,
Nov 27, 2000, 3:00:00 AM11/27/00
to
You've got to be careful with round because if number is exactly halfway
between two integers, then it is rounded to the one that is even.

(round 2.5)
=> 2

I would want it to return 3.

Andy

Tim Bradshaw

unread,
Nov 27, 2000, 3:00:00 AM11/27/00
to
Andy Haywood <andyh...@cwcom.net> writes:

You probably wouldn't if you did lots of numerical stuff which used
ROUND, as round-upwards can lead to nasty upwards drifts in results.
There's a theorem showing why round-to-even is better by <someone> and
Knuth (or Knuth and <someone>).

(Of course `better' is not really absolute, you can compensate for the
upward drift I think, but it makes things fiddly, especially for
people (like me) who are naive about floating point).

--tim

Thomas A. Russ

unread,
Nov 27, 2000, 3:00:00 AM11/27/00
to

round

--
Thomas A. Russ, USC/Information Sciences Institute t...@isi.edu

Johan Kullstam

unread,
Nov 28, 2000, 3:00:00 AM11/28/00
to
Andy Haywood <andyh...@cwcom.net> writes:

> You've got to be careful with round because if number is exactly halfway
> between two integers, then it is rounded to the one that is even.
>
> (round 2.5)
> => 2
>
> I would want it to return 3.

for that, you can use floor on 1/2 more.

* (floor (+ 2.5 1/2))
=> 3
0.0

--
J o h a n K u l l s t a m
[kull...@ne.mediaone.net]
sysengr

Erik Naggum

unread,
Nov 28, 2000, 8:34:28 PM11/28/00
to
* Andy Haywood <andyh...@cwcom.net>

| I would want it to return 3.

If you want that, you don't want rounding, but some sort of fraudulent
scheme perpetrated by merchants and banks and other money institutions
who want to keep your round-off change. Less rebelliously, the sum of
_rounded_ values should equal the rounded sum of the unrounded values,
and if this does not hold, you have an _unmathematical_ operation.

However, you may well define your "rounding" function thus:

(defun penny-theft (number)
(multiple-value-bind (rounded rest)
(truncate (+ number .5))
(values rounded (- rest .5))))

#:Erik
--
Solution to U.S. Presidential Election Crisis 2000:
Let Texas secede from the Union and elect George W. Bush their
very first President. All parties, states would rejoice.

Espen Vestre

unread,
Nov 29, 2000, 3:00:00 AM11/29/00
to
Erik Naggum <er...@naggum.net> writes:

> If you want that, you don't want rounding, but some sort of fraudulent
> scheme perpetrated by merchants and banks and other money institutions
> who want to keep your round-off change.

The normal "0.5-rounding rule" isn't as "fraudulent" as it first seems
if you use it for both debit and credit, i.e. if the numbers are
are evenly distributed around 0.
--
(espen)

Thomas A. Russ

unread,
Nov 29, 2000, 3:00:00 AM11/29/00
to

Erik Naggum <er...@naggum.net> writes:

> ... the sum of


> _rounded_ values should equal the rounded sum of the unrounded values,
> and if this does not hold, you have an _unmathematical_ operation.

Actually, there really isn't any rounding scheme which preserves this
invariant, except in some statistical sense. For example:

(round (+ 2.2 2.2 2.2 2.2 2.2))
vs
(reduce #'= (mapcar #'round '(2.2 2.2 2.2 2.2 2.2)))

Harald Hanche-Olsen

unread,
Nov 29, 2000, 3:00:00 AM11/29/00
to
+ Erik Naggum <er...@naggum.net>:

| Less rebelliously, the sum of
| _rounded_ values should equal the rounded sum of the unrounded values,
| and if this does not hold, you have an _unmathematical_ operation.

Unmathematical in what sense?

* (let ((x 2.3) (y 2.4))
(list '= (+ (round x) (round y)) (round (+ x y))))

(= 4 5)

As a mathematician, I don't call some operations unmathematical if
they fail to commute.

Anyway, round as defined in CL has the very desirable property that if
(+ x y) happens to be an integer, *then* (+ (round x) (round y))
equals (round (+ x y)). So whether the honest merchant rounds
the value owed before computing your change or computes the change and
then rounds the result, the answer is the same.

--
* Harald Hanche-Olsen <URL:http://www.math.ntnu.no/~hanche/>
- Yes it works in practice - but does it work in theory?

Barry Margolin

unread,
Nov 29, 2000, 3:00:00 AM11/29/00
to
In article <pcoelzu...@thoth.home>,

Harald Hanche-Olsen <han...@math.ntnu.no> wrote:
>Anyway, round as defined in CL has the very desirable property that if
>(+ x y) happens to be an integer, *then* (+ (round x) (round y))
>equals (round (+ x y)). So whether the honest merchant rounds
>the value owed before computing your change or computes the change and
>then rounds the result, the answer is the same.

But just make sure that you don't buy more than two things at once!

Actually, the "merchant" that this issue often has more of an effect on is
the government, when computing sales tax. The total tax often differs
depending on whether it's computed on each item or the final total.

--
Barry Margolin, bar...@genuity.net
Genuity, Burlington, MA
*** DON'T SEND TECHNICAL QUESTIONS DIRECTLY TO ME, post them to newsgroups.
Please DON'T copy followups to me -- I'll assume it wasn't posted to the group.

Kent M Pitman

unread,
Nov 29, 2000, 9:14:16 PM11/29/00
to
Barry Margolin <bar...@genuity.net> writes:

> But just make sure that you don't buy more than two things at once!
>
> Actually, the "merchant" that this issue often has more of an effect on is
> the government, when computing sales tax. The total tax often differs
> depending on whether it's computed on each item or the final total.

Indeed. This was a big formative dilemma in my childhood. I got
$0.25 allowance per week, and comic books cost $0.12 for the small
ones or $0.25 for the large ones, with $0.13 being the point below
which tax was not assessed, so I would buy two $0.12 comic books
separately and save the penny over to buy a $0.25 comic book another
week with its unavoidable $0.01 tax. This taught me a lot about
careful fiscal management...

Tim Bradshaw

unread,
Nov 30, 2000, 3:00:00 AM11/30/00
to
t...@sevak.isi.edu (Thomas A. Russ) writes:

>
> Actually, there really isn't any rounding scheme which preserves this
> invariant, except in some statistical sense. For example:
>

The statistical sense is what matters though. You want to try stop
your numerical algorithms drifting off into the sunset if you can.

--tim

Rahul Jain

unread,
Nov 30, 2000, 3:00:00 AM11/30/00
to
In article <31844504...@naggum.net> on <31844504...@naggum.net>,
"Erik Naggum" <er...@naggum.net> wrote:

> If you want that, you don't want rounding, but some sort of fraudulent
> scheme perpetrated by merchants and banks and other money institutions
> who want to keep your round-off change. Less rebelliously, the sum of
> _rounded_ values should equal the rounded sum of the unrounded values,
> and if this does not hold, you have an _unmathematical_ operation

I found myself needing a "penny theft" rounding function when writing a
line rasterizer for a class assignment. Using the more standard rounding
procedure, the lines looked something like:

-
-
---
-
-

--
-> -\-=-=-=-=-=-=-=-=-=-/^\-=-=-=<*><*>=-=-=-/^\-=-=-=-=-=-=-=-=-=-/- <-
-> -/-=-=-=-=-=-=-=-=-=/ { Rahul -<>- Jain } \=-=-=-=-=-=-=-=-=-\- <-
-> -\- "I never could get the hang of Thursdays." - HHGTTG by DNA -/- <-
-> -/- http://photino.sid.rice.edu/ -=- mailto:rahul...@usa.net -\- <-
|--|--------|--------------|----|-------------|------|---------|-----|-|
Version 11.423.999.210020101.23.50110101.042
(c)1996-2000, All rights reserved. Disclaimer available upon request.

Harald Hanche-Olsen

unread,
Nov 30, 2000, 3:00:00 AM11/30/00
to
+ Barry Margolin <bar...@genuity.net>:

| In article <pcoelzu...@thoth.home>,
| Harald Hanche-Olsen <han...@math.ntnu.no> wrote:
| >Anyway, round as defined in CL has the very desirable property that if
| >(+ x y) happens to be an integer, *then* (+ (round x) (round y))
| >equals (round (+ x y)). So whether the honest merchant rounds
| >the value owed before computing your change or computes the change and
| >then rounds the result, the answer is the same.
|

| But just make sure that you don't buy more than two things at once!

Actually, what I wrote isn't even true, as the example x=y=2.5 shows
all too clearly.

(unless (improves (quality (usenet-posts me)))
(revert-to-lurking-mode))

cbbr...@hex.net

unread,
Nov 30, 2000, 3:00:00 AM11/30/00
to
>>>>> "Erik" == Erik Naggum <er...@naggum.net> writes:
Erik> * Andy Haywood <andyh...@cwcom.net> | I would want it to
Erik> return 3.

Erik> If you want that, you don't want rounding, but some sort of
Erik> fraudulent scheme perpetrated by merchants and banks and other
Erik> money institutions who want to keep your round-off change. Less
Erik> rebelliously, the sum of _rounded_ values should equal the
Erik> rounded sum of the unrounded values, and if this does not hold,
Erik> you have an _unmathematical_ operation.

Um, "mathematics" isn't as stringent as you seem to be indicating
here.

It would be _nice_ if the sum of the rounded values should equal the
rounding of the sum of the unrounded values; that isn't necessarily
possible.

After all, suppose we have:
(defvar values '(2.2 3.2 4.2 5.2))
and want to round to the nearest integer.

[4]> (apply #'+ values)

14.8

The reasonable rounding of that total would be to 15.

The _reasonable_ rounding of the individual values would get you
(2 3 4 5)
whose sum is 14.

I can't think of a reasonable rounding rule that would round just
_one_ of those four values up so as to satisfy your expectation that:

> (equal (our-round (apply #'+ values))
(apply #'+ (mapcar #'our-round values)))
T

I don't think that there _is_ a "mathematical operation" to correspond
to our-round.

Erik> However, you may well define your "rounding" function
Erik> thus:

Erik> (defun penny-theft (number) (multiple-value-bind (rounded
Erik> rest) (truncate (+ number .5)) (values rounded (- rest
Erik> .5))))

Providing a somewhat more elaborate set of examples:

(defparameter *sample1* '(2.2 3.2 4.2 5.2 6.2 7.2 8.2 9.2 10.2))
(defparameter *sample2*
(list (random 15.2)(random 15.2)(random 15.2)(random 15.2)(random 15.2)
(random 15.2)(random 15.2)(random 15.2)(random 15.2)(random 15.2)))

(defun rounding-naggum-penny-theft (number)

(multiple-value-bind
(rounded rest)
(truncate (+ number .5))
(values rounded (- rest .5))))

(defun round-to-even (number)
"Rounds to the nearest integer; breaks ties by rounding to a round integer."
(multiple-value-bind
(int-part fp-part)
(truncate number)
(let ((new-int (cond
((> fp-part 0.5) (+ int-part 1))
((< fp-part 0.5) int-part)
(t
(if (evenp int-part)
int-part
(+ int-part 1))))))
(values new-int (- number new-int)))))

(format t "Round-then-add gives: ~D~%Add-then-round gives: ~D~%"
(apply #'+ (mapcar #'round-to-even *sample1*))
(round-to-even (apply #'+ *sample1*)))

(format t "Round-then-add gives: ~D~%Add-then-round gives: ~D~%"
(apply #'+ (mapcar #'rounding-naggum-penny-theft *sample1*))
(rounding-naggum-penny-theft (apply #'+ *sample1*)))

(format t "Round-then-add gives: ~D~%Add-then-round gives: ~D~%"
(apply #'+ (mapcar #'round *sample1*))
(round (apply #'+ *sample1*)))

(format t "Round-then-add gives: ~D~%Add-then-round gives: ~D~%"
(apply #'+ (mapcar #'round-to-even *sample2*))
(round-to-even (apply #'+ *sample2*)))

(format t "Round-then-add gives: ~D~%Add-then-round gives: ~D~%"
(apply #'+ (mapcar #'rounding-naggum-penny-theft *sample2*))
(rounding-naggum-penny-theft (apply #'+ *sample2*)))

(format t "Round-then-add gives: ~D~%Add-then-round gives: ~D~%"
(apply #'+ (mapcar #'round *sample2*))
(round (apply #'+ *sample2*)))

; Results:
; Round-then-add gives: 54
; Add-then-round gives: 56
; Round-then-add gives: 54
; Add-then-round gives: 56
; Round-then-add gives: 54
; Add-then-round gives: 56
; Round-then-add gives: 65
; Add-then-round gives: 64
; Round-then-add gives: 65
; Add-then-round gives: 64
; Round-then-add gives: 65
; Add-then-round gives: 64

Add-then-round gives different results from Round-then-add for _ALL_
of these rounding policies. So either they're all "unmathematical,"
or perhaps your expectations are a bit too high...
--
(concatenate 'string "cbbrowne" "@hex.net")
<http://www.ntlug.org/~cbbrowne/>
DO IT -- it's easier to get forgiveness than permission.

cbbr...@hex.net

unread,
Nov 30, 2000, 3:00:00 AM11/30/00
to
>>>>> "Andy" == Andy Haywood <andyh...@cwcom.net> writes:

Andy> You've got to be careful with round because if number is
Andy> exactly halfway between two integers, then it is rounded to
Andy> the one that is even.

Andy> (round 2.5) => 2

Andy> I would want it to return 3.

It's fair enough to say that someone rounding the price of a product
after doing tax calculations might prefer to round up if, at all
possible, so as to charge the customer as much as possible.

The "round an exact-half to the nearest even value" is the convention
I've always seen in science courses, and is a good policy if you don't
want bias.
--
(concatenate 'string "cbbrowne" "@ntlug.org")
<http://www.ntlug.org/~cbbrowne/>
Rules of the Evil Overlord #15. "I will never employ any device with
a digital countdown. If I find that such a device is absolutely
unavoidable, I will set it to activate when the counter reaches 117
and the hero is just putting his plan into operation."
<http://www.eviloverlord.com/>

Robert Monfera

unread,
Nov 30, 2000, 10:11:54 PM11/30/00
to
Though banks' total assets and liabilities equal, deposits (liabilities)
are always much more fragmented, resulting in a higher number of
rounding on the liability side.

Robert

Espen Vestre

unread,
Dec 1, 2000, 3:00:00 AM12/1/00
to
Robert Monfera <mon...@fisec.com> writes:

> Though banks' total assets and liabilities equal, deposits (liabilities)
> are always much more fragmented, resulting in a higher number of
> rounding on the liability side.

yes, that would mean that in this case the statistical distribution is
'skewed', I guess. My statistics knowledge is too limited (and old) to
grasp the whole picture here, but roughly, the 'bean counter rule' will
probably be 'fair' for all symmetric distributions (which isn't the case
here) with E=0. The lisp rule will be 'fair' for a much broader class of
statistical distributions (with no _general_ limitation on the expected
value of the distribution), but still not all.
--
(espen)

Stefan Simbuerger t2002

unread,
Dec 4, 2000, 3:00:00 AM12/4/00
to
Path: uni-regensburg.de!news-nue1.dfn.de!uni-erlangen.de!newsfeeds.belnet.be!news.belnet.be!news.tele.dk!129.240.148.23!uio.no!Norway.EU.net!127.0.0.1!nobody
From: Erik Naggum <er...@naggum.net>
Newsgroups: comp.lang.lisp
Subject: Re: Round to the nearest whole number?
Date: 29 Nov 2000 01:34:28 +0000
Organization: Naggum Software; vox: +47 800 35477; gsm: +47 93 256 360; fax: +47 93 270 868; http://naggum.no; http://naggum.net
Lines: 21
Message-ID: <31844504...@naggum.net>
References: <271120001453264352%sun...@cad.strath.ac.uk> <joswig-F9A72B....@news.is-europe.net> <271120001510300782%sun...@cad.strath.ac.uk> <3A228DA5...@cwcom.net>
NNTP-Posting-Host: gate.dn.nhst.no
Mime-Version: 1.0
Content-Type: text/plain; charset=us-ascii
X-Trace: oslo-nntp.eunet.no 975464222 12922 195.0.192.66 (29 Nov 2000 02:17:02 GMT)
X-Complaints-To: newsm...@eunet.no
NNTP-Posting-Date: 29 Nov 2000 02:17:02 GMT
mail-copies-to: never
User-Agent: Gnus/5.0803 (Gnus v5.8.3) Emacs/20.7
Xref: uni-regensburg.de comp.lang.lisp:45623

* Erik Naggum


| Less rebelliously, the sum of _rounded_ values should equal the
| rounded sum of the unrounded values, and if this does not hold, you
| have an _unmathematical_ operation.

What do you mean by "unmathematical"? A function in mathematics, say f,
is usually defined as a mapping from some set S to some number field R
in a way as to assign every x from S to some f(x) from R. Note that f
does not necessarily have to be _homomorphic_ (if S has no + operation
this is clear from the outset), i.e. in general f(x+y) /= f(x)+f(y)
holds.

Stefan

Erik Naggum

unread,
Dec 4, 2000, 3:00:00 AM12/4/00
to
* Stefan Simbuerger

| What do you mean by "unmathematical"?

An exaggeration intended to be so blatantly exaggerated that it would
at least seem somewhat humorous to at least one person beside myself.
With a rounding function called "penny-theft" and words like "less
rebelliously", I'm _amazed_ that not a single soul could lighten up.

#:Erik, sighing
--
"When you are having a bad day and it seems like everybody is trying
to piss you off, remember that it takes 42 muscles to produce a
frown, but only 4 muscles to work the trigger of a good sniper rifle."
-- Unknown

Harald Hanche-Olsen

unread,
Dec 5, 2000, 3:00:00 AM12/5/00
to
+ Erik Naggum <er...@naggum.net>:

| * Stefan Simbuerger
| | What do you mean by "unmathematical"?
|
| An exaggeration intended to be so blatantly exaggerated that it would
| at least seem somewhat humorous to at least one person beside myself.
| With a rounding function called "penny-theft" and words like "less
| rebelliously", I'm _amazed_ that not a single soul could lighten up.

Well, mathematicians of course have no sense of humour, since humour
cannot be expressed in Zermelo-Fraenkel set theory. Not to mention
that humour (the good kind) is usually self-referential, and this
would tie us into some kind of Gödelian knot if we tried it.

Lispers thrive on self-reference of course, since lambda calculus has
no problems dealing with it. So they're always full of fun. Which is
why I lurk (most of the time) on this newsgroup, trying to figure out
what this humour thing is all about.

I'll bet it isn't all it's cracked up to be, though.

cbbr...@hex.net

unread,
Dec 5, 2000, 9:34:46 PM12/5/00
to
Erik Naggum <er...@naggum.net> writes:
> * Stefan Simbuerger
> | What do you mean by "unmathematical"?
>
> An exaggeration intended to be so blatantly exaggerated that it
> would at least seem somewhat humorous to at least one person
> beside myself. With a rounding function called "penny-theft" and
> words like "less rebelliously", I'm _amazed_ that not a single
> soul could lighten up.
>
> #:Erik, sighing

You think that the long presentation was intended to be
heavy-hearted?!? [Besides which, based on the pretty dramatically
different understandings of "abusive" that people have on this
newsgroup, why would it be reasonable to expect a common understanding
of humour?]

> "When you are having a bad day and it seems like everybody is trying
> to piss you off, remember that it takes 42 muscles to produce a
> frown, but only 4 muscles to work the trigger of a good sniper rifle."
> -- Unknown

Actually, _that_ quote is likely attributable to my little brother,
Brad... I don't know that he was the _first_ to say it, but say it,
I'm sure he has. Last time I went shooting, it was at the trigger of
a "good sniper rifle..."


--
(concatenate 'string "cbbrowne" "@hex.net") <http://www.ntlug.org/~cbbrowne/>

As usual, this being a 1.3.x release, I haven't even compiled this
kernel yet. So if it works, you should be doubly impressed. (Linus
Torvalds, announcing kernel 1.3.3 on the linux-kernel mailing list.)

Erik Naggum

unread,
Dec 5, 2000, 10:41:17 PM12/5/00
to
* Christopher Browne

| [Besides which, based on the pretty dramatically different
| understandings of "abusive" that people have on this newsgroup, why
| would it be reasonable to expect a common understanding of humour?]

Because it's a much simpler concept, even though both are essentially
destructive in nature and it is always hard to agree on what should be
destroyed because of the complexity of the concept of justice.. Those
who object to "abusive" out of a gut feeling do not object to "humor",
even though the latter is sometimes more destructive. E.g., nobody
has arrested that ridiculously pompous nincompoop Marcus G. Daniels
for any of his attempts at purely destrucdtive "humor", but if anyone
is abusive towards the expectation of respect for _people_ in this
forum, he's your best bet. Light-heartedness is something other than
humor, though. And lightening up is the opposite of pompousness.

#:Erik
--

Geoffrey Summerhayes

unread,
Dec 6, 2000, 12:07:36 AM12/6/00
to

"Erik Naggum" <er...@naggum.net> wrote in message
news:31850628...@naggum.net...

> And lightening up is the opposite of pompousness.

Odd. You would think being full of hot air would have a tendency to cause
lightness...

Geoff


0 new messages