Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 12 of 12

Thread: Questions about integer Overflow in Objects

  1. #1
    Junior Member
    Join Date
    Apr 2024
    Posts
    21
    Thanks
    6
    Thanked 1 Time in 1 Post

    Default Questions about integer Overflow in Objects

    Hello everybody.

    I'm having some problems understanding Overflow.

    I was solving the following exercise:




    Use the Rational class with long values) Write a program that
    computes the following summation series using the Rational class:


    1/2 + 2/3 + 3/4 + ... + 98/99 + 99/100


    You will discover that the output is incorrect because
    of integer overflow (too large).

    To fix this problem, see Programming Exercise 13.15 (Rational Class using BigIntegers)

    First of all, when I run the program it produces integer overflow.

    Also, the program is very slow to complete the task.

    It takes 3 minutes and 26 seconds to finish and the program "freezes" during the execution and then it "unfreezes" and then it continues.

    The majority of my programs up until now take less than 1 minute to finish.

    I was not surprised to see the integer overflow because of the exercise text.

    But because the program was taking too long, I´ve tried to see if I wrote something that was slowing down the exeuction of the program.

    I was trying to see if I could make the program run faster.

    The Rational class is a class that represents a rational number, using a numerator and a denominator.

    Both the numerator and the denominator are long variables.

    The Rational class extends the Number Class and implements the Comparable Interface for <Rational>.

    I've changed the Rational Class in the book in several ways.

    In the Constructor of the Rational Class:


    public Rational(long numerator, long denominator) throws Exception


    this Constructor throws a Custom Exception I've created:


     
    String exceptionMessage;
     
     
            if(denominator == 0) {
     
                exceptionMessage = "in a fraction, the denominator cannot be 0";
     
                System.out.println(exceptionMessage);
     
                throw new InvalidValues(exceptionMessage);
     
            }


    The Rational Class has a method doubleValue() that overrides the method in the Number class.


    The method doubleValue() returns the value of the Rational Numerator/Denominator as a double.


     
     
        /*
        Implement the abstract doubleValue method in Number
        */
     
        @Override
        public double doubleValue() {
     
            return getNumerator() * 1.0 / getDenominator();
     
        }


    The testSummation class is the Test Class.

    I start by defining the variables.



    int currentNumerator;

    int currentDenominator;


    double fractionsSum;



    Then I create the object sum and initiase the variables



    currentNumerator = 1;

    currentDenominator = 2;




    //Create and initialize two rational number sum

    Rational sum;



    sum = new Rational(currentNumerator, currentDenominator);


    fractionsSum = (double)currentNumerator/(double)currentDenominator;


    At this moment, both the double values of sum (Rational object) and fractionsSum (double variable) are the same.


    Then we have a While loop and I also write here what is afte the while loop


     
     
     
            while(currentNumerator < 99) {
     
     
     
                currentNumerator++;
     
                currentDenominator++;
     
     
     
                fractionsSum += (double)currentNumerator/(double)currentDenominator;
     
     
                System.out.println("Current Numerator " + currentNumerator + " and current Denominator " + currentDenominator);
     
     
                System.out.println("The current value of the sum using " + 
                        "Current Numerator and current Denominator is " + 
                        fractionsSum);
     
     
                sum = sum.add( new Rational( currentNumerator, currentDenominator) );
     
                System.out.println("The current value of the sum using " + 
                            "Rational Class is " + sum.doubleValue());
     
     
     
            } //end of while(currentNumerator < 100)
     
     
            System.out.println("The result of the sum using the sum of doubles is " + fractionsSum);
     
     
            System.out.println("The result of the sum using the Rational class is " + sum.doubleValue());


    I would like to say that I've already tested the program by removing the Rational class, that is by just using the fractionsSum variable inside the while loop and the program is much faster.

    Also the overflow only happens in the Rational class.

    I already know some situations where overflow happens.

    For example the maximum value of an integer is:


    2147483647


    So if i have:


    int a = 100000;

    int b = a;

    int c = a;

    int d;

    d = a * b * c;

    this will produce overflow because the multiplication exceeds the maximum for int.

    But in the programs I created, I don't understand why the overflow happens.

    And I don't understand why the overflow happens in the object of the Rational class and why it does not happen in the primitive double variable.

    This is the output and as you can see the values start the same for sum object and fractionsSum double variable.

    There are some variations at the last number due to the calculation methods used.

    Then, at Current Numerator 24 and current Denominator 25 the sum object starts overflowing but the double variable stays fine.


    Current Numerator 20 and current Denominator 21
    The current value of the sum using Current Numerator and current Denominator is 17.354641295237272
    The current value of the sum using Rational Class is 17.354641295237272
    Current Numerator 21 and current Denominator 22
    The current value of the sum using Current Numerator and current Denominator is 18.309186749782725
    The current value of the sum using Rational Class is 18.309186749782725
    Current Numerator 22 and current Denominator 23
    The current value of the sum using Current Numerator and current Denominator is 19.26570848891316
    The current value of the sum using Rational Class is 19.26570848891316
    Current Numerator 23 and current Denominator 24
    The current value of the sum using Current Numerator and current Denominator is 20.22404182224649
    The current value of the sum using Rational Class is 20.224041822246495
    Current Numerator 24 and current Denominator 25
    The current value of the sum using Current Numerator and current Denominator is 21.184041822246492
    The current value of the sum using Rational Class is 2.244902106795326
    Current Numerator 25 and current Denominator 26
    The current value of the sum using Current Numerator and current Denominator is 22.145580283784952
    The current value of the sum using Rational Class is -6.706479358929753
    Current Numerator 26 and current Denominator 27
    The current value of the sum using Current Numerator and current Denominator is 23.108543246747914
    The current value of the sum using Rational Class is -2.033851687488958


    So, I have a few questions:


    What is Overflow?

    In which situations Overflow might happen?

    In these programs why do we have overflow in the object of the Rational class ?

    In these programs, why is it that the double variable does not Overflow ?

    Why is it that the program takes so long to run with the object of the Rational class, but is much faster using only primitives ?


    Thank you,

    Rogério

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,088
    Thanks
    65
    Thanked 2,712 Times in 2,662 Posts

    Default Re: Questions about integer Overflow in Objects

    in the programs I created, I don't understand why the overflow happens.
    Can you post the code where the overflow happens?
    Also print out the values of all the variables used in the statement where there is the overflow.
    If you don't understand my answer, don't ignore it, ask a question.

  3. The Following User Says Thank You to Norm For This Useful Post:

    Rogerm (May 24th, 2024)

  4. #3
    Member
    Join Date
    Jan 2024
    Posts
    75
    Thanks
    0
    Thanked 4 Times in 4 Posts

    Default Re: Questions about integer Overflow in Objects

    Overflow occurs when a value exceeds the maximum range that can be represented by its data type. In Java, for integers, this typically happens when the value exceeds the maximum or minimum value that can be stored in the data type. For example, for the `int` data type, the maximum value is 2147483647, and if a calculation results in a value greater than this, overflow occurs.

    Overflow can happen in various situations, such as arithmetic operations like addition, subtraction, multiplication, or division, where the result exceeds the data type's range. It can also occur when converting between data types, or when using custom data types like the Rational class you're utilizing.

    In the Rational class object, overflow occurs because the numerator and denominator are of type `long`, but the class doesn't handle overflow scenarios gracefully. The calculations performed within the Rational class might result in values larger than what can be represented by a `long`, leading to overflow.

    On the other hand, the double variable does not overflow because the `double` data type has a much larger range compared to `long`. Doubles can represent a wider range of values, including very large or very small numbers, and they have built-in mechanisms to handle overflow by representing them as positive or negative infinity.

    The program takes longer to run with the Rational class objects compared to using only primitives because of the overhead involved in creating and manipulating objects. Object creation, method invocation, and memory allocation contribute to this overhead. Additionally, the Rational class may involve more complex arithmetic operations, which could further slow down the computation compared to simple primitive arithmetic.

    To resolve overflow issues and optimize the program's performance, exploring a Rational class implementation with BigIntegers could be beneficial. Additionally, improving the Rational class for efficiency and seeking programming assignment assistance from credible resources like ProgrammingHomeworkHelp.com may provide valuable insights.

  5. The Following User Says Thank You to patricajohnson51 For This Useful Post:

    Rogerm (May 24th, 2024)

  6. #4
    Junior Member
    Join Date
    Apr 2024
    Posts
    21
    Thanks
    6
    Thanked 1 Time in 1 Post

    Default Re: Questions about integer Overflow in Objects

    Hello Norm.

    Thank you for your answer.

    The overflow happens inside the While loop.

    I've posted the entire while loop in my first post.

    This is the output before the While loop.


    Current Numerator 1 and current Denominator 2
    The current value of the sum using Current Numerator and current Denominator Before the While loop is 0.5
    The current value of the sum Before the While loop using Rational Class is 0.5


    As you can see, the values for the double and for the Rational object are the same, that is 0,5

    I'm sorry but I'm already printing all the values of all the variables used in the While loop, including the value of the sum object using the sum.doubleValue().

    The method doubleValue() returns a double that is the representation of numerator/denominator.

    This is the output in the While loop, that is, starting from Current Numerator 2 and current Denominator 3 inclusive.



    Current Numerator 2 and current Denominator 3
    The current value of the sum using Current Numerator and current Denominator is 1.1666666666666665
    The current value of the sum using Rational Class is 1.1666666666666667
    Current Numerator 3 and current Denominator 4
    The current value of the sum using Current Numerator and current Denominator is 1.9166666666666665
    The current value of the sum using Rational Class is 1.9166666666666667
    Current Numerator 4 and current Denominator 5
    The current value of the sum using Current Numerator and current Denominator is 2.716666666666667
    The current value of the sum using Rational Class is 2.716666666666667
    Current Numerator 5 and current Denominator 6
    The current value of the sum using Current Numerator and current Denominator is 3.5500000000000003
    The current value of the sum using Rational Class is 3.55
    Current Numerator 6 and current Denominator 7
    The current value of the sum using Current Numerator and current Denominator is 4.4071428571428575
    The current value of the sum using Rational Class is 4.4071428571428575
    Current Numerator 7 and current Denominator 8
    The current value of the sum using Current Numerator and current Denominator is 5.2821428571428575
    The current value of the sum using Rational Class is 5.2821428571428575
    Current Numerator 8 and current Denominator 9
    The current value of the sum using Current Numerator and current Denominator is 6.171031746031746
    The current value of the sum using Rational Class is 6.171031746031746
    Current Numerator 9 and current Denominator 10
    The current value of the sum using Current Numerator and current Denominator is 7.071031746031746
    The current value of the sum using Rational Class is 7.071031746031746
    Current Numerator 10 and current Denominator 11
    The current value of the sum using Current Numerator and current Denominator is 7.980122655122655
    The current value of the sum using Rational Class is 7.980122655122655
    Current Numerator 11 and current Denominator 12
    The current value of the sum using Current Numerator and current Denominator is 8.896789321789322
    The current value of the sum using Rational Class is 8.896789321789322
    Current Numerator 12 and current Denominator 13
    The current value of the sum using Current Numerator and current Denominator is 9.819866244866246
    The current value of the sum using Rational Class is 9.819866244866246
    Current Numerator 13 and current Denominator 14
    The current value of the sum using Current Numerator and current Denominator is 10.748437673437675
    The current value of the sum using Rational Class is 10.748437673437673
    Current Numerator 14 and current Denominator 15
    The current value of the sum using Current Numerator and current Denominator is 11.681771006771008
    The current value of the sum using Rational Class is 11.681771006771006
    Current Numerator 15 and current Denominator 16
    The current value of the sum using Current Numerator and current Denominator is 12.619271006771008
    The current value of the sum using Rational Class is 12.619271006771006
    Current Numerator 16 and current Denominator 17
    The current value of the sum using Current Numerator and current Denominator is 13.560447477359244
    The current value of the sum using Rational Class is 13.560447477359242
    Current Numerator 17 and current Denominator 18
    The current value of the sum using Current Numerator and current Denominator is 14.504891921803688
    The current value of the sum using Rational Class is 14.504891921803686
    Current Numerator 18 and current Denominator 19
    The current value of the sum using Current Numerator and current Denominator is 15.45226034285632
    The current value of the sum using Rational Class is 15.452260342856318
    Current Numerator 19 and current Denominator 20
    The current value of the sum using Current Numerator and current Denominator is 16.40226034285632
    The current value of the sum using Rational Class is 16.40226034285632
    Current Numerator 20 and current Denominator 21
    The current value of the sum using Current Numerator and current Denominator is 17.354641295237272
    The current value of the sum using Rational Class is 17.354641295237272
    Current Numerator 21 and current Denominator 22
    The current value of the sum using Current Numerator and current Denominator is 18.309186749782725
    The current value of the sum using Rational Class is 18.309186749782725
    Current Numerator 22 and current Denominator 23
    The current value of the sum using Current Numerator and current Denominator is 19.26570848891316
    The current value of the sum using Rational Class is 19.26570848891316
    Current Numerator 23 and current Denominator 24
    The current value of the sum using Current Numerator and current Denominator is 20.22404182224649
    The current value of the sum using Rational Class is 20.224041822246495
    Current Numerator 24 and current Denominator 25
    The current value of the sum using Current Numerator and current Denominator is 21.184041822246492
    The current value of the sum using Rational Class is 2.244902106795326
    Current Numerator 25 and current Denominator 26
    The current value of the sum using Current Numerator and current Denominator is 22.145580283784952
    The current value of the sum using Rational Class is -6.706479358929753
    Current Numerator 26 and current Denominator 27
    The current value of the sum using Current Numerator and current Denominator is 23.108543246747914
    The current value of the sum using Rational Class is -2.033851687488958
    Current Numerator 27 and current Denominator 28
    The current value of the sum using Current Numerator and current Denominator is 24.07282896103363
    The current value of the sum using Rational Class is 0.006062540035017233
    Current Numerator 28 and current Denominator 29
    The current value of the sum using Current Numerator and current Denominator is 25.03834620241294
    The current value of the sum using Rational Class is 1.42837489101206
    Current Numerator 29 and current Denominator 30
    The current value of the sum using Current Numerator and current Denominator is 26.005012869079604
    The current value of the sum using Rational Class is 1.470596735846486
    Current Numerator 30 and current Denominator 31
    The current value of the sum using Current Numerator and current Denominator is 26.972754804563476
    The current value of the sum using Rational Class is -4.151081703988535
    Current Numerator 31 and current Denominator 32
    The current value of the sum using Current Numerator and current Denominator is 27.941504804563476
    The current value of the sum using Rational Class is 1.2683666964537086
    Current Numerator 32 and current Denominator 33
    The current value of the sum using Current Numerator and current Denominator is 28.911201774260444
    The current value of the sum using Rational Class is 0.144244205173329
    Current Numerator 33 and current Denominator 34
    The current value of the sum using Current Numerator and current Denominator is 29.88179000955456
    The current value of the sum using Rational Class is -0.7717032843878078
    Current Numerator 34 and current Denominator 35
    The current value of the sum using Current Numerator and current Denominator is 30.853218580983132
    The current value of the sum using Rational Class is 1.0717244429445294
    Current Numerator 35 and current Denominator 36
    The current value of the sum using Current Numerator and current Denominator is 31.825440803205353
    The current value of the sum using Rational Class is -0.9415266922456887
    Current Numerator 36 and current Denominator 37
    The current value of the sum using Current Numerator and current Denominator is 32.79841377617833
    The current value of the sum using Rational Class is 6.985654785572958
    Current Numerator 37 and current Denominator 38
    The current value of the sum using Current Numerator and current Denominator is 33.772097986704644
    The current value of the sum using Rational Class is -0.7700949498445708
    Current Numerator 38 and current Denominator 39
    The current value of the sum using Current Numerator and current Denominator is 34.746456961063615
    The current value of the sum using Rational Class is -1.2355967010803388
    Current Numerator 39 and current Denominator 40
    The current value of the sum using Current Numerator and current Denominator is 35.721456961063616
    The current value of the sum using Rational Class is -15.150881023069479
    Current Numerator 40 and current Denominator 41
    The current value of the sum using Current Numerator and current Denominator is 36.69706671716118
    The current value of the sum using Rational Class is -1.2100242575283577
    Current Numerator 41 and current Denominator 42
    The current value of the sum using Current Numerator and current Denominator is 37.67325719335165
    The current value of the sum using Rational Class is 0.10431637954906317
    Current Numerator 42 and current Denominator 43
    The current value of the sum using Current Numerator and current Denominator is 38.65000137939816
    The current value of the sum using Rational Class is -0.4956426970321874
    Current Numerator 43 and current Denominator 44
    The current value of the sum using Current Numerator and current Denominator is 39.62727410667089
    The current value of the sum using Rational Class is -3.8035572414368626
    Current Numerator 44 and current Denominator 45
    The current value of the sum using Current Numerator and current Denominator is 40.60505188444866
    The current value of the sum using Rational Class is -0.39001135489407945
    Current Numerator 45 and current Denominator 46
    The current value of the sum using Current Numerator and current Denominator is 41.58331275401388
    The current value of the sum using Rational Class is -0.2224257640848754
    Current Numerator 46 and current Denominator 47
    ..............
    it continues


    Thank you,

    Rogério

    --- Update ---

    Thank you, Patricia for your post.

    The explanation for the program being so slow is correct.

    The Rational class is immutable, and so, every time a new rational(currentNumerator, currentDenominator) is added to the previous sum object, a new Rational object is created for that new fraction that is to be added.

    Also when I do

    sum = sum.add( new rational(currentNumerator, currentDenominator) )

    A new Rational object is created with the sum of the previous sum object with the new fraction.

    So, a new rational object is created a lot of times.

    Thank you,

    Rogério

  7. #5
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,088
    Thanks
    65
    Thanked 2,712 Times in 2,662 Posts

    Default Re: Questions about integer Overflow in Objects

    The overflow happens inside the While loop.
    Sorry, I do not see where you are talking about. Where is the results of the overflow shown?
    Please add some comments to the print out showing what you are asking about.
    For example, add this at the line: <<<<<<<<<<<< Here is the overflow

    I can not test the while loop because I do not have the Rational class's definition.
    If you don't understand my answer, don't ignore it, ask a question.

  8. #6
    Junior Member
    Join Date
    Apr 2024
    Posts
    21
    Thanks
    6
    Thanked 1 Time in 1 Post

    Default Re: Questions about integer Overflow in Objects

    Hello Norm.

    Thank you for your quick answer.

    I apologise for my delay in answering to you, but I've been having a lot of extra hours at work.

    This is the Output as you have requested:




    This is the Output at the beginning before the While loop
    The two values are the same


    Current Numerator 1 and current Denominator 2
    The current value of the sum using Current Numerator and current Denominator Before the While loop is 0.5
    The current value of the sum Before the While loop using Rational Class is 0.5


    The While loop begins and the following lines
    are created inside the While Loop


    Current Numerator 2 and current Denominator 3
    The current value of the sum using Current Numerator and current Denominator is 1.1666666666666665
    The current value of the sum using Rational Class is 1.1666666666666667
    Current Numerator 3 and current Denominator 4
    The current value of the sum using Current Numerator and current Denominator is 1.9166666666666665
    The current value of the sum using Rational Class is 1.9166666666666667
    Current Numerator 4 and current Denominator 5
    The current value of the sum using Current Numerator and current Denominator is 2.716666666666667
    The current value of the sum using Rational Class is 2.716666666666667
    Current Numerator 5 and current Denominator 6
    The current value of the sum using Current Numerator and current Denominator is 3.5500000000000003
    The current value of the sum using Rational Class is 3.55
    Current Numerator 6 and current Denominator 7
    The current value of the sum using Current Numerator and current Denominator is 4.4071428571428575
    The current value of the sum using Rational Class is 4.4071428571428575
    Current Numerator 7 and current Denominator 8
    The current value of the sum using Current Numerator and current Denominator is 5.2821428571428575
    The current value of the sum using Rational Class is 5.2821428571428575
    Current Numerator 8 and current Denominator 9
    The current value of the sum using Current Numerator and current Denominator is 6.171031746031746
    The current value of the sum using Rational Class is 6.171031746031746
    Current Numerator 9 and current Denominator 10
    The current value of the sum using Current Numerator and current Denominator is 7.071031746031746
    The current value of the sum using Rational Class is 7.071031746031746
    Current Numerator 10 and current Denominator 11
    The current value of the sum using Current Numerator and current Denominator is 7.980122655122655
    The current value of the sum using Rational Class is 7.980122655122655
    Current Numerator 11 and current Denominator 12
    The current value of the sum using Current Numerator and current Denominator is 8.896789321789322
    The current value of the sum using Rational Class is 8.896789321789322
    Current Numerator 12 and current Denominator 13
    The current value of the sum using Current Numerator and current Denominator is 9.819866244866246
    The current value of the sum using Rational Class is 9.819866244866246
    Current Numerator 13 and current Denominator 14
    The current value of the sum using Current Numerator and current Denominator is 10.748437673437675
    The current value of the sum using Rational Class is 10.748437673437673
    Current Numerator 14 and current Denominator 15
    The current value of the sum using Current Numerator and current Denominator is 11.681771006771008
    The current value of the sum using Rational Class is 11.681771006771006
    Current Numerator 15 and current Denominator 16
    The current value of the sum using Current Numerator and current Denominator is 12.619271006771008
    The current value of the sum using Rational Class is 12.619271006771006
    Current Numerator 16 and current Denominator 17
    The current value of the sum using Current Numerator and current Denominator is 13.560447477359244
    The current value of the sum using Rational Class is 13.560447477359242
    Current Numerator 17 and current Denominator 18
    The current value of the sum using Current Numerator and current Denominator is 14.504891921803688
    The current value of the sum using Rational Class is 14.504891921803686
    Current Numerator 18 and current Denominator 19
    The current value of the sum using Current Numerator and current Denominator is 15.45226034285632
    The current value of the sum using Rational Class is 15.452260342856318
    Current Numerator 19 and current Denominator 20
    The current value of the sum using Current Numerator and current Denominator is 16.40226034285632
    The current value of the sum using Rational Class is 16.40226034285632
    Current Numerator 20 and current Denominator 21
    The current value of the sum using Current Numerator and current Denominator is 17.354641295237272
    The current value of the sum using Rational Class is 17.354641295237272
    Current Numerator 21 and current Denominator 22
    The current value of the sum using Current Numerator and current Denominator is 18.309186749782725
    The current value of the sum using Rational Class is 18.309186749782725
    Current Numerator 22 and current Denominator 23
    The current value of the sum using Current Numerator and current Denominator is 19.26570848891316
    The current value of the sum using Rational Class is 19.26570848891316
    Current Numerator 23 and current Denominator 24
    The current value of the sum using Current Numerator and current Denominator is 20.22404182224649
    The current value of the sum using Rational Class is 20.224041822246495



    In here the value of the sum using Rational Class Starts OverFlow
    <----------------------------------------------------------------------------------------------------------------------------

    Current Numerator 24 and current Denominator 25
    The current value of the sum using Current Numerator and current Denominator is 21.184041822246492
    The current value of the sum using Rational Class is 2.244902106795326





    Current Numerator 25 and current Denominator 26
    The current value of the sum using Current Numerator and current Denominator is 22.145580283784952
    The current value of the sum using Rational Class is -6.706479358929753
    Current Numerator 26 and current Denominator 27
    The current value of the sum using Current Numerator and current Denominator is 23.108543246747914
    The current value of the sum using Rational Class is -2.033851687488958
    Current Numerator 27 and current Denominator 28
    The current value of the sum using Current Numerator and current Denominator is 24.07282896103363
    The current value of the sum using Rational Class is 0.006062540035017233
    Current Numerator 28 and current Denominator 29
    The current value of the sum using Current Numerator and current Denominator is 25.03834620241294
    The current value of the sum using Rational Class is 1.42837489101206
    Current Numerator 29 and current Denominator 30
    The current value of the sum using Current Numerator and current Denominator is 26.005012869079604
    The current value of the sum using Rational Class is 1.470596735846486
    Current Numerator 30 and current Denominator 31
    The current value of the sum using Current Numerator and current Denominator is 26.972754804563476
    The current value of the sum using Rational Class is -4.151081703988535
    Current Numerator 31 and current Denominator 32
    The current value of the sum using Current Numerator and current Denominator is 27.941504804563476
    The current value of the sum using Rational Class is 1.2683666964537086
    Current Numerator 32 and current Denominator 33
    The current value of the sum using Current Numerator and current Denominator is 28.911201774260444
    The current value of the sum using Rational Class is 0.144244205173329
    Current Numerator 33 and current Denominator 34
    The current value of the sum using Current Numerator and current Denominator is 29.88179000955456
    The current value of the sum using Rational Class is -0.7717032843878078
    Current Numerator 34 and current Denominator 35
    The current value of the sum using Current Numerator and current Denominator is 30.853218580983132
    The current value of the sum using Rational Class is 1.0717244429445294
    Current Numerator 35 and current Denominator 36
    The current value of the sum using Current Numerator and current Denominator is 31.825440803205353
    The current value of the sum using Rational Class is -0.9415266922456887
    Current Numerator 36 and current Denominator 37
    The current value of the sum using Current Numerator and current Denominator is 32.79841377617833
    The current value of the sum using Rational Class is 6.985654785572958
    Current Numerator 37 and current Denominator 38
    The current value of the sum using Current Numerator and current Denominator is 33.772097986704644
    The current value of the sum using Rational Class is -0.7700949498445708
    Current Numerator 38 and current Denominator 39
    The current value of the sum using Current Numerator and current Denominator is 34.746456961063615
    The current value of the sum using Rational Class is -1.2355967010803388
    Current Numerator 39 and current Denominator 40
    The current value of the sum using Current Numerator and current Denominator is 35.721456961063616
    The current value of the sum using Rational Class is -15.150881023069479
    Current Numerator 40 and current Denominator 41
    The current value of the sum using Current Numerator and current Denominator is 36.69706671716118
    The current value of the sum using Rational Class is -1.2100242575283577
    Current Numerator 41 and current Denominator 42
    The current value of the sum using Current Numerator and current Denominator is 37.67325719335165
    The current value of the sum using Rational Class is 0.10431637954906317
    Current Numerator 42 and current Denominator 43
    The current value of the sum using Current Numerator and current Denominator is 38.65000137939816
    The current value of the sum using Rational Class is -0.4956426970321874
    Current Numerator 43 and current Denominator 44
    The current value of the sum using Current Numerator and current Denominator is 39.62727410667089
    The current value of the sum using Rational Class is -3.8035572414368626
    Current Numerator 44 and current Denominator 45
    The current value of the sum using Current Numerator and current Denominator is 40.60505188444866
    The current value of the sum using Rational Class is -0.39001135489407945
    Current Numerator 45 and current Denominator 46
    The current value of the sum using Current Numerator and current Denominator is 41.58331275401388
    The current value of the sum using Rational Class is -0.2224257640848754
    Current Numerator 46 and current Denominator 47
    The current value of the sum using Current Numerator and current Denominator is 42.5620361582692
    The current value of the sum using Rational Class is -0.8730756350802217
    Current Numerator 47 and current Denominator 48
    The current value of the sum using Current Numerator and current Denominator is 43.541202824935866
    The current value of the sum using Rational Class is 0.24034092898978632
    Current Numerator 48 and current Denominator 49
    The current value of the sum using Current Numerator and current Denominator is 44.52079466167056
    The current value of the sum using Rational Class is 0.23925261969396472
    Current Numerator 49 and current Denominator 50
    The current value of the sum using Current Numerator and current Denominator is 45.500794661670554
    The current value of the sum using Rational Class is 3.6110715750704716
    Current Numerator 50 and current Denominator 51
    The current value of the sum using Current Numerator and current Denominator is 46.481186818533295
    The current value of the sum using Rational Class is 1.222749918523833
    Current Numerator 51 and current Denominator 52
    The current value of the sum using Current Numerator and current Denominator is 47.46195604930253
    The current value of the sum using Rational Class is -0.3399407472093353
    Current Numerator 52 and current Denominator 53
    The current value of the sum using Current Numerator and current Denominator is 48.44308812477423
    The current value of the sum using Rational Class is -1.1293221449181956
    Current Numerator 53 and current Denominator 54
    The current value of the sum using Current Numerator and current Denominator is 49.42456960625571
    The current value of the sum using Rational Class is -0.9192684188814331
    Current Numerator 54 and current Denominator 55
    The current value of the sum using Current Numerator and current Denominator is 50.40638778807389
    The current value of the sum using Rational Class is -1.4093599988814314
    Current Numerator 55 and current Denominator 56
    The current value of the sum using Current Numerator and current Denominator is 51.38853064521675
    The current value of the sum using Rational Class is 1.4271152513485337
    Current Numerator 56 and current Denominator 57
    The current value of the sum using Current Numerator and current Denominator is 52.370986785567624
    The current value of the sum using Rational Class is 8.568269595154918
    Current Numerator 57 and current Denominator 58
    The current value of the sum using Current Numerator and current Denominator is 53.35374540625728
    The current value of the sum using Rational Class is 0.24137640766212334
    Current Numerator 58 and current Denominator 59
    The current value of the sum using Current Numerator and current Denominator is 54.33679625371491
    The current value of the sum using Rational Class is 2.6776845354940506
    Current Numerator 59 and current Denominator 60
    The current value of the sum using Current Numerator and current Denominator is 55.32012958704824
    The current value of the sum using Rational Class is 3.906495522959362
    Current Numerator 60 and current Denominator 61
    The current value of the sum using Current Numerator and current Denominator is 56.303736144425294
    The current value of the sum using Rational Class is 2.807678144233804
    Current Numerator 61 and current Denominator 62
    The current value of the sum using Current Numerator and current Denominator is 57.28760711216723
    The current value of the sum using Rational Class is 1.4923768475022092
    Current Numerator 62 and current Denominator 63
    The current value of the sum using Current Numerator and current Denominator is 58.27173409629421
    The current value of the sum using Rational Class is -0.504117122732608
    Current Numerator 63 and current Denominator 64
    The current value of the sum using Current Numerator and current Denominator is 59.25610909629421
    The current value of the sum using Rational Class is -0.864599142861581
    Current Numerator 64 and current Denominator 65
    The current value of the sum using Current Numerator and current Denominator is 60.24072448090959
    The current value of the sum using Rational Class is -1.544059869941245
    Current Numerator 65 and current Denominator 66
    The current value of the sum using Current Numerator and current Denominator is 61.225572965758076
    The current value of the sum using Rational Class is 1.2860078783505142
    Current Numerator 66 and current Denominator 67
    The current value of the sum using Current Numerator and current Denominator is 62.21064759262375
    The current value of the sum using Rational Class is 1.4131421424405886
    Current Numerator 67 and current Denominator 68
    The current value of the sum using Current Numerator and current Denominator is 63.19594171027081
    The current value of the sum using Rational Class is 1.7957022210909694
    Current Numerator 68 and current Denominator 69
    The current value of the sum using Current Numerator and current Denominator is 64.18144895664761
    The current value of the sum using Rational Class is -4.663975146370067
    Current Numerator 69 and current Denominator 70
    The current value of the sum using Current Numerator and current Denominator is 65.1671632423619
    The current value of the sum using Rational Class is -1.501126418616121
    Current Numerator 70 and current Denominator 71
    The current value of the sum using Current Numerator and current Denominator is 66.15307873531964
    The current value of the sum using Rational Class is -0.026152471596059473
    Current Numerator 71 and current Denominator 72
    The current value of the sum using Current Numerator and current Denominator is 67.13918984643075
    The current value of the sum using Rational Class is -0.23320492483701427
    Current Numerator 72 and current Denominator 73
    The current value of the sum using Current Numerator and current Denominator is 68.12549121629377
    The current value of the sum using Rational Class is 0.10344170314010424
    Current Numerator 73 and current Denominator 74
    The current value of the sum using Current Numerator and current Denominator is 69.11197770278025
    The current value of the sum using Rational Class is -0.4227039377185784
    Current Numerator 74 and current Denominator 75
    The current value of the sum using Current Numerator and current Denominator is 70.09864436944692
    The current value of the sum using Rational Class is 0.7316210868613312
    Current Numerator 75 and current Denominator 76
    The current value of the sum using Current Numerator and current Denominator is 71.08548647471008
    The current value of the sum using Rational Class is -2.291721609587989
    Current Numerator 76 and current Denominator 77
    The current value of the sum using Current Numerator and current Denominator is 72.07249946172307
    The current value of the sum using Rational Class is -6.557710011274694
    Current Numerator 77 and current Denominator 78
    The current value of the sum using Current Numerator and current Denominator is 73.05967894890256
    The current value of the sum using Rational Class is -0.37463775202054095
    Current Numerator 78 and current Denominator 79
    The current value of the sum using Current Numerator and current Denominator is 74.04702072105445
    The current value of the sum using Rational Class is 2.570300603932491
    Current Numerator 79 and current Denominator 80
    The current value of the sum using Current Numerator and current Denominator is 75.03452072105445
    The current value of the sum using Rational Class is -0.25200304568297144
    Current Numerator 80 and current Denominator 81
    The current value of the sum using Current Numerator and current Denominator is 76.02217504204211
    The current value of the sum using Rational Class is -3.717432676898014
    Current Numerator 81 and current Denominator 82
    The current value of the sum using Current Numerator and current Denominator is 77.00997992009088
    The current value of the sum using Rational Class is -0.8569758138656041
    Current Numerator 82 and current Denominator 83
    The current value of the sum using Current Numerator and current Denominator is 77.9979317273198
    The current value of the sum using Rational Class is -0.10525459511493448
    Current Numerator 83 and current Denominator 84
    The current value of the sum using Current Numerator and current Denominator is 78.98602696541504
    The current value of the sum using Rational Class is -0.9921254768566289
    Current Numerator 84 and current Denominator 85
    The current value of the sum using Current Numerator and current Denominator is 79.97426225953268
    The current value of the sum using Rational Class is -0.33695291864638055
    Current Numerator 85 and current Denominator 86
    The current value of the sum using Current Numerator and current Denominator is 80.96263435255594
    The current value of the sum using Rational Class is 0.28577802474417385
    Current Numerator 86 and current Denominator 87
    The current value of the sum using Current Numerator and current Denominator is 81.95114009968238
    The current value of the sum using Rational Class is -0.35869189862280454
    Current Numerator 87 and current Denominator 88
    The current value of the sum using Current Numerator and current Denominator is 82.93977646331874
    The current value of the sum using Rational Class is 2.8214946604749267
    Current Numerator 88 and current Denominator 89
    The current value of the sum using Current Numerator and current Denominator is 83.92854050826256
    The current value of the sum using Rational Class is 2.271670934774492
    Current Numerator 89 and current Denominator 90
    The current value of the sum using Current Numerator and current Denominator is 84.91742939715145
    The current value of the sum using Rational Class is -0.28302798928631834
    Current Numerator 90 and current Denominator 91
    The current value of the sum using Current Numerator and current Denominator is 85.90644038616244
    The current value of the sum using Rational Class is -1.547888097617871
    Current Numerator 91 and current Denominator 92
    The current value of the sum using Current Numerator and current Denominator is 86.89557082094505
    The current value of the sum using Rational Class is 213.10733956923443
    Current Numerator 92 and current Denominator 93
    The current value of the sum using Current Numerator and current Denominator is 87.884818132773
    The current value of the sum using Rational Class is -4.03777241789539
    Current Numerator 93 and current Denominator 94
    The current value of the sum using Current Numerator and current Denominator is 88.87417983490066
    The current value of the sum using Rational Class is -11.855974653294973
    Current Numerator 94 and current Denominator 95
    The current value of the sum using Current Numerator and current Denominator is 89.86365351911118
    The current value of the sum using Rational Class is -2.2551816829946203
    Current Numerator 95 and current Denominator 96
    The current value of the sum using Current Numerator and current Denominator is 90.85323685244451
    The current value of the sum using Rational Class is 1.7421202983822626
    Current Numerator 96 and current Denominator 97
    The current value of the sum using Current Numerator and current Denominator is 91.84292757409399
    The current value of the sum using Rational Class is 1.4115351890189618
    Current Numerator 97 and current Denominator 98
    The current value of the sum using Current Numerator and current Denominator is 92.83272349246134
    The current value of the sum using Rational Class is -0.3681614386035402
    Current Numerator 98 and current Denominator 99
    The current value of the sum using Current Numerator and current Denominator is 93.82262248236033
    The current value of the sum using Rational Class is 0.2261789513772669
    Current Numerator 99 and current Denominator 100
    The current value of the sum using Current Numerator and current Denominator is 94.81262248236033
    The current value of the sum using Rational Class is 2.2743105916772293


    The next lines are after the While Loop
    <-----------------------------------------------------------------------------------------------

    The result of the sum using the sum of doubles is 94.81262248236033
    The result of the sum using the Rational class is 2.2743105916772293


    Thank you,

    Rogério

    --- Update ---

    Hello again.

    I'm posting here the entire code of the Rational Class using long values.

    Thank you

    Rogério


     
     
    /*
     * To change this license header, choose License Headers in Project Properties.
     * To change this template file, choose Tools | Templates
     * and open the template in the editor.
     */
    package Chapter13.EndofChapter13.Exerc13dot18;
     
     
    /**
     *
     * @author Rogerio Biscaia
     */
     
     
     
    public class Rational extends Number implements Comparable<Rational> {
     
     
     
        //Data fields for numerator and denominator
        private long numerator;
        private long denominator;
     
     
        /*
        Construct a rational with
        numerator 0 and
        denominator 1
        */
     
        public Rational() throws Exception {
     
            this(0, 1);
     
        }
     
     
     
        /*
        Construct a rational with
        specified numerator and
        denominator
        */
     
        public Rational(long numerator, long denominator) throws Exception {
     
     
            long gcd;
     
            String exceptionMessage;
     
     
            if(denominator == 0) {
     
                exceptionMessage = "in a fraction, the denominator cannot be 0";
     
                System.out.println(exceptionMessage);
     
                throw new InvalidValues(exceptionMessage);
     
            }
     
     
            gcd = gcd(numerator, denominator);
     
     
            this.numerator = ( (denominator > 0) ? 1 : -1 ) * numerator / gcd;
     
     
            this.denominator = Math.abs(denominator) / gcd;
     
     
            //System.out.println("Created Rational object");
     
     
        } //end of the constructor public Rational(long numerator, long denominator)
     
     
     
     
        /*
        Find the GCD - Greatest Common Divisor
        of two numbers
        */
     
        private static long gcd(long n, long d) {
     
     
            long n1 = Math.abs(n);
     
            long n2 = Math.abs(d);
     
     
            long gcd;
     
     
            int k;
     
     
            gcd = 1;
     
     
            k = 2;
     
     
            while( k <= n1 && k <= n2 ) {
     
     
                if(n1 % k == 0 && n2 % k == 0) {
     
                    gcd = k;
     
                    break;
     
                }
     
                k++;
     
            } //end of while( k <= n1 && k <= n2 )
     
     
            return gcd;
     
     
        } //end of the method private static long gcd(long n, long d)
     
     
     
        /*
        Return numerator
        */
     
        public long getNumerator() {
     
            return numerator;
     
        }
     
     
        /*
        Return denominator
        */
     
        public long getDenominator() {
     
            return denominator;
     
        }
     
     
        /*
        Add a rational number to this rational
        */
     
        public Rational add(Rational secondRational) throws Exception {
     
     
            long n, d;
     
            n = getNumerator() * secondRational.getDenominator() +
                    getDenominator() * secondRational.getNumerator();
     
     
            d = getDenominator() * secondRational.getDenominator();
     
     
            return new Rational(n, d);
     
     
        } //end of the method public Rational add(Rational secondRational)
     
     
     
     
        /*
        Subtract a rational number from this rational
        */
     
     
        public Rational subtract(Rational secondRational) throws Exception {
     
     
            long n, d;
     
            n = getNumerator() * secondRational.getDenominator() -
                    getDenominator() * secondRational.getNumerator();
     
     
            d = getDenominator() * secondRational.getDenominator();
     
     
            return new Rational(n, d);
     
     
        } //end of the method public Rational add(Rational secondRational)
     
     
     
        /*
        Multiply a rational number by this rational
        */
     
        public Rational multiply(Rational secondRational) throws Exception {
     
            long n, d;
     
            n = getNumerator() * secondRational.getNumerator();
     
            d = getDenominator() * secondRational.getDenominator();
     
     
            return new Rational(n, d);
     
     
        } //end of the method public Rational multiply(Rational secondRational)
     
     
     
        /*
        Divide a rational number by this rational
        */
     
        public Rational divide(Rational secondRational) throws Exception {
     
            long n, d;
     
            n = getNumerator() * secondRational.getDenominator();
     
            d = getDenominator() * secondRational.getNumerator();
     
     
            return new Rational(n, d);
     
        } //end of the method public Rational divide(Rational secondRational)
     
     
        /*
        Override toString
        */
     
        @Override
        public String toString() {
     
            if(getDenominator() == 1) {
     
                return getNumerator() + "";
     
            } else {
     
                return getNumerator() + "/" + getDenominator();
     
            }
     
     
        } //end of the method public String toString()
     
     
     
        /*
        It is not possible to
        Override the
        equals method as it was in
        the Object class
        because the new method throws Exception
        */
     
        @Override
        public boolean equals(Object other) {
     
            boolean equalObjects;
     
            equalObjects = false;
     
     
            try {
     
     
                if(other instanceof Rational) {
     
                    equalObjects = objectsEquals(other);
     
                }
     
     
     
            } catch(Exception ex1) {
     
                System.out.println(ex1.toString());
     
            }
     
     
            return equalObjects;
     
        }
     
     
     
     
        private boolean objectsEquals(Object other) throws Exception {
     
            if( this.subtract( (Rational) (other) ).getNumerator() == 0  ) {
     
                return true;
     
            } else {
     
                return false;
     
            }
     
     
        } //end of the method public boolean equals(Object other)
     
     
     
        /*
        Implement the abstract inValue method in Number
        */
     
        @Override
        public int intValue() {
     
            return (int)doubleValue();
     
        }
     
     
        /*
        Implement the abstract floatValue method in Number
        */
     
        @Override
        public float floatValue() {
     
            return (float)doubleValue();
     
        }
     
     
     
        /*
        Implement the abstract doubleValue method in Number
        */
     
        @Override
        public double doubleValue() {
     
            return getNumerator() * 1.0 / getDenominator();
     
        }
     
     
     
     
        /*
        Implement the abstract longValue method in Number
        */
     
        @Override
        public long longValue() {
     
            return (long)doubleValue();
     
        }
     
     
     
        /*
        Implement the compareTo method in Comparable
        */
     
        @Override
        public int compareTo(Rational o) {
     
            int testResult;
     
            testResult = 0;
     
            try {
     
                testResult = compareTwoRationals(o);
     
            } catch(Exception ex1) {
     
                System.out.println(ex1.toString());
     
            }
     
            return testResult;
     
        }
     
     
     
        private int compareTwoRationals(Rational o) throws Exception {
     
     
            if(this.subtract(o).getNumerator() > 0  ) {
     
                return 1;
     
            } else if(this.subtract(o).getNumerator() < 0) {
     
                return -1;
     
            } else {
     
                return 0;
     
            }
     
     
     
        } //end of the method public int compareTwoRationals(Rational o)
     
     
     
     
    } //end of the class Rational

  9. #7
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,088
    Thanks
    65
    Thanked 2,712 Times in 2,662 Posts

    Default Re: Questions about integer Overflow in Objects

    Thanks for the Rational class.
    I suggest you do some debugging. I use print statements to debug.
    Add print statements in the Rational class the print out the values of various variables like numerator, etc to see if their values are what you expect.
    Cut down the looping by exiting while loop when the sum goes negative.
    If you don't understand my answer, don't ignore it, ask a question.

  10. #8
    Junior Member
    Join Date
    Apr 2024
    Posts
    21
    Thanks
    6
    Thanked 1 Time in 1 Post

    Default Re: Questions about integer Overflow in Objects

    Thank you, Norm.

    I will do that.

  11. #9
    Junior Member
    Join Date
    Apr 2024
    Posts
    21
    Thanks
    6
    Thanked 1 Time in 1 Post

    Default Re: Questions about integer Overflow in Objects

    Hello everybody.

    I've changed the Rational Class to use BigInteger objects.

    The problems are now solved.

    Thank you,

    Rogério

  12. #10
    Member
    Join Date
    Jan 2024
    Posts
    75
    Thanks
    0
    Thanked 4 Times in 4 Posts

    Default Re: Questions about integer Overflow in Objects

    Overflow Explanation:

    Overflow occurs when the result of an arithmetic operation exceeds the maximum or minimum value that can be represented by the data type. In Java, for example, integers have a maximum value of 2147483647. When an operation exceeds this value, it "wraps around" to a negative number, causing unexpected behavior.

    Situations Leading to Overflow:

    Overflow can happen in various situations, typically involving arithmetic operations like addition, subtraction, multiplication, or division. It's particularly common when dealing with large numbers or long series of calculations.

    Overflow in the Rational Class Object:

    In your program, overflow occurs in the Rational class object because the values stored in long variables are reaching beyond their capacity during the arithmetic operations performed internally by the Rational class methods.

    Double Variable vs. Overflow:

    The double variable does not overflow because doubles have a much larger range compared to long integers. Doubles can represent values from approximately 4.9e-324 to 1.8e+308, so they can handle the calculations without overflowing.

    Performance Difference:

    The program takes longer to run with the Rational class object because dealing with objects involves additional overhead compared to primitive types. Each operation on Rational objects involves method calls and memory allocation, which adds up and slows down the execution, especially when dealing with a large number of iterations like in your summation series.

    Solution:

    To fix the overflow issue and improve performance, as suggested in the exercise, you can modify the Rational class to use BigIntegers instead of long integers. BigIntegers can handle arbitrarily large integers without overflowing, providing accurate results for your calculations. Additionally, optimizing the implementation of the Rational class methods can also help improve performance.

    To address the overflow issue and enhance performance, consider revising the Rational class to utilize BigIntegers instead of long integers. BigIntegers can accommodate significantly larger integers without encountering overflow problems, ensuring precise outcomes for your calculations. Additionally, refining the implementation of the Rational class methods could contribute to performance optimization. If you require further help with programming assignment, seeking guidance from reputable educational resources or online communities dedicated to programming support, such as programminghomeworkhelp.com, can be beneficial.

  13. The Following User Says Thank You to patricajohnson51 For This Useful Post:

    Rogerm (June 17th, 2024)

  14. #11
    Junior Member
    Join Date
    Apr 2024
    Posts
    21
    Thanks
    6
    Thanked 1 Time in 1 Post

    Default Re: Questions about integer Overflow in Objects

    Thank you Patricia for your detailed answer.

    As I have posted in a previous answer, I've already changed the Rational Class to use BigInteger class objects.

    After that the problems were solved and it did not Overflow.

    Rogério Biscaia

  15. #12
    Member Helium c2's Avatar
    Join Date
    Nov 2023
    Location
    Kekaha, Kaua'i
    Posts
    116
    Thanks
    1
    Thanked 3 Times in 3 Posts

    Default Re: Questions about integer Overflow in Objects

    Well I had a similar case. But in another forum. The numbers got to big. Looking at the problems, this summation may require more rounding if that's what you mean. Anyway I tried a math program in Python, 1,000,000 factorial !. And the computer didn't bother with computing anything. It just says number too large and out of range. Java is a different programming language, but the same functions in computing are still applicable. So I'm not sure how this limits in decimals to the right of the number can be measured that high up or that far into the summation with accuracy. Even looking at it calculus, this is hard to see also. But it will get the general idea. Programming then is still not yet attainable.

  16. The Following User Says Thank You to Helium c2 For This Useful Post:

    Rogerm (July 23rd, 2024)

Similar Threads

  1. Stack Overflow
    By omerben in forum Computer Support
    Replies: 1
    Last Post: June 10th, 2021, 05:34 AM
  2. How to get overflow in a autocomplete row.
    By koeneman in forum Other Programming Languages
    Replies: 1
    Last Post: September 26th, 2018, 04:53 AM
  3. Dealing With Overflow?
    By linzylu1190 in forum Loops & Control Statements
    Replies: 1
    Last Post: May 23rd, 2013, 07:40 PM
  4. [SOLVED] Accessing && Editing properties of objects in an array. Plus a few more questions.
    By CameronFaust in forum Collections and Generics
    Replies: 31
    Last Post: August 10th, 2011, 07:35 PM
  5. doing arithmetic on Integer objects
    By gib65 in forum Java Theory & Questions
    Replies: 4
    Last Post: October 30th, 2010, 09:22 PM