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

Rounding 0.5

429 views
Skip to first unread message

Dale Berry

unread,
Feb 10, 2000, 3:00:00 AM2/10/00
to
To always round up try this function:

function RoundUp(x:Extended) : extended;
begin
result := Trunc(x) + Trunc( Frac(x) * 2);
end;

Michael Foot wrote in message <38A33F8D...@voyager.co.nz>...
>Apologies if this has been covered before.
>
>Could someone please tell me why Round() rounds a number that has 0.5 as
>the fraction part down? I would have though it should be rounded up.
>
>ie.
>
>ShowMessage(FloatToStr(Round(50.5))); //returns 50
>
>
>Thanks,
>
>Mike.
>

Steve Zimmelman

unread,
Feb 10, 2000, 3:00:00 AM2/10/00
to
It's a "feature"...

Here's a small snippet from the Help file:

Description

The Round function rounds a real-type value to an integer-type value.

X is a real-type expression. Round returns an Int64 value that is the value
of X rounded to the nearest whole number. If X is exactly halfway between
two whole numbers, the result is always the even number.

-Steve-


"Michael Foot" <mi...@voyager.co.nz> wrote in message
news:38A33F8D...@voyager.co.nz...


Apologies if this has been covered before.

Could someone please tell me why Round() rounds a number that has 0.5 as
the fraction part down? I would have though it should be rounded up.

ie.

ShowMessage(FloatToStr(Round(50.5))); file://returns 50


Thanks,

Mike.


John Herbster

unread,
Feb 10, 2000, 3:00:00 AM2/10/00
to
Michael, Also look up floor and ceil functions.
They are in the Math unit. Regards, John H

Jeff Overcash (TeamB)

unread,
Feb 10, 2000, 3:00:00 AM2/10/00
to
Actually the help file was still wrong in D4 IIRC. D5's finally got it
correct.

Michael Foot wrote:
>
> Thanks everyone.
>
> I'm still using Delphi 3 which has the following in it's help file.
>
> X is a real-type expression. Round returns a Longint value that is the value of


> X rounded to the nearest whole number. If X is exactly halfway between two

> whole numbers, the result is the number with the greatest absolute magnitude.
>
> This is obviously wrong. It looks like the help file has been "tweeked" in
> Delphi 4 to convey the correct meaning.
>
> Mike.
>

--
Jeff Overcash (TeamB) On waves of silver I dreamed of gold
(Please do not email 'Till I lost the peace that dreaming gives
me directly unless I dreamed of the moment of my own death
asked. Thank You) That no one ever dreams and lives (Marillion)

Michael Foot

unread,
Feb 11, 2000, 3:00:00 AM2/11/00
to
Apologies if this has been covered before.

Could someone please tell me why Round() rounds a number that has 0.5 as
the fraction part down? I would have though it should be rounded up.

ie.

ShowMessage(FloatToStr(Round(50.5))); //returns 50


Thanks,

Mike.


Dave

unread,
Feb 11, 2000, 3:00:00 AM2/11/00
to
Michael Foot <mi...@voyager.co.nz> wrote in message
news:38A33F8D...@voyager.co.nz...
> Apologies if this has been covered before.
>
> Could someone please tell me why Round() rounds a number that has 0.5 as
> the fraction part down? I would have though it should be rounded up.
>
It doesnt always round it down. It rounds it to the closest *even* number.

ie
Round(50.5) returns 50
Round(51.5) returns 52

Read the help on Round.

Michael Foot

unread,
Feb 11, 2000, 3:00:00 AM2/11/00
to
Thanks everyone.

I'm still using Delphi 3 which has the following in it's help file.

X is a real-type expression. Round returns a Longint value that is the value of
X rounded to the nearest whole number. If X is exactly halfway between two
whole numbers, the result is the number with the greatest absolute magnitude.

This is obviously wrong. It looks like the help file has been "tweeked" in
Delphi 4 to convey the correct meaning.

Mike.

Steve Zimmelman wrote:

> It's a "feature"...
>
> Here's a small snippet from the Help file:
>
> Description
>
> The Round function rounds a real-type value to an integer-type value.
>

> X is a real-type expression. Round returns an Int64 value that is the value


> of X rounded to the nearest whole number. If X is exactly halfway between

> two whole numbers, the result is always the even number.


Dr J D Collins

unread,
Feb 11, 2000, 3:00:00 AM2/11/00
to

> Could someone please tell me why Round() rounds a number that has
0.5 as
> the fraction part down? I would have though it should be rounded up.
>
> ie.
>
> ShowMessage(FloatToStr(Round(50.5))); file://returns 50
>

Rounding is really only for real numbers where no previous rounding
has occurred. In such a situation the probability of getting a value
which is exactly 0.5 is zero so whether you round up or down is of no
practical consequence. However if all your original numbers are exact
to one decimal place in a sense you have already rounded values. Thus
values of 0.5 will be obtained by rounding down half the time and
rounding up half the time so you should do the same thing when
rounding 0.5 to avoid biassing values upwards.

Regards
John Collins


Arthur E.F.Heinrich

unread,
Feb 11, 2000, 3:00:00 AM2/11/00
to
Dr J D Collins escreveu na mensagem <880144$d4...@bornews.borland.com>...

>
>Rounding is really only for real numbers where no previous rounding
>has occurred. In such a situation the probability of getting a value
>which is exactly 0.5 is zero so whether you round up or down is of no
>practical consequence.

I disagree with your probability. All the odd numbers when divided by 2
generate numbers with exact representation in base 2.

