Skip to main content

Translation Not Available Yet

Modulo Operations with Negative Numbers is not yet available in Polski. Showing the English version below. All formulas and calculations work the same.

Przejdź do kalkulatora
Advertisement

Ostatnia aktualizacja: 1 sierpnia 2026

Modulo Operations with Negative Numbers

Quick Answer

This calculator computes modulo for negative numbers using three conventions at once: truncated (JavaScript/C-style), floored (Python-style), and Euclidean (always non-negative), helping compare how they diverge for negative operands.

Enter a dividend and divisor, and the calculator compares truncated, floored, and Euclidean modulo results side by side.

Przydatne
Nieprzydatne
Zapisz jako obraz
Udostępnij
Osadź
Cytuj
Napisz opinię

Wzór

truncatedMod = a % n; flooredMod = a - n*floor(a/n); euclideanMod = ((a % n) + |n|) % |n|

Gdzie:

  • a=Dividend, may be negative
  • n=Divisor, may be negative
Modulo with Negative NumbersNegative seven mod three evaluated three ways: truncated gives negative one, floored gives two, and Euclidean gives two.-7 mod 3Truncated (JS %)-1Floored2Euclidean2
Negative numbers can give different modulo results depending on the convention used.

Rozwiązane przykłady

Negative dividend

Compare -7 mod 3 across conventions.

  1. 1Truncated: -7 % 3 = -1
  2. 2Floored: -7 - 3*floor(-7/3) = 2
  3. 3Euclidean: always non-negative, so 2
Ostateczna Odpowiedź: truncatedMod = -1, flooredMod = 2, euclideanMod = 2

Negative divisor

Compare 7 mod -3 across conventions.

  1. 1Truncated: 7 % -3 = 1
  2. 2Floored: 7 - (-3)*floor(7/-3) = -2
  3. 3Euclidean: 1
Ostateczna Odpowiedź: truncatedMod = 1, flooredMod = -2, euclideanMod = 1

Both negative

Compare -7 mod -3 across conventions.

  1. 1Truncated: -7 % -3 = -1
  2. 2Floored: -1
  3. 3Euclidean: 2
Ostateczna Odpowiedź: truncatedMod = -1, flooredMod = -1, euclideanMod = 2

Positive values agree

Compare 7 mod 3, where all conventions match.

  1. 1Truncated: 7 % 3 = 1
  2. 2Floored: 1
  3. 3Euclidean: 1
Ostateczna Odpowiedź: truncatedMod = 1, flooredMod = 1, euclideanMod = 1

Wprowadzenie

When negative numbers are involved, "modulo" can mean different things in different programming languages. This calculator computes the three most common conventions side by side so you can see exactly how they differ.

Why Negative Modulo Isn't Universal

Unlike modulo of two positive numbers, the sign of the result for negative operands depends on the convention chosen. JavaScript, Java, and C use truncated division; Python uses floored division; and many textbooks define a strictly non-negative Euclidean remainder.

  • Truncated modulo keeps the same sign as the dividend

  • Floored modulo keeps the same sign as the divisor

  • Euclidean modulo is always between 0 and the absolute value of the divisor

How the Calculator Works

The calculator computes all three conventions from the same inputs: the raw % operator for truncated modulo, a floor-based formula for floored modulo, and a sign correction for Euclidean modulo.

  • Both a and n must be finite numbers

  • The divisor n cannot be zero

  • All three results are computed and returned together

Input Guide

Enter the dividend a and the divisor n; either or both can be negative.

  1. 1

    a: the dividend, e.g. -7

  2. 2

    n: the divisor, e.g. 3

Output Guide

The calculator returns all three modulo conventions so you can compare them directly.

  1. 1

    truncatedMod: result matching JavaScript, Java, and C's % operator

  2. 2

    flooredMod: result matching Python's % operator

  3. 3

    euclideanMod: the always non-negative remainder

How to Use This Calculator

Enter any dividend and divisor, then compare the three outputs to see how conventions diverge for negative values.

  • Try a negative dividend with a positive divisor

  • Try a positive dividend with a negative divisor

  • Compare all three outputs when both are negative

Common Mistakes

Mixing up conventions between programming languages is the most common source of negative-modulo bugs.

  • Assuming a language's % always returns a non-negative value

  • Porting code between languages without checking modulo conventions

  • Entering a zero divisor, which is undefined for all conventions

Practical Uses

Understanding these conventions matters when porting code, working with circular data structures, or teaching modular arithmetic.

  • Debugging sign differences when porting code between languages

  • Wrapping array indices safely regardless of sign

  • Teaching the different definitions of modular arithmetic

FAQ

What is truncated modulo?

Truncated modulo (used by JavaScript, Java, and C) keeps the same sign as the dividend; for example -7 % 3 equals -1.

What is floored modulo?

Floored modulo (used by Python) keeps the same sign as the divisor; for example -7 mod 3 equals 2 in Python.

What is Euclidean modulo?

Euclidean modulo always returns a non-negative remainder between 0 and the absolute value of the divisor, regardless of sign.

Why do different languages give different answers for the same inputs?

Each language chooses a different mathematical convention for handling negative operands, even though they agree completely for positive inputs.

Which convention should I use?

Use whichever convention matches your target programming language or the mathematical definition your problem requires; the Euclidean convention is often preferred when a non-negative result is required.

Do all three conventions agree for positive numbers?

Yes, when both the dividend and divisor are positive, truncated, floored, and Euclidean modulo all produce the same result.

Can the divisor be zero?

No, all three modulo conventions are undefined when the divisor is zero, so the calculator returns a validation error.

Is Euclidean modulo ever negative?

No, by definition Euclidean modulo is always in the range from 0 up to (but not including) the absolute value of the divisor.