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
In article <271120001453264352%sung...@cad.strath.ac.uk>, "Sungwoo,
Lim" <sung...@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
> > 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
Andy Haywood <andyhayw...@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.
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).
Andy Haywood <andyhayw...@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 [kulls...@ne.mediaone.net] sysengr
* Andy Haywood <andyhayw...@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:
#: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.
Erik Naggum <e...@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)
Erik Naggum <e...@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:
| 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.
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.
In article <pcoelzump3v....@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.
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...
In article <3184450468066...@naggum.net> on <3184450468066...@naggum.net>,
"Erik Naggum" <e...@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-j...@usa.net -\- <- |--|--------|--------------|----|-------------|------|---------|-----|-| Version 11.423.999.210020101.23.50110101.042 (c)1996-2000, All rights reserved. Disclaimer available upon request.
| In article <pcoelzump3v....@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.
>>>>> "Erik" == Erik Naggum <e...@naggum.net> writes:
Erik> * Andy Haywood <andyhayw...@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:
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.
>>>>> "Andy" == Andy Haywood <andyhayw...@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/>
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.
Espen Vestre wrote: > 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.
Robert Monfera <monf...@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)
* 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 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
| * 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.
Erik Naggum <e...@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.)
* 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 -- "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