aspose file tools*
The moose likes Beginning Java and the fly likes Java Math issue incorrect output Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Java Math issue incorrect output" Watch "Java Math issue incorrect output" New topic
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: 3649
    
  17

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: 42913
    
  68
However, I do not wish to use modPow

Why not?
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 40038
    
  28
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
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14430
    
  23

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 8 API documentation
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19783
    
  20

Oddly enough the pow method takes an int, not a BigInteger like modPow.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
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: 3649
    
  17

Um yes? It's the modPow() method in BigInteger. Why don't you use it?
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19783
    
  20

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: 11497
    
  16

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


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Java Math issue incorrect output