How Math.Round is working? [closed] - c#

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 7 years ago.
Improve this question
Recently I found an issue with C# Math.Round that makes me confused:
Math.Round(1.125, 2) == 1.12
However:
Math.Round(1.115, 2) == 1.12
Also
Math.Round(1.225, 2) == 1.23
And
Math.Round(1.215, 2) == 1.22
The last three samples are make sense just want to know how does it work for the first one?
Regarding to the document, if round to nearest even number then why in third sample round to Odd number?
Is anyone has any idea how is rounding the digits?
Updating
As I put the link, I know try to round to nearest even number, I want to know how implementing it (Using shift bytes)?

In the last three examples, it's working as documented. In the first case though, it appears to go wrong:
Math.Round(1.125, 2) == 1.12 // Would expect 1.13
The reason is simple: floats are stored as binary values; not decimal. So it's rounding the binary value that 1.125 has been converted to. In this case it is slightly below 1.125 and thus rounds "the wrong way".

Related

Why does Math.Round give me two different values? [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 5 months ago.
Improve this question
class Program {
static void Main(string[] args) {
double d = 120.5;
Console.WriteLine(Math.Round(120.5)); //121
Console.WriteLine(Math.Round(d)); // 120
}
}
When a variable is passed as an argument into Math.Round it produces an answer similar to Convert.ToInt32 where floating numbers are rounded off to the nearest even number if the trailing tenth number is 0.5.
Anyone can kindly explain? Thanks in advance.
Thanks for the answers! I use Replit most of the time, that's the output I got. But seeing the replies, I tested it again in VS and I got both 120.
I guess there's a bug in replit?
Kindly refer to attachments.
enter image description here
enter image description here
I tested your code and returns 120 in two modes. It is good to know that the Math.Round() has features that you can use.
For example, you can say to always round to a number that is further from zero:
double d = 120.5;
Console.WriteLine(Math.Round(d,MidpointRounding.AwayFromZero)); //always 121
or final digit is even:
Console.WriteLine(Math.Round(d,MidpointRounding.ToEven)); //always 120
and other features like MidpointRounding.ToNegativeInfinity, MidpointRounding.ToPositiveInfinity ....

c# string.CompareOrdinal vs operator == [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
I want to compare two strings in a linq expression. Do I take advantage if I use `string.CompareOrdinal or is it the same?
list.Where(str1 => string.CompareOrdinal(str1, str2) == 0);
list.Where(str1 => str1 == str2);
According to benchmarks done by someone else, string.CompareOrdinal can be slightly faster than == when doing a lot of comparisons:
Most of the board remained green up through 10,000 comparisons and didn’t register any time.
At the 100,000 and 1,000,000 marks, things started to get a bit more interesting in terms of time differences.
String.CompareOrdinal was the constant superstar. What surprised me is for the case-insensitive comparisons, String.CompareOrdinal outperformed most other methods by a whole decimal place.
For case sensitive comparisons, most programmers can probably stick with the “==” operator.
-- The Curious Consultant: Fastest Way to Compare Strings in C# .Net
Note, though, that we are talking about a total difference of 3 milliseconds for 100,000 case-sensitive string comparisons, and that no measurable differences have been observed for 10,000 and 1,000,000 comparisons.
Thus, is very unlikely that this difference is relevant to your application (especially if you are using LINQ-to-objects), so the more readable == should be preferred.

Redundant Casting in Visual studio [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 5 years ago.
Improve this question
Im working out a compression ratio and I noticed that I have a "Redundant Cast" in this line:
double ratio = (double)data.Length / (double)comp.Length;
Obviously dividing both array lengths without casting to double will return only the integer component of the number.
But why is this cast redundant?
Having just one cast on one Length removes the warning.
To my understanding if we had:
double ratio = (double)data.Length / comp.Length;
or
double ratio = data.Length / (double)comp.Length;
wouldnt that then potentially cause another developer to get confused about what value is being calculated here.
It seams to me that having an implicit cast can cause code readability issues. Especially in more complex one line calculations (which really is another readability question).
I also thought casting values to different forms produce different outputs depending on where the cast is made in the equation.
As a developer, I can say that
double ratio = (double)data.Length / comp.Length;
or
double ratio = data.Length / (double)comp.Length;
Will never confuse me. I recoginze this "pattern":
Division operation
One operand is casted to double
So, I understand, that second operand and result are double. And I (as developer) don't need second cast operator.

Bizarre division behavior [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 6 years ago.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Improve this question
I know at a first glance, this will get a duplicate mention. And it very well might be, but I imagine most people are going to think that the answer is similar to this question: Division returns zero
That is not my case. That case, is dividing a smaller number by a larger number integers and getting 0. That makes sense. What's happening for me does not make sense.
Here's my code:
decimal correctedImageWidth = screenWidth / maxColumnsWithMaxSizeImages;
The value for screenwidth, which is an int, is 1024. The value for maxColumnsWithMaxSizeImages, also an int, is 3.
Somehow, correctedImageWidth becomes 0. Which is odd, because 1024/3 does not equal 0, nor would the rounded off number be 0, like the other SO questions had. You'd think I'd get something like 341. Here's proof that the numbers are what I say they are:
As you can see in my watch. screenWidth is 1024, maxColumnsWithMaxSizeImages is 3. However, dividing these 2 into correctedImageWidth is 0? Is there any reason why I would get 0 from this? I have shown this to colleagues, who are equally as confused. Here's my entire code, perhaps there's something I'm doing? Unlikely, seeing as they're both ints and they both have valid integer values. But obviously there must be something I'm doing? Here's the code:
int maxColumnsWithMaxSizeImages = (int)System.Math.Ceiling(decimal.Divide(1024, 480));
if (maxColumnsWithMaxSizeImages < _minimumImagesPerRow)
{
.....
} else if (maxColumnsWithMaxSizeImages > _maximumImagesPerRow)
{
....
} else
{
//between 2 and 4 columns
var screenWidth = App.ScreenWidth;
decimal correctedImageWidth = (decimal)((decimal)screenWidth / (decimal)maxColumnsWithMaxSizeImages);
decimal test2 = 1024 / 3;
decimal test3 = (decimal)1024 / (decimal)3;
var test = correctedImageWidth;
}
UPDATE
For some reason, it appears that there was a conflict in my variable declarations. Even though I declared them both in different scopes, for whatever reason it was stirring a conflict. After renaming the variable, I get the corrected value.
You've defined the field correctedImageWidth twice, once as int (in the if block) and once as decimal (in the else block).
The screenshot of your Watch window shows the int typed field value, which shows 0 at this point (wasn't assigned since your in the else block). Try watching the decimal typed field, or just hovering your mouse over the other decimal typed field while debugging, it should show you the correct value.

Calculate & subtract percentage in C# [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 8 years ago.
Improve this question
Well I know this would be a very easy question ,but i am poor at this concept of Maths.
I know what my final answer should be ,but this answer should come after I subtract some specific percentage from another amount. I want to know what that another amount should be.
Making it a bit simpler :
this is my equation : x - 12% of x should result in 250$. I need to find x.
( Please know if I add 12% to 250 , it will be wrong as 12% of 250 would be different from 12% of what that another amount would be ).
Thank you friends.
This is simply algebra (not necessarily even relevant to C#).
Your equation is:
Solve for :
Therefore this can be calculated in C#:
double x = 250/0.88;
Console.WriteLine(x);
which outputs 284.090909090909

Categories