The round function round to the nearest even number to reduce the error.
To roundup like in financial applications, you can use other methods.

To round to float type, use RoundedNumber := Int(Number + 0.5);
To round to integer type ise RoundedNumber := Trunc(Number + 0.5);

Arthur

John Herbster

unread,
Feb 11, 2000, 3:00:00 AM2/11/00
to
Arthur, I believe that the good doctor was refering to
"real" numbers in the sense of representing measurements
on a "continuous" scale with a random distribution.
Best regards, John H.

Dr J D Collins

unread,
Feb 12, 2000, 3:00:00 AM2/12/00
to
> >Rounding is really only for real numbers where no previous rounding
> >has occurred. In such a situation the probability of getting a
value
> >which is exactly 0.5 is zero so whether you round up or down is of
no
> >practical consequence.
>
> I disagree with your probability. All the odd numbers when divided
by 2
> generate numbers with exact representation in base 2.
>
There are an infinite number of real numbers (real in the mathematical
sense) between 0 and 1 so the probability of any particular number is
zero. In real life numbers generally are restricted to a discrete list
(real numbers are either rounded or truncated to some fixed length).
This clearly alters the probability of getting 0.5. The more you
truncate or round to get shorter lengths the higher the probability of
getting 0.5. If you have measurements of real numbers to 10 decimal
places then how you round 0.5 has little effect. If however it is only
possible to get measurements to the nearest 0.5 (eg 1, 1.5, 2, 2.5
etc) then rounding the conventional way could have a dramatic effect.
Ideally in such a situation, if you are going to round, you should
randomly choose whether to round up or round down. Provided even and
odd numbers are equally likely then the Round function will provided a
reasonable alternative.

Often you do not have measurements on real numbers but rather
measurements which take particular exact discrete values. In such a
case where 0.5 does not mean round about 0.5 as you would get say
measuring with a ruler but means exactly 0.5 then the whole question
of whether rounding should be done at all needs to be looked at. If
you have a situation where exactly 0.5 has a high probability of
occurring you need to remember that a computer calculation will almost
never produce exactly 0.5 (ie 0.50000000......) so the Round function
may never have its intended effect for 0.5 in any case. Another
important factor in relation to rounding is the relative accuracy of
the measurement. If at best you cannot get better than say + or - 10
even thinking about having fractions in the answer is nonsense.

As a consequence people doing rounding need to understand the sorts of
measurements/numbers/calculations they are dealing with (and the way
computers store numbers) in order to decide whether rounding is
appropriate or inappropriate. Often when rounding is mandatory you
will find the ridiculous situation where people do not round and give
every significant figure as though by doing this they are adding some
additional accuracy to the figures. Quoting say 2.467345 instead of
2.5 for annual inflation is a nonsense since even this latter figure
probably only indicates a real figure in the range of 2 to 3. People
like to look at the effectiveness of road safety and if the fatalities
drop from 21 to 15 on a holiday weekend there is much congratulations
and backslapping for the success of a campaign whilst if the figure
rises from 21 to 30 then the public is admonished for ignoring all the
warnings. If you understand probabilities for such situations then you
will know that in reality 15 means around 7 to 22, 21 means around 12
to 30 and 30 means 19 to 41 so there is no real reason to decide that
the changes were any more than chance.

Regards
John Collins


Henrick Hellström

unread,
Feb 13, 2000, 3:00:00 AM2/13/00
to

> Could someone please tell me why Round() rounds a number that has 0.5 as
> the fraction part down? I would have though it should be rounded up.

Rounding 2*n + 0.5 to 2*n, and 2*n + 1.5 to 2*(n + 1), is a mathematical
convention. Pascal was originally designed as mathematical tool, so I see no
reason why this legacy should not be carried on

The rationale behind this mathematical convention has already been explained
by Dr J D Collins.

Paul Ferrara

unread,
Feb 13, 2000, 3:00:00 AM2/13/00
to
As others have pointed out, it rounds to the nearest even whole number. It
works that way because Delphi simply uses Intel's rounding function and
that's the way Intel did it.

To get the result that one would expect for financial calculations, for
example, you'd need to use something like:

Function CurrencyRound(V : Double) : Double;
begin
if v > 0.0 then
Result := Trunc(V * 100 + 0.5) / 100
else
Result := Trunc(V * 100 - 0.5) / 100;
end;

Paul/ColumbuSoft


Michael Foot <mi...@voyager.co.nz> wrote in message
news:38A33F8D...@voyager.co.nz...
> Apologies if this has been covered before.
>

> Could someone please tell me why Round() rounds a number that has 0.5 as
> the fraction part down? I would have though it should be rounded up.
>

> ie.
>
> ShowMessage(FloatToStr(Round(50.5))); file://returns 50
>
>

> Thanks,
>
> Mike.
>

Henrick Hellström

unread,
Feb 13, 2000, 3:00:00 AM2/13/00
to
> As others have pointed out, it rounds to the nearest even whole number.
It
> works that way because Delphi simply uses Intel's rounding function and
> that's the way Intel did it.

You make it look like Intels choice of rounding function was more or less
arbitrary. As others have pointed out, it is not. The choice complies with
mathematical convention.

In fact, had they choosen any other rounding function, that choice would
have been more or less arbitrary. How fractions are rounded in financial
transactions depends on many thing: If I go to the nearest supermarket here
in Sweden, they are likely to round exactly the way the Round function does.
When I pay my taxes, however, all incomes from interest are rounded down to
the nearest integer equal to 0 or greater than 100.

