power : Java Glossary

go to home page P words local find full screen, hide local find menu Google search web for more information on this topic jump to foot of page translate this page with Babelfish by Roedy Green ©1996-2009 Canadian Mind Products
index page for letter ⇒ punctuation 0-9 A B C D E F G H I J K L M N O P Q R S T U V W X Y Z (all)
power
To raise a number to a power, you can use Math. pow( double x, double p).

This also works for non integer p and negative p. So you can compute roots with it too, e.g. the twelfth root of 2 is Math. pow( 2, 1.0d/12.0d ).

Math. pow is a very expensive operation. It has to calculate the natural logarithm of x using polynomial interpolation (lots of multiplies), then it multiplies by p, then it calculates e to that power, again using polynomial interpolation. It also has to ensure if both operands are precise integers represented an doubles and the result is an integer that can be precisely represented as a double, then the result must be bang on. Avoid Math.pow if you possibly can. If you look at the source code for Math.pow you may think I am all wet. Much of this complexity is hidden inside the floating point hardware. Like all floating point routines, Math.pow’s results are approximate.

If you want perfection, use long, BigInteger or BigDecimal. For squared and cubed you can use x*x or x*x*x. For 2**n you can use 1<<n. For other integral powers you can use Patricia Shanahan’s method, which it turns out is almost identical to the method Knuth gives on page 462 of The Art of Computer Programming Volume 2 Seminumerical Algorithms. The method dates back to 200 BC in India.

book cover recommend book⇒The Art of Computer Programming Volume 2 Seminumerical Algorithms, Third Edition.
 hardcover
ISBN13:978-0-201-48541-7impressioncounter
ISBN10:0-201-48541-9impressioncounter
publisher:Addison-Wesley
published:1998-10-15
by:Donald Knuth
UK flag abe books.co.uk abe books.ca Canadian flag
UK flag amazon.co.uk. amazon.ca. Canadian flag
German flag abe books.de chapters.indigo.ca. Canadian flag
German flag amazon.de. abe books.com American flag
French flag abe books.fr amazon.com. American flag
French flag amazon.fr. barnes and noble.com American flag
Italian flag abe books.it powells.com American flag
Spanish flag iberlibro.com abe books anz Australian flag
Here is a fast integer log2 approximation:

Powers of Two

Powers of Two
n 2n decimal 2n hex notes
0 1 0000_0000_0000_0001
1 2 0000_0000_0000_0002
2 4 0000_0000_0000_0004
3 8 0000_0000_0000_0008
4 16 0000_0000_0000_0010
5 32 0000_0000_0000_0020
6 64 0000_0000_0000_0040
7 128 0000_0000_0000_0080
8 256 0000_0000_0000_0100
9 512 0000_0000_0000_0200
10 1,024 0000_0000_0000_0400
11 2,048 0000_0000_0000_0800
12 4,096 0000_0000_0000_1000
13 8,192 0000_0000_0000_2000
14 16,384 0000_0000_0000_4000
15 32,768 0000_0000_0000_8000
16 65,536 0000_0000_0001_0000
17 131,072 0000_0000_0002_0000
18 262,144 0000_0000_0004_0000
19 524,288 0000_0000_0008_0000
20 1,048,576 0000_0000_0010_0000
21 2,097,152 0000_0000_0020_0000
22 4,194,304 0000_0000_0040_0000
23 8,388,608 0000_0000_0080_0000
24 16,777,216 0000_0000_0100_0000
25 33,554,432 0000_0000_0200_0000
26 67,108,864 0000_0000_0400_0000
27 134,217,728 0000_0000_0800_0000
28 268,435,456 0000_0000_1000_0000
29 536,870,912 0000_0000_2000_0000
30 1,073,741,824 0000_0000_4000_0000
31 2,147,483,648 0000_0000_8000_0000
32 4,294,967,296 0000_0001_0000_0000
33 8,589,934,592 0000_0002_0000_0000
34 17,179,869,184 0000_0004_0000_0000
35 34,359,738,368 0000_0008_0000_0000
36 68,719,476,736 0000_0010_0000_0000
37 137,438,953,472 0000_0020_0000_0000
38 274,877,906,944 0000_0040_0000_0000
39 549,755,813,888 0000_0080_0000_0000
40 1,099,511,627,776 0000_0100_0000_0000
41 2,199,023,255,552 0000_0200_0000_0000
42 4,398,046,511,104 0000_0400_0000_0000
43 8,796,093,022,208 0000_0800_0000_0000
44 17,592,186,044,416 0000_1000_0000_0000
45 35,184,372,088,832 0000_2000_0000_0000
46 70,368,744,177,664 0000_4000_0000_0000
47 140,737,488,355,328 0000_8000_0000_0000
48 281,474,976,710,656 0001_0000_0000_0000
49 562,949,953,421,312 0002_0000_0000_0000
50 1,125,899,906,842,624 0004_0000_0000_0000
51 2,251,799,813,685,248 0008_0000_0000_0000
52 4,503,599,627,370,496 0010_0000_0000_0000
53 9,007,199,254,740,992 0020_0000_0000_0000
54 18,014,398,509,481,984 0040_0000_0000_0000
55 36,028,797,018,963,968 0080_0000_0000_0000
56 72,057,594,037,927,936 0100_0000_0000_0000
57 144,115,188,075,855,872 0200_0000_0000_0000
58 288,230,376,151,711,744 0400_0000_0000_0000
59 576,460,752,303,423,488 0800_0000_0000_0000
60 1,152,921,504,606,846,976 1000_0000_0000_0000
61 2,305,843,009,213,693,952 2000_0000_0000_0000
62 4,611,686,018,427,387,904 4000_0000_0000_0000
63 9,223,372,036,854,775,808
-9,223,372,036,854,775,808
8000_0000_0000_0000 In Java, this value will appear as negative since there are no unsigned 64-bit numbers.
64 18,446,744,073,709,551,616 1_0000_0000_0000_0000 Requires 65 bits. Cannot be expressed in Java.

CMP homejump to top
CMP logo
feedback Please email your feedback for publication, errors, omissions, broken/redirected link reports
and suggestions to improve this page to Roedy Green : feedback email
made with CSS
HTML Checked!
ICRA ratings logo
mindprod.com IP:[65.110.21.43]
Your face IP:[38.103.63.58]
You are visitor number 28,397.
You can get a fresh copy of this page from: or possibly from your local J: drive (Java virtual drive/mindprod.com website mirror)
http://mindprod.com/jgloss/power.html J:\mindprod\jgloss\power.html