Program error in data types
hello,
i have written the below piece of code for calculating the power function, but when i run the program, it shows the wrong result and i verified it by calculating manually. so please help me to get the correct result.
public class powerFunction
{
public static void main(String ar[])
{
int c = 11;
int d = 23;
int n = 187;
long dt;
dt = (long)((Math.pow(c,d)) % n);
System.out.println("dt =" +dt);
}
}
The output should be 88, but it is displaying 149.
Related Article: Javascript : (Data types)
is a Java discussion thread by viteb that has 1 reply and was last updated 8 months ago.
adil_bashir
Junior Poster in Training
67 posts since Jan 2012
Reputation Points: 3
Solved Threads: 1
Skill Endorsements: 0
Isn't this fun!! I tried a "manual" calculation in both double and long variables:
void calcDouble() {
double c=11, cPow2 = c*c, cPow4 = cPow2*cPow2,
cPow8 = cPow4*cPow4,cPow16 = cPow8*cPow8,
cPow23 = cPow16*cPow4*cPow2*c;
System.out.println(cPow23 + " " + cPow23%187);
}
void calcLong() {
long c=11, cPow2 = c*c, cPow4 = cPow2*cPow2,
cPow8 = cPow4*cPow4,cPow16 = cPow8*cPow8,
cPow23 = cPow16*cPow4*cPow2*c;
System.out.println(cPow23 + " " + cPow23%187);
}
Which gave the following results:
8.954302432552372E23 175.0
6839173302027254275 65
neither of which match yours.
Personally I'd put my money on the long manual version.
Any other offers?
JamesCherrill
... trying to help
8,509 posts since Apr 2008
Reputation Points: 2,583
Solved Threads: 1,454
Skill Endorsements: 30
when and where you convert to long also has its influence..
stultuske
Industrious Poster
4,370 posts since Jan 2007
Reputation Points: 1,318
Solved Threads: 610
Skill Endorsements: 24
listen dears..
if we break (11^23) % 187, then it can written as
{(11^1 % 187) * (11^2 % 187) * (11^4 % 187) * (11^8 % 187) * (11^8 % 187)} % 187
= {11 * 121 * 55 * 33 * 33} % 187
= 88
but programatically, itz not 88. so wHat to do.
adil_bashir
Junior Poster in Training
67 posts since Jan 2012
Reputation Points: 3
Solved Threads: 1
Skill Endorsements: 0
No, that's not valid. You must complete the power expression before doing the modulo
JamesCherrill
... trying to help
8,509 posts since Apr 2008
Reputation Points: 2,583
Solved Threads: 1,454
Skill Endorsements: 30
adil_bashir
Junior Poster in Training
67 posts since Jan 2012
Reputation Points: 3
Solved Threads: 1
Skill Endorsements: 0
mathematically its valid
there are forms of math in which
1 + 1 = 1
and
1 + 0 = 0
are valid and correct too, but that's not really what we're looking for, is it?
stultuske
Industrious Poster
4,370 posts since Jan 2007
Reputation Points: 1,318
Solved Threads: 610
Skill Endorsements: 24
adil_bashir
Junior Poster in Training
67 posts since Jan 2012
Reputation Points: 3
Solved Threads: 1
Skill Endorsements: 0
that is the title of my problem, so can u please tell me the exact datatype that can be used.
adil_bashir
Junior Poster in Training
67 posts since Jan 2012
Reputation Points: 3
Solved Threads: 1
Skill Endorsements: 0
can anyone help me to use BigInteger for it?
adil_bashir
Junior Poster in Training
67 posts since Jan 2012
Reputation Points: 3
Solved Threads: 1
Skill Endorsements: 0
Solved by myself..
import java.math.BigInteger;
public class abc3
{
public static void main(String ar[])
{
BigInteger ct = new BigInteger("11");
BigInteger n = new BigInteger("187");
BigInteger d = new BigInteger("23");
BigInteger e = new BigInteger("7");
BigInteger decr = ct.modPow(d,n);
System.out.println("Decr = "+decr);
}
}
and now the output is 88.
thanks all
adil_bashir
Junior Poster in Training
67 posts since Jan 2012
Reputation Points: 3
Solved Threads: 1
Skill Endorsements: 0
Question Answered as of 1 Year Ago by
dantinkakkar,
stultuske
and
JamesCherrill