Tony Nasca

unread,
Feb 13, 2000, 3:00:00 AM2/13/00
to
Here is round function that I created to solve the problem of rounding..

Other tools like xcurrency:=strtofloat(format('%12.2f',[100.005])) does not
always return proper results.

This may be extravagant but it works. xcurrency:=roundnumber(100.005,2)

returns 100.01.

function roundnumber(i:double;l:integer):currency;
var
sfld,sInt,sFloat,sPartial:string;
begin
sfld:='%12.'+inttostr(l)+'f';
sFloat:=currtostr(i);
if pos('.',sFloat)>0 then begin
if length(copy(sfloat,pos('.',sFloat)+1,l+1))>l then begin
sPartial:=copy(sFloat,1,pos('.',sFloat)+l-1);

sInt:=copy(sFloat,pos('.',sFloat)+l,1)+'.'+copy(sFloat,pos('.',sFloat)+l+1,1
);
if round(strtocurr(sInt))>9 then
spartial:=currtostr(strtocurr(spartial)+0.1)
else
spartial:=spartial+inttostr(round(strtocurr(sInt)));
sfld:=format(sfld,[strtocurr(spartial)]);
end
else sfld:=format(sfld,[i]);
end
else
sfld:=format(sfld,[i]);
result:=strtocurr(sfld);
end;


Dr J D Collins <jcol...@acay.com.au> wrote in message
news:880144$d4...@bornews.borland.com...


>
> > Could someone please tell me why Round() rounds a number that has
> 0.5 as
> > the fraction part down? I would have though it should be rounded up.
> >
> > ie.
> >
> > ShowMessage(FloatToStr(Round(50.5))); file://returns 50
> >
>

> Rounding is really only for real numbers where no previous rounding
> has occurred. In such a situation the probability of getting a value
> which is exactly 0.5 is zero so whether you round up or down is of no

Paul Ferrara

unread,
Feb 13, 2000, 3:00:00 AM2/13/00
to
Supermarkets in Sweden are a lot different than those in the US then. In
fact, I've never seen a business, including banks, that used anything other
than 5/4 rounding. I'd be very surprised if you're correct about Swedish
supermarkets. It does sound like you haven't actually checked it.

Paul

Henrick Hellström <henrick....@telia.com> wrote in message
news:886pju$gt...@bornews.borland.com...

Arthur E.F.Heinrich

unread,
Feb 13, 2000, 3:00:00 AM2/13/00
to
Hi.

In the system unit there is a procedure called Set8087CW. This procedure
changes the numeric processor control variable which controls many things
like precision and rounding mode.

Is there any one who knows if this routine can be used to change the
behavior of round function?

Arthur

Paul Ferrara wrote in message <886jud$gk...@bornews.borland.com>...

John Herbster

unread,
Feb 13, 2000, 3:00:00 AM2/13/00
to
The following code from D5's System.pas is interesting.
The Trunc() code temporarly changes the Control Word
(with FLDCW) rounding flags and Round() does not --
it just uses what ever was already there. -- John H

procedure _ROUND;
asm
{ -> FST(0) Extended argument }
{ <- EDX:EAX Result }
SUB ESP,8
FISTP qword ptr [ESP]
FWAIT
POP EAX
POP EDX
end;

procedure _TRUNC;
asm
{ -> FST(0) Extended argument }
{ <- EDX:EAX Result }
SUB ESP,12
FSTCW [ESP]
FWAIT
FLDCW cwChop
FISTP qword ptr [ESP+4]
FWAIT
FLDCW [ESP]
POP ECX
POP EAX
POP EDX
end;

:

"Arthur E.F.Heinrich" <art...@xperiment.com> wrote in message
news:887boo$h4...@bornews.borland.com...

Dr John Stockton

unread,
Feb 13, 2000, 3:00:00 AM2/13/00
to
JRS: In article <886aau$gk...@bornews.borland.com> of Sun, 13 Feb 2000
14:12:24 in news:borland.public.delphi.objectpascal, Henrick Hellström

<henrick....@telia.com> wrote:
>
>> Could someone please tell me why Round() rounds a number that has 0.5 as
>> the fraction part down? I would have though it should be rounded up.
>
>Rounding 2*n + 0.5 to 2*n, and 2*n + 1.5 to 2*(n + 1), is a mathematical
>convention. Pascal was originally designed as mathematical tool, so I see no
>reason why this legacy should not be carried on
>
>The rationale behind this mathematical convention has already been explained
>by Dr J D Collins.

This convention is sometimes desirable.
This convention is sometimes NOT desirable.

It should not be imposed, and a good language library would offer a full
choice of roundings, including those that may be slower to execute.

The same applies to the behaviour of X div|mod Y as X goes through zero.

--
© John Stockton, Surrey, UK. j...@merlyn.demon.co.uk Turnpike v4.00 MIME. ©
Web <URL: http://www.merlyn.demon.co.uk/> - FAQish topics, acronyms, & links.
Proper 4-line sig. separator is as above, a line exactly "-- " (SonOfRFC1036)
Do not Mail News to me. Before a reply, quote with ">" or "> " (SonOfRFC1036)

Mike Williams

unread,
Feb 13, 2000, 3:00:00 AM2/13/00
to
> But please define the term "5/4 rounding". I am not familiar with the
> terminology of american accounting. Does it mean that fractions in the
> interval 0.0 .. 0.8 are rounded down? Such a scam! That would be illegal
in
> Sweden.

