0
\$\begingroup\$

Your goal is to calculate the exponential of a float (e**x) up to 2 decimal places for any float within 1 to 10. The accepted error range is 0.01.

However, you must not use the built-in exponential function or the logarithm function.

In fact, attempts to circumvent this by (2**x)**(1/ln(2)) are prohibited, even if you hardcode the ln(2).

Basically, the power function is fine, as long as the index is not derived from the input.

Shortest solution in bytes wins.

(You may include an extra solution which uses the built-in, but which does not affect the score in any way.)

Specs

  • Program or function
  • Any reasonable input/output format

Testcases

1 -> 2.71 or 2.72 or 2.73
\$\endgroup\$
  • \$\begingroup\$ you must not use the built-in exponential function would seem to collide with the power function is fine \$\endgroup\$ – cat Apr 30 '16 at 11:57
  • \$\begingroup\$ x** 3 is allowed while 3**x is not. \$\endgroup\$ – Leaky Nun Apr 30 '16 at 12:00
  • \$\begingroup\$ What about 3 ^ 3, or x ^ y>? (I'm not suggesting hardcoding the output, I just don't get the difference) \$\endgroup\$ – cat Apr 30 '16 at 12:02
  • \$\begingroup\$ The index cannot be any function of the input. \$\endgroup\$ – Leaky Nun Apr 30 '16 at 12:03
  • 5
    \$\begingroup\$ We've calculated e^x and ln(x) before. codegolf.stackexchange.com/questions/9080/calculate-ex-and-lnx/… \$\endgroup\$ – miles Apr 30 '16 at 12:11
3
\$\begingroup\$

Matlab, 17 bytes

@(x)(1+x/1e9)^1e9
\$\endgroup\$
  • \$\begingroup\$ I'm deeply confused as to why this works. \$\endgroup\$ – Addison Crump Apr 30 '16 at 12:33
  • \$\begingroup\$ exp(x) = limit (1+x/n)^n as n->infinity. As long as you only have a fixed range you just need to choose n big enough. \$\endgroup\$ – flawr Apr 30 '16 at 12:34
  • \$\begingroup\$ OH. Welp. My Taylor series workings out are now useless. xD \$\endgroup\$ – Addison Crump Apr 30 '16 at 12:35
  • \$\begingroup\$ The talyor series are also quite well known exp(x) = sum x^n / n! for n=0 upto inftinity, converges everywhere. \$\endgroup\$ – flawr Apr 30 '16 at 12:35
  • \$\begingroup\$ Well, yeah - I meant porting it to AS. \$\endgroup\$ – Addison Crump Apr 30 '16 at 12:36
0
\$\begingroup\$

Haskell, 19 bytes

(**1e9).(1+).(/1e9)

or alternatively a named function:

f x=(1+x/1e9)**1e9
\$\endgroup\$
0
\$\begingroup\$

AppleScript, 62 bytes

Okay, limit approximation of ex is better. ;P Thanks, flawr.

(1+(display dialog""default answer"")'s text returned/1e9)^1e9
\$\endgroup\$
0
\$\begingroup\$

MATL, 9 bytes

1e9/Q1e9^

Based upon @flawr's answers.

Try it Online

Explanation

        % Implicitly grab input as a number
1e9     % Number literal
/       % Divide the input by 1e9
Q       % Add one
1e9     % Number literal
^       % Exponent
        % Implicitly display result
\$\endgroup\$
0
\$\begingroup\$

AppleScript, 204 bytes

set x to(display dialog""default answer"")'s text returned
set r to 1
set i to 1
repeat 29
set r to r+(x^i)/f(i)
set i to i+1
end
return r
on f(a)
set x to a
repeat x-1
set a to a-1
set x to a*x
end
x
end

Yeah, it's kinda long. This uses the 30th term Taylor polynomial representation for e^x.

How this works:

The Taylor series expansion for ex is:

taylor

Now, according to the Lagrange error bound, I know that any given Taylor polynomial can get a precision to the first unused term of the Taylor series.

In more mathematical terms, I know that:

power

where I will have a precision to ex of ak+1.

Since the OP specified that I must have a precision of 2 decimal places or better, ak+1 must be less than or equal to 1/100. I need to work out the point at which the maximum error of the function possible, located at 10 in this case is less than said error. After a little trial and error, I found that:

precision

Which means that:

final

For as far as our precision is concerned. The code reflects this in that I will calculate:

calculated sum

\$\endgroup\$

Not the answer you're looking for? Browse other questions tagged or ask your own question.