{"id":7097,"date":"2025-08-09T00:01:10","date_gmt":"2025-08-09T07:01:10","guid":{"rendered":"https:\/\/c-for-dummies.com\/blog\/?p=7097"},"modified":"2025-08-16T08:12:28","modified_gmt":"2025-08-16T15:12:28","slug":"fun-with-the-exp-function","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=7097","title":{"rendered":"Fun with the <em>exp()<\/em> Function"},"content":{"rendered":"<p>Deep in the crevasse of oddball math functions is one that raises Euler&#8217;s number (<em>e<\/em>) to a given power. This calculation is vital enough in programming that it sports its own function, <em>exp()<\/em>. And I would ignore this function with exuberant glee were it not for a trick someone showed me a long time ago.<br \/>\n<!--more--><br \/>\nEuler&#8217;s number is approximately 2.71828. I&#8217;ve written about it <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=2680\">before<\/a> on this blog, specifically how to calculate it.<\/p>\n<p>The most important thing to remember about Euler&#8217;s number is that <em>Euler<\/em> is pronounced &#8220;oil-er.&#8221; Beyond that, lots of mathy interesting things happen with this number <em>e<\/em>, including using it as the base for an exponential expression, <em>e<sup>n<\/sup><\/em>. It&#8217;s such a big deal that it has its own name, the <a href=\"https:\/\/en.wikipedia.org\/wiki\/Exponential_function\" target=\"_blank\">exponential function<\/a>.<\/p>\n<p>Do yourself a favor: If you&#8217;re ever at a nerd cocktail party and some mathematician starts off on the exponential function <em>quickly run away<\/em>! Lots of things go on with this function, but it does have an interesting effect in programming that I&#8217;ve played with to much delight.<\/p>\n<p>The following code demonstrates the <em>exp()<\/em> function by outputting a buncha values:<\/p>\n<h3><a href=\"https:\/\/github.com\/dangookin\/C-For-Dummies-Blog\/blob\/master\/2025_08_09-Lesson.c\" rel=\"noopener\" target=\"_blank\">2025_08_09-Lesson.c<\/a><\/h3>\n<pre class=\"screen\">\r\n#include &lt;stdio.h&gt;\r\n#include &lt;math.h&gt;\r\n\r\nint main()\r\n{\r\n    int x;\r\n    double b;\r\n\r\n    for( x=0; x&lt;64; x++ )\r\n    {\r\n        b = exp( (double)x\/20.0 );\r\n        printf(\"%f\\n\",b);\r\n    }\r\n\r\n    return 0;\r\n}<\/pre>\n<p>Looping variable <code>x<\/code> is an integer; it&#8217;s not a good idea to use real numbers as looping values. In the <em>exp()<\/em> function, <code>x<\/code> is cast as <em>double<\/em> because the <em>exp()<\/em> function requires a <em>double<\/em> as its argument. The function returns a <em>double<\/em>, the value of the expression <em>e<\/em> raised to the <code>x\/20.0<\/code> power.<\/p>\n<p>Remember to link in the math library when building this program in Linux. At the command prompt, the switch is <code>-lm<\/code> (little L, little M), which I add last:<\/p>\n<p><code>clang 2025_08_09-Lesson.c -lm<\/code><\/p>\n<p>Here is the output:<\/p>\n<p><code>1.000000<br \/>\n1.051271<br \/>\n1.105171<br \/>\n1.161834<br \/>\n1.221403<br \/>\n1.284025<br \/>\n1.349859<br \/>\n1.419068<br \/>\n1.491825<br \/>\n1.568312<br \/>\n1.648721<br \/>\n1.733253<br \/>\n1.822119<br \/>\n1.915541<br \/>\n2.013753<br \/>\n2.117000<br \/>\n2.225541<br \/>\n2.339647<br \/>\n2.459603<br \/>\n2.585710<br \/>\n2.718282<br \/>\n2.857651<br \/>\n3.004166<br \/>\n3.158193<br \/>\n3.320117<br \/>\n3.490343<br \/>\n3.669297<br \/>\n3.857426<br \/>\n4.055200<br \/>\n4.263115<br \/>\n4.481689<br \/>\n4.711470<br \/>\n4.953032<br \/>\n5.206980<br \/>\n5.473947<br \/>\n5.754603<br \/>\n6.049647<br \/>\n6.359820<br \/>\n6.685894<br \/>\n7.028688<br \/>\n7.389056<br \/>\n7.767901<br \/>\n8.166170<br \/>\n8.584858<br \/>\n9.025013<br \/>\n9.487736<br \/>\n9.974182<br \/>\n10.485570<br \/>\n11.023176<br \/>\n11.588347<br \/>\n12.182494<br \/>\n12.807104<br \/>\n13.463738<br \/>\n14.154039<br \/>\n14.879732<br \/>\n15.642632<br \/>\n16.444647<br \/>\n17.287782<br \/>\n18.174145<br \/>\n19.105954<br \/>\n20.085537<br \/>\n21.115344<br \/>\n22.197951<br \/>\n23.336065<\/code><\/p>\n<p>Thank you for scrolling down.<\/p>\n<p>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&#8217;s this rate that makes the <em>exp()<\/em> function&#8217;s output interesting to me. As another programmer pointed out years go, you can use these values to simulate a bouncing ball.<\/p>\n<p>The video player below shows an animation from code that uses the <em>exp()<\/em> function to help bounce an asterisk across the terminal window.<\/p>\n<div style=\"width: 584px;\" class=\"wp-video\"><video class=\"wp-video-shortcode\" id=\"video-7097-1\" width=\"584\" height=\"389\" preload=\"metadata\" controls=\"controls\"><source type=\"video\/mp4\" src=\"https:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2025\/08\/0809-video_1.mp4?_=1\" \/><a href=\"https:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2025\/08\/0809-video_1.mp4\">https:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2025\/08\/0809-video_1.mp4<\/a><\/video><\/div>\n<p>In the video, you see the ball start out slowly, then accelerate as it &#8220;drops&#8221; to the bottom of the screen. The vertical positions echo the output shown above and at the same rate.<\/p>\n<p>I&#8217;ve also animated the asterisk to move left-to-right across the screen, though it stops after 1013 movements. I&#8217;ll explain more as I continue to work with the <em>exp()<\/em> function over the next few lessons.<\/p>\n<p>In <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=7111\">next week&#8217;s Lesson<\/a>, 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&#8217;s code to work to make the ball &#8220;bounce.&#8221;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Of what possible use could there be fore a function <em>e<sup>n<\/sup><\/em>? <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=7097\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2],"tags":[],"class_list":["post-7097","post","type-post","status-publish","format-standard","hentry","category-main"],"_links":{"self":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/7097","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=7097"}],"version-history":[{"count":10,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/7097\/revisions"}],"predecessor-version":[{"id":7126,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/7097\/revisions\/7126"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=7097"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=7097"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=7097"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}