Author
Java Math issue incorrect output
Mohtashim Shaikh
Ranch Hand
Joined: Jul 18, 2010
Posts: 43
51^43Mod77 in scientific calculator gives 2 as the output however,
(int)(Math.pow(51,43)%(double)77) gives 12 which should be 2 instead.
Can you please help ?
Wouter Oet
Saloon Keeper
Joined: Oct 25, 2008
Posts: 2700
I guess that the integer overflows resulting in wrong output.
"Any fool can write code that a computer can understand. Good programmers write code that humans can understand." --- Martin Fowler
Please correct my English.
Stephan van Hulst
Bartender
Joined: Sep 20, 2010
Posts: 3047
Actually, int isn't the culprit, double is. Double simply isn't big enough to represent 51^43 at integer precision.
Instead, try BigInteger to do the calculation.
Mohtashim Shaikh
Ranch Hand
Joined: Jul 18, 2010
Posts: 43
It works !!!
However, I do not wish to use modPow
How can i (a raiseto b) Mod c without using modPow?
when a,b and c are BigIntegers
Ulf Dittmer
Marshal
Joined: Mar 22, 2005
Posts: 35241
posted Aug 17, 2011 23:39:00
0
However, I do not wish to use modPow
Why not?
Android apps – ImageJ plugins – Java web charts
Campbell Ritchie
Sheriff
Joined: Oct 13, 2005
Posts: 32666
You can write your own method to raise numbers to their powers.
Mohtashim Shaikh
Ranch Hand
Joined: Jul 18, 2010
Posts: 43
Campbell Ritchie wrote: You can write your own method to raise numbers to their powers.
Does that mean there is no way inbuilt methods trick to achieve this ?
Jesper de Jong
Java Cowboy
Bartender
Joined: Aug 16, 2005
Posts: 12921
Yes, there is, but you just said that you don't want to use it...
Besides modPow, class BigInteger also has separate pow and mod methods. Lookup the API documentation of class BigInteger .
Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Rob Spoor
Sheriff
Joined: Oct 27, 2005
Posts: 19216
Oddly enough the pow method takes an int, not a BigInteger like modPow .
SCJP 1.4 - SCJP 6 - SCWCD 5
How To Ask Questions How To Answer Questions
Mohtashim Shaikh
Ranch Hand
Joined: Jul 18, 2010
Posts: 43
Rob Spoor wrote: Oddly enough the
pow method takes an int, not a
BigInteger like
modPow .
Thats the very reason I asked if there isany inbuilt functionality inorder to achieve modPow. If so can someone help me with the code syntax.
Stephan van Hulst
Bartender
Joined: Sep 20, 2010
Posts: 3047
Um yes? It's the modPow() method in BigInteger . Why don't you use it?
Rob Spoor
Sheriff
Joined: Oct 27, 2005
Posts: 19216
Or implement pow yourself. It's not that hard. Inefficient maybe, with all the BigInteger objects that are created, but not hard.
Mohtashim Shaikh
Ranch Hand
Joined: Jul 18, 2010
Posts: 43
Stephan van Hulst wrote: Um yes? It's the modPow() method in
BigInteger . Why don't you use it?
I wish to separate the pow and the mod functionalities . Not sure how could I ?
fred rosenberger
lowercase baba
Bartender
Joined: Oct 02, 2003
Posts: 9947
Mohtashim Shaikh wrote:
Stephan van Hulst wrote: Um yes? It's the modPow() method in
BigInteger . Why don't you use it?
I wish to separate the pow and the mod functionalities . Not sure how could I ?
1) Write your own pow method
2) Write your own mod method.
OR
1) Use BigInteger 's pow method
2) Use BigInteger 's mod method
Never ascribe to malice that which can be adequately explained by stupidity.
subject: Java Math issue incorrect output