How does round(x,y) think?

How does round(x,y) think? - Messages

#1 Posted: 4/28/2010 5:57:18 PM
cos_phi

cos_phi

0 likes in 39 posts.

Group: User

For example I use this in a for statement: round( N/2 , 0 ).


N=3   =>   2
N=4   =>   2
N=5   =>   2 (?)
N=6   =>   3
N=7   =>   4
N=8   =>   4
N=9   =>   4 (?)

Please help me, I'm a bit confuse...


Regards,
Vlad.
#2 Posted: 4/28/2010 7:02:54 PM
Radovan Omorjan

Radovan Omorjan

325 likes in 2052 posts.

Group: Moderator

Hello Vlad,

(From Wikipeda)
There are many conventions in rounding numbers. I think SMath uses "round half to even" or unbiased rounding convention i.e. If the fraction of y is 0.5, then q is the even integer nearest to y.
Thus, for example, +23.5 becomes +24, +22.5 becomes +22, -22.5 becomes -22, and -23.5 becomes -24

Regards,
Radovan
When Sisyphus climbed to the top of a hill, they said: "Wrong boulder!"
#3 Posted: 4/28/2010 7:50:06 PM
cos_phi

cos_phi

0 likes in 39 posts.

Group: User

Wrote

Hello Vlad,

(From Wikipeda)
There are many conventions in rounding numbers. I think SMath uses "round half to even" or unbiased rounding convention i.e. If the fraction of y is 0.5, then q is the even integer nearest to y.
Thus, for example, +23.5 becomes +24, +22.5 becomes +22, -22.5 becomes -22, and -23.5 becomes -24

Regards,
Radovan





Radovan,

That's what I thought and wanted to believe, but then the results are not as the thinking implies. Let's assume that it rounds to x+1 if its fractional part is >= 0.5, else x; so, for 1.5 => 2, for 1.4 => 1. Then, for:

N=3   =>   3/2=1.5 =>  2 (ok)
N=4   =>   4/2=2   =>  2 (ok)
N=5   =>   5/2=2.5 =>  2 ?
N=6   =>   6/2=3   =>  3 (ok)
N=7   =>   7/2=3.5 =>  4 (ok)
N=8   =>   8/2=4   =>  4 (ok)
N=9   =>   9/2=4.5 =>  4 ?

Now you see why I am confuse? It's the same for 1.5 => 1 and 1.6 => 2.


Regards,
Vlad.


PS: I have one of the files for wiki. If you're interested, let me know. It's an example of the "p-q theory".
#4 Posted: 4/28/2010 9:04:10 PM
Radovan Omorjan

Radovan Omorjan

325 likes in 2052 posts.

Group: Moderator

Vlad,

I did not understand you and I might be wrong, sorry .I hope Andrey will crarify the behavior of the "round" function. For example:
[MATH]round(1,5;0)=2[/MATH]
[MATH]round(2,5;0)=2[/MATH]
This seems to me in accordance with the "round half to even" rule. To be honest, if I am using the rounding by hand I am always using the rule of increasing the digit on the place "n" if the digit on the place "n+1" is equal or greater than 5, keeping the "n"-th digit othervise, regardles of positive or negative numbers.

34.445 with two significant decimals will be 34.45
[MATH]round(34,445;2)=34,44[/MATH]
34.455 with two significant decimals will be 34.46
[MATH]round(34,455;2)=34,46[/MATH]
-28.3645 with three significant decimals will be -28.365
[MATH]round(-28,3645;3)=-28,364[/MATH]
-28.3655 with three significant decimals will be -28.366
[MATH]round(-28,3655;3)=-28,366[/MATH]

SMath will always have the last digit even.

Regards,
Radovan


When Sisyphus climbed to the top of a hill, they said: "Wrong boulder!"
1 users liked this post
Andrey Ivashov 6/18/2010 11:23:00 AM
#5 Posted: 4/28/2010 9:04:14 PM
Andrey Ivashov

Andrey Ivashov

2270 likes in 3734 posts.

Group: Super Administrator

Radovan is right. Check http://stackoverflow.com/questions/977796/in-c-math-round2-5-result-is-2-instead-of-3-are-you-kidding-me for more info...

I will change round method to "round away from 0" into the next release.

Vlad, if you need a "standard" round behavior and you can't wait for the future version of program, I can create a plugin for you... just let me know.
#6 Posted: 4/28/2010 9:15:07 PM
Radovan Omorjan

Radovan Omorjan

325 likes in 2052 posts.

Group: Moderator

Thanks Andrey,

I think the "round away from 0" will be acceptable by most of the users.

Vlad, I hope you will agree as well.

Regards,
Radovan

When Sisyphus climbed to the top of a hill, they said: "Wrong boulder!"
#7 Posted: 4/28/2010 9:16:40 PM
Andrey Ivashov

Andrey Ivashov

2270 likes in 3734 posts.

Group: Super Administrator

In addition: as a temporary solution, overridden function can be used:
[MATH]round(x;n)←trunc(x*10^n+if(x≥0;0,5;-0,5))/{10^n}[/MATH]

Regards.
#8 Posted: 4/28/2010 9:23:39 PM
cos_phi

cos_phi

0 likes in 39 posts.

Group: User

Thank you both for the replies but, really, there's no need for sorry or special plugins For the time being, I got a quick workaround with some 'if' statements. And, as Doris Day sang: "Que sera, sera", the same goes for the next release. But the bankers, ah! the bankers!


Regards,
Vlad.


[EDIT]
You were fast!
Quote

Vlad, I hope you will agree as well.



Oh, yes, I fully agree! But others will have to, as well, even if I have a feeling most will. Why would the C# guys choose that rounding, anyway?

Andrey, thank you for the solution, yours is more elegant than mine...
[/EDIT]
#9 Posted: 4/29/2010 6:47:38 AM
cos_phi

cos_phi

0 likes in 39 posts.

Group: User

What a good sleep won't do to you!
 if mod(N,2)=0
        N/2
       else
        (N+1)/2
#10 Posted: 7/16/2010 9:31:55 PM
Andrey Ivashov

Andrey Ivashov

2270 likes in 3734 posts.

Group: Super Administrator

Fixed. Next version of SMath Studio will have "round away from 0" round behavior.

Regards.
1 users liked this post
cos_phi 7/23/2010 3:11:00 PM
  • New Posts New Posts
  • No New Posts No New Posts