I suspect it means round up on 5 and down on 4, however illogical that may
be.

-Mike

John Herbster

unread,
Feb 13, 2000, 3:00:00 AM2/13/00
to
For most people, whether scam or not depends on who
gets the money. <g>

I am sure that all prices are not equally distributed. Did you
save a spreadsheet? Could you calculate an avg rounding
"error" for your transaction set? Would be interesting. Here
we have lots of prices ending in *.99 or *.95.

Henrick wrote ...
> Since I have the habit of always saving all reciepts, I was
> actually able to check it ...


Henrick Hellström

unread,
Feb 14, 2000, 3:00:00 AM2/14/00
to
Well, if you'd call having a bachelor's degree in mathematics having
"checked it", i have. :-)

Since I have the habit of always saving all reciepts, I was actually able to

check it out: The smallest monetary unit in Sweden is 0:50 SEK. Fractions of
SEK in the interval :00 .. :25 are rounded down to the nearest whole number
of SEK. Fractions in the interval :26 .. :74 are rounded to :50. Fractions
in the interval :75 .. :99 are rounded up.

But please define the term "5/4 rounding". I am not familiar with the
terminology of american accounting. Does it mean that fractions in the
interval 0.0 .. 0.8 are rounded down? Such a scam! That would be illegal in
Sweden.


Paul Ferrara <pa...@nospam.columbusoft.com> skrev i
diskussionsgruppsmeddelandet:887bms$h3...@bornews.borland.com...

Henrick Hellström

unread,
Feb 14, 2000, 3:00:00 AM2/14/00
to
> This convention is sometimes desirable.
> This convention is sometimes NOT desirable.
>
> It should not be imposed, and a good language library would offer a full
> choice of roundings, including those that may be slower to execute.
>
> The same applies to the behaviour of X div|mod Y as X goes through zero.

Couldn't agree more!

I would strongly prefer that (X mod Y) > 0 if Y > 0. The expression (((X mod
Y) + Y) mod Y) one has to write from time to time just looks awful!

Henrick Hellström

unread,
Feb 14, 2000, 3:00:00 AM2/14/00
to
> For most people, whether scam or not depends on who
> gets the money. <g>

Well that right, but the area is probably subject to legislation in Sweden.


> I am sure that all prices are not equally distributed. Did you
> save a spreadsheet? Could you calculate an avg rounding
> "error" for your transaction set? Would be interesting. Here
> we have lots of prices ending in *.99 or *.95.
>
> Henrick wrote ...

> > Since I have the habit of always saving all reciepts, I was

> > actually able to check it ...

Average rounding error? I am quite sure the supermarket doesn't use
"stocastic" rounding, but you never can be too sure, can you?

What I did was to compare the three figures "Total" (which might end in e.g.
*.99 etc), "Cash" (what I handed to the cashier) and "Back to customer"
(what the register calculated that he or she should hand back to me). In one
case I found Total: 156.75, Cash: 160.00, Back to customer: 3.00. In another
case I found Total: 280.25, Cash: 300.00, Back to customer: 20.00.


John Herbster

unread,
Feb 14, 2000, 3:00:00 AM2/14/00
to
Henrick, I will look around for an expert in sales tax here to try to
find out how the tax collected on the individual sales is compares
with the product of the total sales times the rate. I suspect that it
will be a little hard to figure, because of little discounts that the
merchant collectors get to keep. Regards, John H

Henrick Hellström

unread,
Feb 14, 2000, 3:00:00 AM2/14/00
to
Now, that's what I call an interesting angle!

If the fraction of the price of a single item tends to be in the upper part
of the interval 0 ... 1 (e.g. "Only 9.99"), then each customer in Sweden
might have to buy at least 25 items before the total is rounded down.
Question is whether it is the actual price or the sales tax that causes this
to happen. Should the tax revenue service or the supermarket have the
rounding surplus?

In Sweden, I think there is legislation (or perhaps some kind of rules of
ethical conduct proposed by the trade organisation) against that, and has
been for the last couple of decades.


John Herbster <herb...@swbell.net> skrev i
diskussionsgruppsmeddelandet:38a7f474@dnews...

Ray Lischner

unread,
Feb 14, 2000, 3:00:00 AM2/14/00
to
On Sun, 13 Feb 2000 19:48:08 -0300, "Arthur E.F.Heinrich" <art...@xperiment.com>
wrote:

> In the system unit there is a procedure called Set8087CW. This procedure
>changes the numeric processor control variable which controls many things
>like precision and rounding mode.
>
> Is there any one who knows if this routine can be used to change the
>behavior of round function?

Yes. Download the source code for Delphi in a Nutshell from my web site. It
includes some code to make it easy to set the rounding mode.
--
Ray Lischner, author of Delphi in a Nutshell (coming later this year)
http://www.bardware.com and http://www.tempest-sw.com

Rob Kennedy

unread,
Feb 14, 2000, 3:00:00 AM2/14/00
to
"Henrick Hellström" <henrick....@telia.com> wrote in message
news:88a735$in...@bornews.borland.com...

> > > I would strongly prefer that (X mod Y) > 0 if Y > 0.
> > >The expression (((X mod Y) + Y) mod Y) one has to write from time to
time
> > just looks awful!
> > The above equates to the same as X mod Y.. what are you trying to
achieve?
>
> (X mod Y) will be negative if X < 0 and Y > 0. To ensure that the
remainder
> will always be in the interval 0..Y-1, you have add Y to the preliminary,
> possibly negative, remainder, and then apply mod Y once more in case the
> first remainder wasn't negative.

