C# modulo operator [duplicate] - c#

This question already has answers here:
Mod of negative number is melting my brain
(15 answers)
Modulo operation with negative numbers
(12 answers)
Closed 2 years ago.
Just recently I stumbled upon a problem using the (what I assumed to be modulo) operator %:
Basically (-1) % 5 returns -1 instead of 4, so % seems to return the remainder instead of the mathematically correct modulus (either that or Wolfram Alpha is bad at math).
My question is quite simple:
Is there a real modulo operator in C# that I don't know of? Or do I have to write my own code for that?
All results I can find in the internet only point to the % ...

There is no modulo operator in c#.
The % operator, is the remainder operator:https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/operators/arithmetic-operators#remainder-operator-
The remainder operator % computes the remainder after dividing its left-hand operand by its right-hand operand.

Related

Assigning float with equation including ints [duplicate]

This question already has answers here:
Division returns zero
(8 answers)
Closed 9 months ago.
score = houseWithHospitalCount/houseCount * houseCount;
score is a float and the rest are int.
when houseWithHospitalCount is less than houseCount score always returns 0
example: 2 = 2/2 * 2
example: 0 = 1/2 * 2
Your question is similar to this question, answered here:
Division returns zero
From looking at that post, it appears you would need to use decimal literals for your ints to be interpreted as decimals and the division to not return zero.

I'm trying to make c# calculation but it's wrong [duplicate]

This question already has answers here:
Why does integer division in C# return an integer and not a float?
(8 answers)
How can I divide two integers to get a double?
(9 answers)
Closed 2 years ago.
I'm currently making a new project on c# and i have a problem - calculations are wrong. My calculation is 1 * (1 + (1 - 160 / 180)) but it returns 2. I checked this in my calculator and the right answer is 1.11111...
Please help!
P. S. sorry for my bad english
160 / 180 is integer division and will result in 0.
Try using 160.0/180 in order to promote the result a double.

Console.Writeline(integer); prints number without decimals even though there should be decimals [duplicate]

This question already has answers here:
Possible Loss of Fraction
(5 answers)
Closed 4 years ago.
I'm trying to print out this integer variable which in the case of my test numbers should be 5,2333 but only 5 gets printed.
int finalTimeInt = ((N * 60 + n) / 2) / 60;
Console.Writeline(finalTimeInt);
As stated already I'm expecting an output of 5,2333 but am only getting a 5.
Integers can't hold decimals. If you want a variable to store decimals, you should be using a floating-point type, like float, or double if you want more accuracy.

Weird C# Math calculation [duplicate]

This question already has answers here:
Is floating point math broken?
(31 answers)
Closed 7 years ago.
below code is weird. when i debug it, i can see both "result" and "round" are 5, but output is "false". Any idea?
double result = Math.Log (243, 3); // 5
double round = Math.Round (result); // 5
Console.WriteLine (result == round);
Comparison of floating point with equality operator could have loss of precision while rounding values. You can fix this by compare a difference with epsilon. Use a Tolerance like this:
Console.WriteLine(Math.Abs(result - round) < 0.0000001); // True

What does '<<' do in a C# integer declaration? [duplicate]

This question already has answers here:
C# Left Shift Operator
(4 answers)
Closed 7 years ago.
In C#, what does the << operator do when used to declare a variable?
int layer = 1 << 8;
I've tried looking up the operator, but only confirmed what I already understood. I'm not to sure how it is being applied in a way that is compatible with the integer.
The left-shift operator (<<) shifts its first operand left by the number of bits specified by its second operand. The type of the second operand must be an int or a type that has a predefined implicit numeric conversion to int.

Categories

Resources