0

I am dividing 2 BigInteger values

 N = 94400567826854724487909837398348327858277687772498043028143080274141357168982328449151039212940544180753206250882172215623025263699414679367833398858755440423828380162368132951103946930488990027827989933432181959968836035369641054955336867327681756681532771354933952141599581827698618739724580124555641633861 e = 44598251142417535121365176389097472886004547620498587196637028250785621416609954924107228171779826812564450558386070863338405090708203920066925426522584172770629487602232360693706521107911895310319989921169318202224097739016252544913819802671755649536088117809831381940146419602124919706024152810967126830967 

I am using this code to do so

System.out.println(e.divide(N)); 

and it prints 0. Is there something wrong with the divide method?

1
  • 4
    What did you expect as the result? This is equivalent to 4/9 which in integer arithmetic is also 0 Commented Nov 7, 2012 at 13:05

3 Answers 3

9

Your e value is smaller than your N value, and you're performing integer arithmetic, therefore you'll get a value of 0.

If you want non-integer division, use BigDecimal instead.

Sign up to request clarification or add additional context in comments.

Comments

2

You are dividing a number x for a number that is about 2x. That means that result will be between 0 and 1. Since it is a Big Integer, the result will be a Big Integer, which will be 0.

If you want decimal values, use BigDecimal instead.

Comments

0

You are probably dividing them in wrong order, this works for me:

new BigInteger("94400567826854724487909837398348327858277687772498043028143080274141357168982328449151039212940544180753206250882172215623025263699414679367833398858755440423828380162368132951103946930488990027827989933432181959968836035369641054955336867327681756681532771354933952141599581827698618739724580124555641633861") .divide(new BigInteger("44598251142417535121365176389097472886004547620498587196637028250785621416609954924107228171779826812564450558386070863338405090708203920066925426522584172770629487602232360693706521107911895310319989921169318202224097739016252544913819802671755649536088117809831381940146419602124919706024152810967126830967")) 

and yields... 2 (rounded 9/4 since BigInteger is used for integer values only).

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.