That's by the definition of remainder. Let Q = x div y and R = x mod y. Q
is already well-defined, of course. R is such that (Q * y) + R = x. What
you're asking for is not mathematically correct.

--Rob

Henrick Hellström

unread,
Feb 15, 2000, 3:00:00 AM2/15/00
to
> I assume "SV:", if not an indication of nationality, is the Swedish for
> "Re:".

I would rather say it is an indication of the stupidity of the developers at
MicroSoft working with Outlook Express. I simply don't have the option to
set the automatic reply-prefix. I can guarantee that it annoys me more than
it does you.


> Good (and non-Swedish?) newsreaders, in compliance with the RFCs, will
> recognise "Re:" and not add another;

Is MicroSoft violating RFC?


> but, for some reason or other, they
> don't seem to know the Swedish version. "Re:" is, of course, derived
> from the language much used by the great Linnaeus, of Lund & Uppsala.

Latin? I thought it was short for "Reply", just like "e.g." in fact means
"example given" and not "exempli grata". ;-)

Dave Nottage

unread,
Feb 15, 2000, 3:00:00 AM2/15/00
to
> > This convention is sometimes desirable.
> > This convention is sometimes NOT desirable.
A convention has usually been decided by a majority or experts in the field.
Changing it just because a few consider it undesirable, would be
undesirable. It's simple.. look it up in the help. If it doesnt behave the
way you want, code it another way.

> > It should not be imposed, and a good language library would offer a full
> > choice of roundings, including those that may be slower to execute.

Object Pascal offers a choice of roundings. Take a look at Trunc, for
example.

> > The same applies to the behaviour of X div|mod Y as X goes through zero.

So now mod and div needs to be changed because of a few that disagree? Mod's
meaning is: what is the remainder when you divide x, y times.. and zero
divided by anything is zero with no remainder. At least it was when I went
to school..

>
> Couldn't agree more!

Henrick Hellström

unread,
Feb 15, 2000, 3:00:00 AM2/15/00
to
> > I would strongly prefer that (X mod Y) > 0 if Y > 0.
> >The expression (((X mod Y) + Y) mod Y) one has to write from time to time
> just looks awful!
> The above equates to the same as X mod Y.. what are you trying to achieve?

(X mod Y) will be negative if X < 0 and Y > 0. To ensure that the remainder

Henrick Hellström

unread,
Feb 15, 2000, 3:00:00 AM2/15/00
to

Bevan Arps

unread,
Feb 15, 2000, 3:00:00 AM2/15/00
to
Michael Foot wrote:

> Could someone please tell me why Round() rounds a number that has 0.5 as
> the fraction part down? I would have though it should be rounded up.

I've read the rest of this thread with some interest, but there is one
thing I've got to add:

Several people wrote comments like this (quoting one example at random):

> The round function round to the nearest even number to
> reduce the error.
> To roundup like in financial applications, you can use
> other methods.

Rounding 0.5 to the nearest EVEN whole number is called "Bankers
Rounding."

The motivations for it can be justified on statistical grounds but never
lose sight of the fact that it originated with the Banking industry.

Not maths, not stats. Banks.

If you're writing an application that deals with money figures, then
check with your local governmental bodies about rounding rules. They
WILL have an opinion - on everything from rounding itself, to
propagation of rounding errors and how that should be handled.

Just my 2c,
Bevan.

_______________________________________________________________________
Bevan Arps "Programming is an art form that fights back"
OO Developer Phone: (+64 4) 473 1480
ACT Financial Systems, Fax: (+64 4) 473 1515
Level 10, Dalmuir House, Wellington. email: bevan...@actfs.co.nz
PO Box 10-278, Wellington. email: bevan...@clear.net.nz
_______________________________________________________________________

Dopeler Effect (n) The tendency of stupid ideas
to seem smarter when they come at you rapidly.
_______________________________________________________________________
***********************************************************************
This communication is confidential to ACT Financial Systems (Asia
Pacific) and is intended for use only by the addressee. The views
and opinions expressed in this email are the senders own and do not
represent the views and opinions of ACT Financial Systems (Asia
Pacific).
***********************************************************************

Paul Ferrara

unread,
Feb 15, 2000, 3:00:00 AM2/15/00
to

Bevan Arps <bevan...@actfs.co.nz> wrote in message
news:38A8AC65...@actfs.co.nz...

> Michael Foot wrote:
>
> Rounding 0.5 to the nearest EVEN whole number is called "Bankers
> Rounding."
>
> The motivations for it can be justified on statistical grounds but never
> lose sight of the fact that it originated with the Banking industry.
>
> Not maths, not stats. Banks.

I don't know who coined that but this came up a long time ago on CIS and the
banker's and CPA's that I checked with at the time never heard of it. They
use and expect 5/4 rounding. Credit card and savings account statements use
5/4 rounding. I publish an accounting system and I guarantee you that if I
used anything other than 5/4 rounding it would be considered to be buggy
software.

There may well be valid uses for the approach that Intel took for certain
purposes but dealing with dollars and cents isn't one of them.


Paul / ColumbuSoft


Henrick Hellström

unread,
Feb 15, 2000, 3:00:00 AM2/15/00
to
> > (X mod Y) will be negative if X < 0 and Y > 0. To ensure that the
> remainder
> > will always be in the interval 0..Y-1, you have add Y to the
preliminary,
> > possibly negative, remainder, and then apply mod Y once more in case the
> > first remainder wasn't negative.
>
> That's by the definition of remainder. Let Q = x div y and R = x mod y.
Q
> is already well-defined, of course. R is such that (Q * y) + R = x. What
> you're asking for is not mathematically correct.

