# Maths Course - Rounding - incorrect Calcs?

At 4.34 ish

void Start()
{
float a = 5.3f;
float b = 4.7f;
float c = 2.5f;

``````    print(a * b + c);
print(Mathf.Round(a) + "  " +  Mathf.Round(b) + "  " + Mathf.Round(c));
print(Mathf.Round(a) * Mathf.Round(b) + Mathf.Round(c));
}
``````

gave me
27.41
5 5 2
27

not in the vid which had 26.47 and 28

Thanks for pointing this out @EddieRocks, youâ€™re absolutely right - I really should have noticed that before the video got uploaded and will get it corrected asap. Iâ€™ll also do everything I can to stop it happening again.

The correct sum should have been:
5.1 x 4.7 + 2.5 = 26.47
This then rounds down to 26.

Rather than:
5.3 x 4.7 + 2.5 = 27.41, as you said.

The second half is correct though (using traditional rounding), as 2.5 would round up to 3, so;
5 x 5 + 3 = 25 + 3 = 28

The System.Math library in C# actually uses bankerâ€™s rounding by default, so by extension UnityEngine.Mathf does too. This is the same in some other languages too, but not all.

This means that if a number is equidistant between two integers (i.e. x.5), the nearest even integer is returned - so if x is even then the 0.5 is rounded down and if x is odd then it gets rounded up.
For example, 2.5 would round down to 2 and 3.5 would round up to 4.

This might sound kind of weird but it does have itâ€™s uses, especially when youâ€™re dealing with a lot of numbers that need rounding.

In some Round functions you can pass in a second parameter to specify the type of midpoint rounding you want to use. However, in Unity, Mathf.Round doesnâ€™t let you do this.
One way around it would be to use the System.Math library directly and write something like;
`System.Math.Round(x, System.MidpointRounding.AwayFromZero);`
(You can remove the `System` part if you include `using System;` at the top of your script)

Iâ€™m trying to keep examples in pseudo-code rather than being language specific, but noting some of these weird quirks could definitely be helpful. My main advice though would be to always check the docs when using a new function for the first time, as they donâ€™t always work like youâ€™d expect.

Anyway, I hope that all helps.
I really want to make the course the best it can be, so any feedback (good or bad) is very much appreciated - and definitely continue to call me out if/when I make mistakes!

3 Likes

You are doing a great job Gary! New video with correct values is uploaded

1 Like

Thanks Gary.

Very interesting on the actual rounding - Iâ€™d not heard of bankers rounding even though I did work for a bank though it makes a little sense in averaging out things though doubt it has been used in banks for many many years .
Also lucky you chose an even number or I wouldnâ€™t have known any better.

I am hoping the course though maths, is focused around uses in gaming/coding which most of the course so far has been lackingâ€¦

1 Like

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.