Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8460435
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T13:36:23+00:00 2026-06-10T13:36:23+00:00

major edit: 100% solved! it’s called Modular arithmetic thanks Peter!! i need to add

  • 0

major edit: 100% solved! it’s called Modular arithmetic thanks Peter!!

i need to add two numbers with a fixed min/max value.
i want my numbers behave like java’s int/byte/short (overflowing to its opposite value and continuing the operation)

System.out.println((byte) (Byte.MAX_VALUE));    // 127
System.out.println((byte)(Byte.MAX_VALUE + 1)); // -128
System.out.println((byte)(Byte.MAX_VALUE + 2)); // -127
System.out.println((byte)(Byte.MAX_VALUE + 3)); // -126

but with a fixed .MAX_VALUE and .MIN_VALUE. if a number’s value is 3 and it’s maxValue is 5 and minValue is 2, then when i add 4 to it (3+4=should be 7) it overflows
so 3+4: 3 -> 4 -> 5 -> 2 -> 3
example:

    int value = 0, minValue = -2, maxValue = 1;
    MyNumber n = new MyNumber(value, minValue, maxValue);

    // possible values: -2 -1  0  1 -2 -1  0  1 -2 -1  0  1 ..

    n.add(2);   // 0+2 = -2
    n.add(-2);  // -2-2 = 0
    n.add(5);   // 0+5 = 1       
    n.add(-5);  // 1-5 = 0       
    n.add(-5);  // 0-5 = -1       
    n.add(-1);  // -1-1 = -2       
    n.add(11);  // -2+11 = 1

this is what i did:

class MyNumber {

    int value;
    final int minValue, maxValue;

    public MyNumber(int value, int minValue, int maxValue) {
        if (value < minValue || value > maxValue || maxValue < minValue) {
            throw new RuntimeException();
        }
        this.value = value;
        this.minValue = minValue;
        this.maxValue = maxValue;
    }

    void add(int amount) {
        int step = 1;
        if (amount < 0) {
            step = -1;
            amount = -amount;
        }
        while (amount-- > 0) {
            value += step;
            if (value < minValue)
                value = maxValue; // overflows
            if (value > maxValue)
                value = minValue; // overflows
        }
    }
}

it works but i don’t want to iterate the whole addition since i’m going to work with big numbers
i think it has something to do with MOD… (i am terrible at maths)
nearly randomly i made this:

void add(int amount) {
    value = (value + amount) % (maxValue - minValue + 1);
}

i was so close but it fails at

n = new MyNumber(-2, -4, -1);
n.add(2); // -2+2 shows 0 instead of -4   (-2.. -1.. *overflow*.. -4)

i surrender

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T13:36:25+00:00Added an answer on June 10, 2026 at 1:36 pm

    I would try to make things as clear as possible. e.g

    If you want clock arithmetic you can do

       // in the constructor
       this.range = maxValue - minValue + 1;
       this.value = -minValue;
    
       // in the adder.
       public void add(int num) {
           value = (value + num) % range;
           if(value < 0) value += range;
           // or
           value = ((value + num) % range + range) % range;
       }
    
    
       // add a getter for value.
       public int getValue() { return value + minValue; };
    

    If you wanted bounded arithmetic.

        value = Math.min(maxValue, Math.max(minValue, value + step));
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

MAJOR EDIT: Problem was solved after reading Will Dean's comment. The original question is
[Major Edit based on experience since 1st post two days ago.] I am building
Major Edit: I misread the article! The comment was in regards to the finalize
EDIT: Now a Major Motion Blog Post at http://messymatters.com/sealedbids The idea of rot13 is
MAJOR EDIT I have restructured my question to be more appropriate and less arbitrary,
major edit of a tumbleweed, may remain a tumbleweed... If I have a list
// Major edit, sorry in bed with back pain, screwed up post One of
There are two major refactoring tools which can be installed for Visual Studio that
I have and add/edit form that adds/edits depending on whether there is an id
EDIT: Issue solved, answered it below. Lame error. Blah So I upgraded to Django

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.