Actually, It is not mathematically incorrect. Firstly, I disagree that Q is
well-defined by the Delphi operator, since it is rounded in the direction of
zero. It should always be rounded down.

Secondly, the expression (x mod y) is in most algebraic contexts meaning the
whole equivalence class of integers congruent to x (modulo y), and each of
these equivalence classes is commonly denoted by the least non-negative
integer in that class. Practically, you would for instance have a hard time
constructing a pseodo random generator using the Delphi mod operator, not to
mention converting a very large integer into another base, or applying many
of the most common encryption and digital signature schemes.

Henrick Hellström

unread,
Feb 15, 2000, 3:00:00 AM2/15/00
to
> There may well be valid uses for the approach that Intel took for certain
> purposes but dealing with dollars and cents isn't one of them.

It could be that the bankers who invented the rounding in question (assuming
that Bevan Arps is right) weren't americans. Americans have a double zero on
their roulette tables. I'd be surprise if there was no other example of
americans manipulating financial probabilities. ;-)

(Just kidding. Almost.)

Paul Ferrara

unread,
Feb 15, 2000, 3:00:00 AM2/15/00
to
Our accounting system is in use in more than 40 countries. 5/4 rounding
isn't unique to the USA.

Paul / ColumbuSoft

Henrick Hellström <henrick....@telia.com> wrote in message

news:88b5g3$j9...@bornews.borland.com...

Henrick Hellström

unread,
Feb 15, 2000, 3:00:00 AM2/15/00
to
You could be right. However, the fact that someone has satisified customers
in more than 40 countries, usually proves nothing more than that the
customers haven't found any dysfuntion worth complaining about.

Almost every developer has experience of telling their customer that: "Beta
testing shows that the program will give the wrong answer in about 1% of
times. I am willing to correct these errors, but it will take twice as long
and cost you the double." Confronted with such alternatives clients usually
declares that they are satisified with the current version.


Paul Ferrara <pa...@nospam.columbusoft.com> skrev i

diskussionsgruppsmeddelandet:88bclb$jc...@bornews.borland.com...

John Herbster

unread,
Feb 15, 2000, 3:00:00 AM2/15/00
to
What is 5/4 rounding?

Corrie Engelbrecht

unread,
Feb 15, 2000, 3:00:00 AM2/15/00
to
On Tue, 15 Feb 2000 10:03:41 +0100, "Henrick Hellström"
<henrick....@telia.com> wrote:

>>
>> That's by the definition of remainder. Let Q = x div y and R = x mod y.
>Q
>> is already well-defined, of course. R is such that (Q * y) + R = x. What
>> you're asking for is not mathematically correct.
>
>Actually, It is not mathematically incorrect. Firstly, I disagree that Q is
>well-defined by the Delphi operator, since it is rounded in the direction of
>zero. It should always be rounded down.
>

In what way is Q rounded when the division operator is DIV?

>integer in that class. Practically, you would for instance have a hard time
>constructing a pseodo random generator using the Delphi mod operator, not to
>mention converting a very large integer into another base, or applying many
>of the most common encryption and digital signature schemes.
>

This is a fact.

--
"I doubt, therefore I might be."
- Des-somebody.

Corrie Engelbrecht

unread,
Feb 15, 2000, 3:00:00 AM2/15/00
to
On Tue, 15 Feb 2000 01:26:40 +0100, "Henrick Hellström"
<henrick....@telia.com> wrote:

>> I assume "SV:", if not an indication of nationality, is the Swedish for
>> "Re:".
>
>I would rather say it is an indication of the stupidity of the developers at
>MicroSoft working with Outlook Express. I simply don't have the option to
>set the automatic reply-prefix. I can guarantee that it annoys me more than
>

Did they take away your option to use another newsreader as well?

Corrie Engelbrecht

unread,
Feb 15, 2000, 3:00:00 AM2/15/00
to
On Tue, 15 Feb 2000 14:31:17 +1300, Bevan Arps
<bevan...@actfs.co.nz> wrote:

>
>Rounding 0.5 to the nearest EVEN whole number is called "Bankers
>Rounding."
>

This is just a name for the method. It does not mean that banks
invented it. In fact, they didn't. It was a way to distinguish between
so-called mathematical rounding and this technique. And is doesn't
work in the way you describe.

MathRound(0.5) = 1
MathRound(0.4) = 0

BankerRound(0.5) = 1
BankerRound(0.4) = 1

This is what is comes down to, carried into infinity for however many
decimals you want. It is usually applied when the significant decimal
digits after calculation is 2. Hence the association with decimal
currency representation and hence the name "Banker's Rounding".

Henrick Hellström

unread,
Feb 15, 2000, 3:00:00 AM2/15/00
to
> In what way is Q rounded when the division operator is DIV?

DIV rounds to the nearest integer in direction of zero. According to the
euclidian algorithm (at least the one I was taught) Q should always be
rounded down.

Henrick Hellström

unread,
Feb 15, 2000, 3:00:00 AM2/15/00
to
> Did they take away your option to use another newsreader as well?

Of course not. :-) It is just that I already have a lot of strange programs
on my hard drive, and installing a newsreader just to get the reply-prefix
right would make it one too many.

John Herbster

