i trying write code raises 2 to
the powers of 0, 1, 2. 3. 4, .... n
for example 31 reproduce following results:
1 2 4 8 16 32 64 128 256 512 1024 2048 4096 8192 16384 32768 65536 131072 262144 524288 1048576 2097152 4194304 8388608 16777216 33554432 67108864 134217728 268435456 536870912 1073741824 2147483648 i have written following code:
public class powersof2 { public static void printpowersof2(int n) { if (n >= 0 && n <= 64) { (int = 0 ; <= n ; i++) { system.out.print( math.pow(2, i) + " "); } } else { system.out.println("you have input invalid value"); return; } } public static void main(string[] args) { printpowersof2(31); system.out.println(); } } however, produces following result instead:
1.0 2.0 4.0 8.0 16.0 32.0 64.0 128.0 256.0 512.0 1024.0 2048.0 4096.0 8192.0 16384.0 32768.0 65536.0 131072.0 262144.0 524288.0 1048576.0 2097152.0 4194304.0 8388608.0 1.6777216e7 3.3554432e7 6.7108864e7 1.34217728e8 2.68435456e8 5.36870912e8 1.073741824e9 2.147483648e9 how can fix that?
also have question
when input larger values n such 62 values start same. example 62 gives:
1 2 4 8 16 32 64 128 256 512 1024 2048 4096 8192 16384 32768 65536 131072 262144 524288 1048576 2097152 4194304 8388608 16777216 33554432 67108864 134217728 268435456 536870912 1073741824 2147483647 2147483647 2147483647 2147483647 2147483647 2147483647 2147483647 2147483647 2147483647 2147483647 2147483647 2147483647 2147483647 2147483647 2147483647 2147483647 2147483647 2147483647 2147483647 2147483647 2147483647 2147483647 2147483647 2147483647 2147483647 2147483647 2147483647 2147483647 2147483647 2147483647 2147483647 2147483647 how can fix issue?
because public static double pow(double a, double b) return double. , java double 64 bit iee754 number precision, see decimal points in end.
you can cast int (int)math.pow(2, i)but be aware 2^31 onwards not expected results because int range -2^31 +2^31.
** first solution:**
so, expected result power 63, cast long because java long has range -2^63 +2^63
(int = 0; <= 64; i++) { system.out.println((long)math.pow(2, i)); } you query result many 2147483647 :
called narrowing, when bigger container value casted smaller container value.
below excerpt java language specification - $5.1.3 narrowing primitive conversion, rules governing primitive narrowing.
so, after power of 31, higher bits (32, 33....) discarded, , hence result of 31 bits i.e. 2^31.
in case, per jls, "integral type t" int, bits discarded n lowest order bits (which means 31, because narrowing int)
a narrowing conversion of signed integer integral type t discards n lowest order bits, n number of bits used represent type t. in addition possible loss of information magnitude of numeric value, may cause sign of resulting value differ sign of input value.
a comprehensive solution: (as per discussion going around)
primitive data types has range limitation, there biginteger, bigdecimal provides arbitrarily long values , precision. so, can use biginteger accurate result.
notice that if java's long 2^63 result 9223372036854775807 while per power of 2 wiki result should 9223372036854775808 when use biginteger
bottom line: use biginteger desired range of power of 2.
hope covers concern , give scalable solution.
(int = 0; <= 64; i++) { //system.out.println((long)math.pow(2, i)); system.out.println(biginteger.valueof(2).pow(i)); //gives exact result per wiki information }
Comments
Post a Comment