user2638464
user2638464

Reputation:

Issue with Java simple arithmetic

I'm working on a small Java program, and somewhere, my calculations are going awry. My code is as follows:

import java.io.*;
import java.util.*;
import java.text.*;

public class NumManip
{

    public static void main(String args[])
    {

        Scanner numGetter = new Scanner(System.in);
        final int MULT_1 = 9;
        final int MULT_2 = 12345679;
        final int MULT_3 = 1000;
        int poorHatedNumber;

        System.out.print("Enter a digit between 1 and 9 that you dislike: ");
        poorHatedNumber = numGetter.nextInt();
        int num1 = poorHatedNumber * MULT_1, num2 = num1 * MULT_2;
        long num3 = num2 * MULT_3;

        System.out.println();

        System.out.println("           " + poorHatedNumber);
        System.out.println("          " + "x" + MULT_1);
        System.out.println("          __");
        System.out.println("          " + num1);
        System.out.println("   x" + MULT_2);
        System.out.println("          __");
        System.out.println("   " + num2);
        System.out.println("       x" + MULT_3);
        System.out.println("____________");
        System.out.println(num3);

    }

}

I've tryed printing num1, num2, and num3 on the screen to see what the problem is, and num1 is right, num2 is right, and num3 is freaky. My input is 9, and the first calculation multiplies by 9 and gets 81. Then it multiplies that by 12345679 and gets 999999999, and then it multiplies by 1000 and gets -727380968. What's wrong with that last step? I'm REALLY new to Java, and I don't get the issue.

Upvotes: 1

Views: 147

Answers (4)

user555045
user555045

Reputation: 64913

Don't worry, it's not such a silly mistake really. The whole "numbers are usually a fixed size"-deal confuses just about everyone initially. Now that you know what's up, here's something even weirder. It's not really an answer to your question, but now that you've just seen an example of "bad overflow", you might find this interesting.

Consider an odd number x. There is a number y such that x * y == 1. That number y is called the modular multiplicative inverse, and it can easily be computed (see Hacker's Delight, exact division by a constant). This may seem really counter intuitive, because it essentially allows a weird kind "division" that only works if the number was an exact multiple of the divisor, and in general it allows you to "undo" a multiplication by an odd number. For example, if you have a = 3 * b, then b = -1431655765 * a - regardless of any overflow in either multiplication, so overflow need not be "destructive".

Upvotes: 0

Aaron
Aaron

Reputation: 650

I think you're resulting in a number bigger than the datatype can handle, and as the datatype is signed, it wraps around into the negatives.

Upvotes: 0

Subhrajyoti Majumder
Subhrajyoti Majumder

Reputation: 41230

As num2 and num1 both are integer, so integer multiplication happened and it exceeds the max of integer value.

long num3 = (long)num2 * MULT_3;

Upvotes: 0

Nir Alfasi
Nir Alfasi

Reputation: 53545

999999999 * 12345679 = 1.234567898765432e+16 which is way bigger than the maximum value of an int which is 2,147,483,647

Since Java uses 2-compliment method to store int number (meaning that the leftmost bit is turned on when the number is negative) this calculation "overflows" (carry-over) to the that bit which results in a negative result.

For calculation with such big numbers you should use BigDecimal

Upvotes: 1

Related Questions