power : Java Glossary

*0-9ABCDEFGHIJKLMNOPQRSTUVWXYZ (all)

power
Introduction Magic Round Binary Numbers
Fast log₂ Algorithm Links
Powers of Two

Introduction

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 ). (This number was the secret of Bach’s equal tempering that permitted changes of key without “wolves“.)

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 as 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 x2 and cubed x2 you can use x*x or x*x*x. For 2n 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.

Fast log₂ Algorithm

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 With 8-bit addressing, you can span 256 bytes
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 With 16-bit addressing, you can span 64K bytes
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 With 32-bit addressing, you can span 4 Gigabytes
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.

With 64-bit addressing you can span 18 exabytes.

Magic Round Binary Numbers

Icons come in 16, 32, 24, 64, 128 and 256 sizes. These are magic numbers when you express them in binary, much like 10, 100, 100 are in decimal notation. SHA digests come in 32, 64 and 128 byte lengths, again all powers of two. A megabyte is 1,048,576 bytes, a power of two, 220. A gigabyte is 1,073,741,824 bytes, again power of two, 230.


This page is posted
on the web at:

http://mindprod.com/jgloss/power.html

Optional Replicator mirror
of mindprod.com
on local hard disk J:

J:\mindprod\jgloss\power.html
Canadian Mind Products
Please the feedback from other visitors, or your own feedback about the site.
Contact Roedy. Please feel free to link to this page without explicit permission.

IP:[65.110.21.43]
Your face IP:[44.204.164.147]
You are visitor number