Exponentiation

The C language lacks an exponentiation operator, which would raise a base value to a certain power. For example: 2^8, which in some programming languages is an expression to raise 2 to the 8th power. Instead, C uses the pow() function.

Before powering forward, I’ll dispense with some jargon. Happily refer to Figure 1.

Figure 1. Mathematical stuff, beautifully illustrated.

In the expression 2^8, 2 is the base and 8 is the exponent. Using the format presented in Figure 1, this expression is written as 28. This format is common in written mathematics, but in C, it’s written as pow(2,8).

Being a math noob as I am, I figured that the exponent argument must always be a positive integer value. Yes: I know! Math nerds, contain yourselves! The exponent argument in the pow() function is a double, so it can be any real value, not just positive integers.

So I got thinking. What about fractional values? What about raising the power of 2 to the 1/10th power? To wit:

#include <stdio.h>
#include <math.h>

int main()
{
    int e;
    double exponent;

    exponent = 1.0;
    for( e=0; e<21; e++ )
    {
        printf("2 to the %.4f power is %f\n",
                exponent,
                pow( 2, exponent)
              );
        exponent -= 0.05;
    }

    return(0);
}

(Some compilers may require the math library to be linked in to create the program.)

The for loop decreases the exponent value by .05 each turn, raising the power of 2 to fractional values. Here’s the output:

2 to the 1.0000 power is 2.000000
2 to the 0.9500 power is 1.931873
2 to the 0.9000 power is 1.866066
2 to the 0.8500 power is 1.802501
2 to the 0.8000 power is 1.741101
2 to the 0.7500 power is 1.681793
2 to the 0.7000 power is 1.624505
2 to the 0.6500 power is 1.569168
2 to the 0.6000 power is 1.515717
2 to the 0.5500 power is 1.464086
2 to the 0.5000 power is 1.414214
2 to the 0.4500 power is 1.366040
2 to the 0.4000 power is 1.319508
2 to the 0.3500 power is 1.274561
2 to the 0.3000 power is 1.231144
2 to the 0.2500 power is 1.189207
2 to the 0.2000 power is 1.148698
2 to the 0.1500 power is 1.109569
2 to the 0.1000 power is 1.071773
2 to the 0.0500 power is 1.035265
2 to the -0.0000 power is 1.000000

In the middle, you see 2 raised to the 0.5000 power, which is 21/2 power, or the square root of two, 1.414214. If you’re clever, I suppose you could use the pow() function with the fraction 0.5 and dispense with the companion sqrt() function.

Just as you can stuff a fraction into the exponent argument in the pow() function, you can also dabble with negative numbers. Scary stuff.

#include <stdio.h>
#include <math.h>

int main()
{
    int e;
    double exponent;

    exponent = -1.0;
    for( e=0; e<20; e++ )
    {
        printf("2 to the %.4f power is %f\n",
                exponent,
                pow( 2, exponent)
              );
        exponent -= 0.2;
    }

    return(0);
}

I have no idea what’s going on here, but the output is interesting:

2 to the -1.0000 power is 0.500000
2 to the -1.2000 power is 0.435275
2 to the -1.4000 power is 0.378929
2 to the -1.6000 power is 0.329877
2 to the -1.8000 power is 0.287175
2 to the -2.0000 power is 0.250000
2 to the -2.2000 power is 0.217638
2 to the -2.4000 power is 0.189465
2 to the -2.6000 power is 0.164938
2 to the -2.8000 power is 0.143587
2 to the -3.0000 power is 0.125000
2 to the -3.2000 power is 0.108819
2 to the -3.4000 power is 0.094732
2 to the -3.6000 power is 0.082469
2 to the -3.8000 power is 0.071794
2 to the -4.0000 power is 0.062500
2 to the -4.2000 power is 0.054409
2 to the -4.4000 power is 0.047366
2 to the -4.6000 power is 0.041235
2 to the -4.8000 power is 0.035897

I see that 2-1 is 0.5000, or 1/2. And 2-2 is 0.2500 or 1/4; 2-3 is 0.1250 or 1/8; and 2-4 is 0.0625 or 1/16. Fascinating.

Somewhere on YouTube I’m sure I could watch a video that would explain how the math works, drop the term “logarithm” a few times and have me nodding me head in vague agreement. But then after watching this video, I’d probably watch the Vienna Philharmonic play the Imperial March and forget all about the math. I prefer things that way.

2 thoughts on “Exponentiation

  1. I think the best way to get your head round exponential functions is to plot them.

    “Equations are just the boring part of mathematics. I attempt to see things in terms of geometry.” – Stephen Hawking

    Try going to Wolfram Alpha and pasting
    plot 2^x

    You can then see how 2^0.5 = 1.414, 2^-1 = 0.5 etc.

    Floating point formats like IEEE 754 use negative powers to represent fractions which is why you cannot represent any fraction exactly, some are approximate. You can make any integer from 1, 2, 4 etc (until you run out of bits) but you can’t make any fraction from 1/2, 1/4 etc.

    If this is a real problem you can use some sort of binary coded decimal format like 8421 BCD which effectively stores the number as a string of characters although as only 0-9 are needed you only need 4 bits, hence the name 8421. The downside is it’s slow, I have heard estimates of 20 times slower.

Leave a Reply