Fun with the exp() Function

Deep in the crevasse of oddball math functions is one that raises Euler’s number (e) to a given power. This calculation is vital enough in programming that it sports its own function, exp(). And I would ignore this function with exuberant glee were it not for a trick someone showed me a long time ago.

Euler’s number is approximately 2.71828. I’ve written about it before on this blog, specifically how to calculate it.

The most important thing to remember about Euler’s number is that Euler is pronounced “oil-er.” Beyond that, lots of mathy interesting things happen with this number e, including using it as the base for an exponential expression, en. It’s such a big deal that it has its own name, the exponential function.

Do yourself a favor: If you’re ever at a nerd cocktail party and some mathematician starts off on the exponential function quickly run away! Lots of things go on with this function, but it does have an interesting effect in programming that I’ve played with to much delight.

The following code demonstrates the exp() function by outputting a buncha values:

2025_08_09-Lesson.c

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

int main()
{
    int x;
    double b;

    for( x=0; x<64; x++ )
    {
        b = exp( (double)x/20.0 );
        printf("%f\n",b);
    }

    return 0;
}

Looping variable x is an integer; it’s not a good idea to use real numbers as looping values. In the exp() function, x is cast as double because the exp() function requires a double as its argument. The function returns a double, the value of the expression e raised to the x/20.0 power.

Remember to link in the math library when building this program in Linux. At the command prompt, the switch is -lm (little L, little M), which I add last:

clang 2025_08_09-Lesson.c -lm

Here is the output:

1.000000
1.051271
1.105171
1.161834
1.221403
1.284025
1.349859
1.419068
1.491825
1.568312
1.648721
1.733253
1.822119
1.915541
2.013753
2.117000
2.225541
2.339647
2.459603
2.585710
2.718282
2.857651
3.004166
3.158193
3.320117
3.490343
3.669297
3.857426
4.055200
4.263115
4.481689
4.711470
4.953032
5.206980
5.473947
5.754603
6.049647
6.359820
6.685894
7.028688
7.389056
7.767901
8.166170
8.584858
9.025013
9.487736
9.974182
10.485570
11.023176
11.588347
12.182494
12.807104
13.463738
14.154039
14.879732
15.642632
16.444647
17.287782
18.174145
19.105954
20.085537
21.115344
22.197951
23.336065

Thank you for scrolling down.

In the big picture, values output are teensy tiny at first, but grow larger as the loop progresses. Near the end, values increase at a faster rate. It’s this rate that makes the exp() function’s output interesting to me. As another programmer pointed out years go, you can use these values to simulate a bouncing ball.

The video player below shows an animation from code that uses the exp() function to help bounce an asterisk across the terminal window.

In the video, you see the ball start out slowly, then accelerate as it “drops” to the bottom of the screen. The vertical positions echo the output shown above and at the same rate.

I’ve also animated the asterisk to move left-to-right across the screen, though it stops after 1013 movements. I’ll explain more as I continue to work with the exp() function over the next few lessons.

In next week’s Lesson, I borrow some screen manipulating and timing functions from other Lessons in this blog to help animate the asterisk. Plus I put the loop shown in this lesson’s code to work to make the ball “bounce.”

Leave a Reply