unread,
Feb 15, 2000, 3:00:00 AM2/15/00
to
Corrie or anyone, Could you give us a definitive definition
of MathRounding() and BankerRounding()? Can anyone
give us some insite as to when and where the "bankers'
rounding" term orginated? Regards, John H

Paul Keating

unread,
Feb 15, 2000, 3:00:00 AM2/15/00
to
There is a good reason for bankers' rounding. It's out of favour now because
computers can't do it (not easily, anyway) because you have to take 2 digits
into consideration.

The reason for it is that 5/4 rounding introduces a small upward bias into
the price.

Suppose I'm selling you something, and say for the sake of argument the
price has to be in 0.10 increments.
If I round 1.00 to 1.00 I gain nothing, nor do you.
If I round 1.01 to 1.00 I lose 0.01
If I round 1.02 to 1.00 I lose 0.02
If I round 1.03 to 1.00 I lose 0.03
If I round 1.04 to 1.00 I lose 0.04
If I round 1.05 to 1.10 I gain 0.05
If I round 1.06 to 1.10 I gain 0.04
If I round 1.07 to 1.10 I gain 0.03
If I round 1.08 to 1.10 I gain 0.02
If I round 1.09 to 1.10 I gain 0.01

Assuming my prices have the last digit uniformly distributed, on 10
representative sales I will gain a total of 0.15 and lose a total of 0.10.
So on ten sales my net gain is 0.05, or half of the increment, and on
average, on each sale my gain is 0.005 or one-twentieth of the increment.

Banker's rounding says that you round up 0.05 half the time (when the next
number is even) and down the other half of the time (when the next number is
odd). That eliminates the upward bias. The way I was taught to do it is to
look at the number on the right of the 5, so 0.054 and 0.056 round up (4 and
6 are even), but 0.055 and 0.057 round down (5 and 7 are odd).

But it doesn't really matter if you look at the number on the left instead.
All you need is a consistent 2-sided dice to decide whether to round up or
down.

Paul Keating
Johannesburg
kea...@acm.org
ICQ 63479492

------------------
They CLOSED our orchestra! Your support can help revive it!
http://musicfans.to/save-the-nso


--
Paul Keating
Johannesburg
kea...@acm.org
ICQ 63479492

------------------
They CLOSED our orchestra! Your support can help revive it!
http://musicfans.to/save-the-nso
------------------

Bevan Arps <bevan...@actfs.co.nz> wrote in message
news:38A8AC65...@actfs.co.nz...
> Michael Foot wrote:
>

> > Could someone please tell me why Round() rounds a number that has 0.5 as
> > the fraction part down? I would have though it should be rounded up.
>
> I've read the rest of this thread with some interest, but there is one
> thing I've got to add:
>
> Several people wrote comments like this (quoting one example at random):
>
> > The round function round to the nearest even number to
> > reduce the error.
> > To roundup like in financial applications, you can use
> > other methods.
>

> Rounding 0.5 to the nearest EVEN whole number is called "Bankers
> Rounding."
>

> The motivations for it can be justified on statistical grounds but never
> lose sight of the fact that it originated with the Banking industry.
>
> Not maths, not stats. Banks.
>

Mike Williams

unread,
Feb 15, 2000, 3:00:00 AM2/15/00
to
> MathRound(0.5) = 1
> MathRound(0.4) = 0
>
> BankerRound(0.5) = 1
> BankerRound(0.4) = 1

If by "BankerRound" you are referring to the Delphi round function then the
correct answer for your last two examples is 0. In fact 0.4 would always
round down. 0.5 is where different rounding methods differ.

-Mike

Dr John Stockton

unread,
Feb 15, 2000, 3:00:00 AM2/15/00
to
JRS: In article <88a6fk$in...@bornews.borland.com> of Tue, 15 Feb 2000
11:08:13 in news:borland.public.delphi.objectpascal, Dave Nottage

<davidn@%n0sPaM-chariot.net.au> wrote:
>> > This convention is sometimes desirable.
>> > This convention is sometimes NOT desirable.
>A convention has usually been decided by a majority or experts in the field.

Here, that may not be the case; it may have been decided in effect by
Intel when introducing hardware DIV. Remember Dirac & the Fishermen.

>Changing it just because a few consider it undesirable, would be
>undesirable.

I am not suggesting changing the present convention, just the lack of
alternative *provision*.

>It's simple.. look it up in the help. If it doesnt behave the
>way you want, code it another way.

That's an inefficient approach, people-wise.

>> > It should not be imposed, and a good language library would offer a full
>> > choice of roundings, including those that may be slower to execute.

>Object Pascal offers a choice of roundings. Take a look at Trunc, for
>example.

