-2

I have a python program which should multiply 2 big integers.

The problem is I get a different result in java with BigInteger class with same input.

I have tried DecInt library for python but gives the same answer as using pure python.

Here are my variables:

d = 372049305848826709205673800090501485720867768816

r = 5452188953055713107393819158892374332916586527736541041226026450382

Result I get in python from d * r:

2028483115341019294875069650745272851135156323450218238187883716036516369477015140871224045070868977706272670887712

Result I get in java with BigInteger class:

9530687378863294988874153740700860249994095546182028483115341019294875069650745272851135156323450218238187883716036516369477015140871224045070868977706272670887712

Here is my java code:

BigInteger d = new BigInteger("372049305848826709205673800090501485720867768816");
        BigInteger r = new BigInteger("5452188953055713107393819158892374332916586527736541041226026450382");
        BigInteger tmp1 = d.multiply(r);
        System.out.println(tmp1);

As you can see, there are some most significant digits that are missed in python's result.

Is there any solution for that?

Yashar
  • 2,455
  • 3
  • 25
  • 31
  • 6
    The python answer is actually the [right](http://www.wolframalpha.com/input/?i=372049305848826709205673800090501485720867768816+*+5452188953055713107393819158892374332916586527736541041226026450382) one. – ninesalt May 18 '18 at 20:38
  • @ninesalt can you explain more? – Yashar May 18 '18 at 20:42
  • 5
    Not without seeing the Java code. – Ignacio Vazquez-Abrams May 18 '18 at 20:49
  • @Yashar Try it with another calculator, like `echo '372049305848826709205673800090501485720867768816 * 5452188953055713107393819158892374332916586527736541041226026450382' | bc`. Try multiplying as floats. Try counting the number of expected digits. Try multiplying the first few digits. There are lots of ways to double-check a calculation—and all of them agree with the Python result here. – abarnert May 18 '18 at 21:03
  • The big question is where these other 48 digits that have nothing to do with this operation came from. Probably you're doing something wrong in the Java code you haven't shown us, but maybe your computer is trying to tell you something important. Or maybe [*someone else*](http://tardis.wikia.com/wiki/Shift). – abarnert May 18 '18 at 21:06
  • @IgnacioVazquez-Abrams I just add my java code to the question – Yashar May 18 '18 at 21:12
  • The Java result is obviously too long to be correct. The product of two integers should be about as long as the two factors put together. – user2357112 May 18 '18 at 21:14
  • 2
    That code does not print that output. You need to give us a [mcve] that actually does. One possibility is that you're doing something else that prints out `953068737886329498887415374070086024999409554618`, without a newline, earlier in your program. So this line is printing the right answer, but it's getting concatenated onto all those other characters from some other part of your code. – abarnert May 18 '18 at 21:14

1 Answers1

1

Both Java and Python give the same answer:

2028483115341019294875069650745272851135156323450218238187883716036516369477015140871224045070868977706272670887712

The problem must be in your Java code then. Make sure you preform multiplication correctly and verify your input.

Also, check if there is anything that might print digits just before the result. The simplest way I can think of is:

System.out.println("the result is: " + int1.multiply(int2));

That will separate the output you are interested in from everything that is already printed.

Ernest
  • 2,799
  • 12
  • 28