# Write a power function in c

In the beginning, we will see how a simple arithmetic function can be converted into a computer program.

## C program to find power of a number using for loop

Source Update: I've added a new section for larger values of n. Later, we will learn to convert more complex functions. This is not a huge difference, but still around 2. Let's see if the difference is the same for bigger exponent. So, if we do not know any other language, it is better to stick to C. The above function maps the relationship between two sets of natural numbers as given at the right in the graph below. I hope you found this benchmark interesting : For those interested in the code of the benchmark, it's available on Github. The results are the same for clang. Moreover, there is no difference between the two libraries. We could think of a function with a parameter as representing a whole family of functions, with one function for each value of the parameter. If you really care about precise computation, you should not use -ffast-math, but in most case, I think it's fine. Typically a function is deterministic, it can produce only a single output from a unique set of inputs. Conclusion If you are using double precision double , std::pow x, n will be slower than the handcrafted equivalent unless you use -ffast-math, in which case, there is absolutely no overhead. I've checked for higher values of the exponent and the result is also the same. Since I'm mostly interested in single precision performance neural networks are only about single precision , the first benchmarks will be using float.

For example, an exponential function arises in simple models of bacteria growth An exponential function can describe growth or decay. Although we can solve many more problems by using those programming languages than by using high school math, it is often easy to learn programming by solving smaller problems first.

Let's see how is the performance with higher range of n: We can see that the pow function time still remains stable while our loop-based pow function still increases linearly.

I'm a bit disappointed by the lack of single-precision performance for std::pow. The result was two equivalent systems, the Turing machine and lambda calculus.

It also will reduce the accuracy of some operations.

The exponential function.

Rated 6/10
based on 87 review

Download