Yes, and it is a limited choice, and the choices are not, in the Help,
fully compared and contrasted (I have not yet found a reference to
Banker's Rounding in the Help; "Rounding" deserves its own Index entry).

>> > The same applies to the behaviour of X div|mod Y as X goes through zero.
>So now mod and div needs to be changed because of a few that disagree? Mod's
>meaning is: what is the remainder when you divide x, y times.. and zero
>divided by anything is zero with no remainder. At least it was when I went
>to school..

Similar comments apply. What you learnt at school does not encompass
all that is likely to be wanted in practice; but it should be the aim of
the compiler/library writers to provide all that is likely to be wanted,
in an efficient manner. Changing div & mod would break existing code;
the answer is to add either new operators or, more reasonably, new
library functions (or pseudo-functions, like Abs).

--
© John Stockton, Surrey, UK. j...@merlyn.demon.co.uk Turnpike v4.00 MIME. ©
Web <URL: http://www.merlyn.demon.co.uk/> - FAQish topics, acronyms, & links.
Proper 4-line sig. separator is as above, a line exactly "-- " (SonOfRFC1036)
Do not Mail News to me. Before a reply, quote with ">" or "> " (SonOfRFC1036)

Paul Ferrara

unread,
Feb 15, 2000, 3:00:00 AM2/15/00
to
Round up on > .5 and down on < .4. It's what most people consider to be
standard rounding.

Paul

John Herbster <herb...@swbell.net> wrote in message news:38a9541e@dnews...
> What is 5/4 rounding?
>
>

John Herbster

unread,
Feb 15, 2000, 3:00:00 AM2/15/00
to
Paul,
Thanks for the partial explanation. How would 45/100 round under
5/4 rule?
While it might be true that "most people consider [the 5/4 method]
to be standard rounding", I have been around a long while and I do not
recall a such a survey as is implied ever being done or reported.
Regards, John H

"Paul Ferrara" <pa...@nospam.columbusoft.com> wrote in message
news:88co1n$km...@bornews.borland.com...

Mike Williams

unread,
Feb 15, 2000, 3:00:00 AM2/15/00
to
> Thanks for the partial explanation. How would 45/100 round under
> 5/4 rule?

I don't think the explanation "Round up on > .5 and down on < .4" is quite
accurate. I suspect what he meant was "Round up on >= .5 and down on < .5".
This, of course, assumes that we're rounding to the nearest whole number.
If rounding to the nearest tenth, 0.45 would round to 0.5 under the
"conventional" rule but would round to 0.4 using the Banker's rounding
algorithm implemented by the round() function in Delphi. If rounding to the
nearest whole number, 0.45 would round down to zero with either method.

-Mike

Corrie Engelbrecht

unread,
Feb 16, 2000, 3:00:00 AM2/16/00
to

No, I am referring to what "Banker's rounding" actually means. I
thought it was common knowledge, really. Banker's rounding always
rounds up, on the last digit. Always. It is really just a modified
trunc.

Corrie Engelbrecht

unread,
Feb 16, 2000, 3:00:00 AM2/16/00
to
On Tue, 15 Feb 2000 19:22:45 -0600, "John Herbster"
<herb...@swbell.net> wrote:

>Paul,


> Thanks for the partial explanation. How would 45/100 round under
>5/4 rule?

> While it might be true that "most people consider [the 5/4 method]
>to be standard rounding", I have been around a long while and I do not
>recall a such a survey as is implied ever being done or reported.
>

Well, I don't think we are talking about having done a survey. Nobody
said that. It's just that I know of few people who expect any other
form of rounding.

45/100 = 0.45.

Case studies:

1. Assume that you are rounding to the nearest integer. Then the
"rounding digit" to look at is the 4, and therefore:
round(0.45) = 0

2. Assume that you are rounding to the nearest decimal. Then the
significant digit is the 5, and therefore:
round(0.45) = 0.5

This is an explanation that uses 5/4 rounding for the round function,
and not any other method like Delphi (rather, that Intel) uses.

John Herbster

unread,
Feb 16, 2000, 3:00:00 AM2/16/00
to
Corrie, Now you have me really confused. What do you mean by UP for
negative numbers? (Towards the more negative or zero?) And what do you
mean by the LAST DIGIT for numbers like PI or the binary floating point
approximation of numbers like 0.1 that cannot be expressed exactly in
binary floating point? Regards, John H

Corrie wrote > ...

Craig Stuntz

unread,
Feb 16, 2000, 3:00:00 AM2/16/00
to
"Jeff Overcash (TeamB)" wrote:
>
> Actually the help file was still wrong in D4 IIRC. D5's finally got it
> correct.
>
D4's help is correct.

-Craig

--
Craig Stuntz cstuntz@no_spam.vertexsoftware.com
---------------- -----------------------------
Delphi Developer Vertex Systems Corporation
& Cat Wrangler http://www.vertexsoftware.com

Corrie Engelbrecht

unread,
Feb 17, 2000, 3:00:00 AM2/17/00
to
On Wed, 16 Feb 2000 08:44:15 -0600, "John Herbster"
<jo...@petronworld.com> wrote:

>Corrie, Now you have me really confused. What do you mean by UP for
>negative numbers? (Towards the more negative or zero?) And what do you
>

I left that information out, since rounding is performed on the
absolute value of a number, not the real one. Therefore, a negative
number is rounded up as if it were positive.

>mean by the LAST DIGIT for numbers like PI or the binary floating point
>approximation of numbers like 0.1 that cannot be expressed exactly in
>

Last digit refers to the last significant digit in the decimal portion
of the real number, depending on how many decimals must be left in the
rounded version. Remember that Delphi's standard Round() function
always returns a Int64 (for D4) or another integer type (32 bit word
for D3).

You can only write this:
x := Round(0.345) // meaning x will equal 0

You cannot write this:
y := Round(0.345,2) // meaning you would like x to equal 3.35


You can simulate it, of course, by using the Frac() and multiplying it
with 10^(required decimals), but it is probably not worth it, with the
way the Delphi standard Round() works.

>binary floating point? Regards, John H
>

Binary floating point is not feasible when you need accuracy, that is
true. Without using third party libraries or writing your own, all you
can do is give up and use a real computer. Since such a computer does
not exist (that can represent a floating point number accurately to
any degree of accuracy you are likely going to need in applications
where such issues are important), such issues are largely theoretical.

0 new messages