Article

From:
To:
John Herbster
Subject:
Re: Rounding in OCL
Newsgroup:
borland.public.delphi.modeldrivenarchitecture.eco

Re: Rounding in OCL

Hello John,

> Just to help clarify the argument, are you referring to rounding of
> decimal fraction variables like varDecimal or BCD? Or binary fraction
> variables like single, double, and extended?
Decimal and Double.


>> It is used in mathematics, physics. ...
>> http://en.wikipedia.org/wiki/Rounding
> Ditto for most of the other rounding methods. I bet that I can even
> find an application of "statistical rounding".
> 
True, But you don't need to search for application that uses arithmetic rounding.
Pick the first one on the internet.
Also ask any developer (except .NET one) what do they expect from Round(3.5) 
and Round(4,5).
I bet they will tell 4 and 5. And I also belive that some .NET developers 
still expect the same.


>> The default .NET implementation of Math.Round implements bankers
>> rounding and might be used for banking calculations.
>> 
>> Also ROUND functions in the databases (including mssql) also use
>> [symmetric] arithmetic rounding.
>> 
> Note SQL Server can also use "floor" and "ceil" rounding, although
> they only round to whole number values.
You say "can". Yes.
But still default ROUND operation uses arithmetic rounding. Floor and ceil 
are just another story, they don't round.
They just cut (truncate). I don't think we should mix it.

> Discussion of rounding and a source of names for different
> rounding methods:
> http://support.microsoft.com/kb/196652
Nothing new. Except: "There are no Excel spreadsheet functions that perform
banker's rounding."
If it is such common why isn't it there?
Also as far as I know (I might be wrong of course) MsSql doesn't have banker's 
rounding as well.
So it's just really a .NET "default feature".
Have a look at number of complains at this "feature": http://www.google.com.au/search?q=rounding+in+.net


> Discussion of role of accountant in making some rounding
> decisions:
> http://bytes.com/forum/thread374225.html
This discussin came to life only because of .NET's default rounding method.
This discussion just would not exist if .NET's default rounding would be 
arithmetic one.


>> So I think ECO (if it supports round in PS) will probably
>> fail the test using OclService and OclPsService like
>> "self.Transactions->select(Amount.round>4)"
Small correction here "Amount.round=4"
>> If there will be Transactions with Amounts 3.5 and 4.5
>> (they will round to 4 and 4 correspondingly).
>> [Symmetric arithmetic rounding] is used everywhere ...
>> 
> I doubt that!
Do you doubt that ECO will fail here?
And you are right. I have tried and it just doesn't support .round operation 
on the database. What a fun.
Do you doubt it is used everywhere?

Ok. From wiki. Let's take: - C - "round to nearest integer, halfway away from zero" - Pascal - "Round and RoundTo (nonstandard) use banker's rounding" - PHP - "Any decimal .5 or above is rounded up, anything under .5 is rounded down." - Python - "use common rounding for POSITIVE numbers: round(0.5) is 1.0." - JavaScript - "Uses Asymmetric Arithmetic Rounding" - MsSql - "Uses Symmetric Arithmetic Rounding"
(Please note where words 'nonstandard' and 'common' are applied to)
Exceptions are only: - VB - banker's rounding. - .NET Framework - banker's rounding.

> 
>> Math.Round in .NET is just wrong.
>> 
> To which of the overloaded Math.Round methods are you referring?
> http://msdn.microsoft.com/en-us/library/system.math.round.aspx
> 
Round(decimal) and round(double).
Both of them "follow IEEE Standard 754, section 4".
I see what you mean: "you can choose method using MidpointRounding parameter".
But it is also different story.
I'm pretty sure ECO do not set that parameter and uses default.
It also has been introduced in .NET 2.0. So there were obviously reasons 
for that.

>> int := Round(4.5) ===Visualised as===>4
>> And it was really incorrect. So I had to replace Math.Round
>> with my own method.
> Did you extend your program to do rounding to a specified
> number of decimal fraction digits?
I don't remember exactly. I think I only added rounding to integer only.
I really have to remember the ANSI standard that I was implementing to find 
out.

> Did you program use decimal fraction variables like varDecimal
> or BCD?  Or binary fraction variables like single and double?
Those old ANSI Pascal had only floating point types as far as I can remember.


> I wonder how you would do that within the FPU? <a challenge>
> 
>> If you need non-standard rounding - implement it.
>> 
> That is easier said than done, especially if you have to do it using
> floating binary point variables, like single, double, and extended, to
> represent decimal fraction values.
You don't need to implement it, it's already available in .NET 2.0.
But not by default (which I believe should). Just use (MidpointRounding param)

But here is the sample for you: public static decimal ArithmeticRound2(double value) { return ArithmeticRound2( (decimal)value ); }
public static decimal ArithmeticRound(decimal value, int count) { int tCount = 1; for(int i = 0; i < count; i++) tCount = tCount * 10;
value = value * tCount;
if (value - decimal.Truncate(value) >= 0.5m) value = decimal.Truncate(value) + 1; else value = decimal.Truncate(value); return value / tCount; } Better sample: http://blog.falafel.com/2006/03/23/ImplementingStandardArithmeticRoundingInNET.aspx

My point is that you can use any required rounding method in your application. I'm not talking about particular cases. I'm talking about programming platforms and languages.
I also belive that .round operation in old, good Bold was actually arithmetic (Am I right?). If so it means the interface was broken and nobody probably even mentioned it during years (since first ECO).
So I'm saying that basic mathematical operations should be "standartised". And the best standard is aruthmetic rounding. So I believe ECO should use it and fix the .NET's leak.
Cheers.
FYI: Phrase searches are enclosed in either single or double quotes
 
 
Originally created by
Tamarack Associates
Fri, 29 Mar 2024 08:24:06 UTC
Copyright © 2009-2024
HREF Tools Corp.