Inconsistent Math.Floor results [duplicate] - c#

This question already has answers here:
Why are floating point numbers inaccurate?
(5 answers)
Closed 4 years ago.
I'm a bit puzzled by the results of the Math.Floor function in C#.
I get a return of 91 as expected with the call below:
Math.Floor(91.0);
But if I use the call below I get a returned value of 90, while I still expect 91 in this case.
Math.Floor(9.1/0.1);
Is this just due to small rounding errors and is there a way to get consistent results?

Yes, this problem is related to precision, since Math.Floor() simply doesn't round mathematically and always down instead. So even 90.9999999 still results in only 90.
For accurate rounding, use Math.Round() instead or add 0.5 to the value passed.

Related

Trying to understand Mathf.Approximately [duplicate]

This question already has answers here:
Is Mathf.Approximately(0.0f, float.Epsilon) == true its correct behavior?
(2 answers)
Closed 1 year ago.
Can someone give a simplest example, when Mathf.Approximately gives true?
A try the following and it is always false:
Debug.Log(Mathf.Approximately(0.0001f, 0.0f)); // False
Debug.Log(Mathf.Approximately(0.00000001f, 0.0f)); // False
Debug.Log(Mathf.Approximately(0.00000000000001f, 0.0f)); // False
The documentation says that they're equal if they are "within a very small value (epsilon)".
Epsilon is documented as "the smallest value a float can have different from zero".
How small is that? FloatMinNormal or FloatMinDenormal depending on IsFlushToZeroEnabled, which as you can see from source code would be about 1e-38, much smaller than 0.00000000000001f.
For non-zero values, the exact computation depends on the order of magnitude of the numbers, you can check out the source code for the precise formula.
Some numbers (e.g. 0.1) can't be represented exactly with a float. This is why Approximately can be more useful than straight equality. It is a complex topic, though, that needs more than a SO answer to fully develop. If you want to learn more, maybe start there: https://floating-point-gui.de/

Math.Abs weird behaviour [duplicate]

This question already has answers here:
Double vs Decimal Rounding in C#
(2 answers)
Closed 7 years ago.
I am doing following equation but the result is not as expected
double dasdas = Math.Abs(3.2 - 1.9);
The result is
1.3000000000000003
However the correct result should be
1.3
What may be the reason of this?
c# 4.5.2
This is because you're using a double - which is a floating point. These by definition this cannot store the exact number.
You need to use Decimal.
Take at look at here and What Every Computer Scientist Should Read About Floating Point
Use decimal:
decimal dasdas = Math.Abs(3.2m - 1.9m);

Math.Round(((4.006+4.007)/2),3) = 4.006? [duplicate]

This question already has answers here:
C# - (int)Math.Round((double)(3514 + 3515)/2) =3514?
(2 answers)
Closed 9 years ago.
Similar question, but without double and 3 decimal places.
The difference is that the average of two integers we may have a double as a result, but when we use (int) Math.Ceiling ((double) value), result an integer.
C# - (int)Math.Round((double)(3514 + 3515)/2) =3514?
But in this case, we have two doubles and
Math.Round(((4.006+4.007)/2),3); // returns 4.006
Math.Round(((4.008+4.007)/2),3); // returns 4.008
WHY?
From the MSDN:
Return Value
Type: System.Double The integer nearest a. If the fractional component
of a is halfway between two integers, one of which is even and the
other odd, then the even number is returned. Note that this method
returns a Double instead of an integral type.
Remarks
The behavior of this method follows IEEE Standard 754, section 4. This
kind of rounding is sometimes called rounding to nearest, or banker's
rounding. It minimizes rounding errors that result from consistently
rounding a midpoint value in a single direction.
Also check this related thread

Math.Round() seems to be not consistent [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
.Net Round Bug
In C#: Math.Round(2.5) result is 2 (instead of 3)! Are you kidding me?
Code:
var d1 = Math.Round(187.5); // 188
var d2 = Math.Round(62.5); // 62
Why is it so?
By default, Math.Round uses a form of rounding called Banker's Rounding, which rounds to the nearest even integer when the input is halfway between two integers.
See Why does .NET use banker's rounding as default? for an understanding of this design decision.
If you don't like this behaviour, you can always use this overload of Math.Round, which lets you specify the MidPointRoundingMode (ToEven, AwayFromZero).
You can change this behaviour with an call to this overload of the method - http://msdn.microsoft.com/en-us/library/ms131274.aspx

What Should be the result of 128.01+0.01 in C# [duplicate]

This question already has answers here:
Closed 12 years ago.
Possible Duplicates:
Precision of Floating Point
Floating point arithmetic is too reliable.
Hi Guys,
I came across a rather strange looking problem, i am running a loop from 82.01 to 169.06 in steps of 0.01 but when i reach 128.01 and do (128.01+0.01) it gives 128.019999999998 instead of 128.02. I am using double for all these computations. If i use decimal to do these computations it works out fine, am i missing a very basic funda here, i found some articles and discussions on the web explaining that decimal is the correct data type to do these computations but still a basic computation like (128.01+0.01) should give correct results.
Floating-point calculations are inherently inaccurate, so, this behaviour is completely normal. If you really need higher accuracy, stick to decimals, but keep in mind that they are way, way slower than floats/doubles. Usually, it's better to just accept the inaccuracies and round as needed.
Here is the scientific detailed explanation of your issue

Categories