math: Move mathcommon.h to toplevel include.
[libcstd.git] / src / math / exp.c
blob548318b07ea34ba502d848a196ebc486664dde37
1 /*
2 * exp: exponential function.
3 * Copyright (C) 2010 Nick Bowler.
5 * License LGPLv3+: GNU LGPL version 3 or later. See COPYING.LIB for terms.
6 * See <http://www.gnu.org/licenses/lgpl.html> if you did not receive a copy.
7 * This is free software: you are free to change and redistribute it.
8 * There is NO WARRANTY, to the extent permitted by law.
9 */
11 #include <cstd/mathcommon.h>
13 TYPE MF(exp)(TYPE x)
15 TYPE acc = 1, last = -1, curr = 1;
17 if (x != x)
18 return x;
19 if (x < 0)
20 return 1/MF(exp)(-x);
22 for (TYPE i = 1; acc != last; i++) {
23 last = acc;
24 curr *= x/i;
25 acc += curr;
28 return acc;