C# modulus equation return wrong value

c# modulo
c# modulus negative
can modulus be negative
rust modulo
negative modulo
c# get remainder of decimal
c# integer division rounding
c# absolute value
var newPosition = (position - key) % alphabet.Length;

When position is 66, key is 7964 and alphabet.length = 91, newPosition is somehow -72, even though it should be 19. Why?

Be aware % is actually not a modulus its a remainder

static decimal modulus(decimal a, decimal b)
{
  return a - b * Math.Floor(a / b);
}

...

Console.WriteLine( nfmod(66-7964 , 91));

Ouput

19

Full Demo Here

Mod of negative number is melting my brain, Please note that C# and C++'s % operator is actually NOT a modulo, it's remainder. The formula for modulo that you want, in your case, is: Just add your modulus (arrayLength) to the negative result of % and you'll be fine. Even worse, with default unchecked context, the first answer may return the wrong answer (see  Note: If the first argument to the predefined modulo operator is 81 and the second operand is 80, the expression evaluates to a value of 1. C# program that uses modulo operator using System; class Program { static void Main() { // When 1000 is divided by 90, the remainder is 10.

Use / for module

var newPosition = (position - key) / alphabet.Length;

C# Modulus Is WRONG!, 0 -2 -> -2 -1 -> -1. Modulus is supposed to give me values from [0..3] but I'm getting negative numbers! The formula for modulo that you want, in your case, is: Almost all programming languages return a signed remainder. In C#, the modulus operator (%) is an operator that is meant to find the remainder after dividing the first operand (the first number) by the second. The best way to understand how the modulus works is to see it in action. Take a look at the example below: Console.WriteLine(5%2) Console.WriteLine(6%3) Console.WriteLine(10%4)

((position - key) % alphabet.Length + alphabet.Length) % alphabet.Length

Did the trick

Bitcoin for Nonmathematicians: Exploring the Foundations of Crypto , Those formulas in chapter 7 look relatively simple but there are several “tricks” required in There is a C# operator for calculating the modulus: %. However, this code may return a wrong result if a has a negative value because our curve is  The absolute value of z is the value produced by |x| - n * |y| where n is the largest possible integer that is less than or equal to |x| / |y| and |x| and |y| are the absolute values of x and y, respectively.

Arithmetic operators, Learn about C# operators that perform multiplication, division, remainder, addition, and subtraction operations with The unary + operator returns the value of its operand. WriteLine(d == 1.0m); // output: False Console. Solving Absolute Value Equations and Inequalities - Number Line & Interval Notation - Algebra - Duration: 13:13. The Organic Chemistry Tutor 188,972 views 13:13

Math.Abs Method (System), Returns the absolute value of a double-precision floating-point number. The following example uses the Abs(Decimal) method to get the absolute value of a number of Decimal values. C# Copy. Run WriteLine("Unable to calculate the absolute value of {0}. CLSCompliant(false)] public static sbyte Abs (sbyte value​); C# is one of the best languages which you can easily understand the functionality of its formula. This % sign is called modulus in C# which is used to get a particular value of a variable. It gives you the remainder after division like 6%2.

Mathematical Operators - Learning C# 3.0 [Book], Thus, dividing 17 by 4 returns a value of 4 (C# discards the remainder of 1). Of course, you might want to calculate the remainder from an integer division, For that, C# provides a special operator, modulus ( % ), to retrieve the remainder. You need to implement a parser of your own which could solve the equations. One needs to be well aware of your requirements to help you out. Search google for "C# Equation Parser". It may give you some ideas to start with.

Comments
  • % is not modulus. It is remainder. You could use something like this: ((position - key) % alphabet.Length + alphabet.Length) % alphabet.Length
  • @PetSerAl What's modulus then?
  • AFAIK, there are no modulus operator in C#.
  • I do want to point out that % is the modulo operator in C#, and (66-7964) % 91 is -72. I wanted to point it out, as the terms are something I often confuse, especially as they're both abbreviated to 'mod.' Worse, there's a number of different meanings involved for both terms mathematically: math.stackexchange.com/questions/472856
  • That still returned a negative number