[Question]
  [
My boss just told me to write a cosine function. Being a good math geek, my mind immediately conjured the appropriate Taylor Series.
$$\cos(x) = \frac 1 {0!} - \frac {x^2} {2!} + \frac {x^4} {4!} - \frac {x^6} {6!} + \cdots + \frac{(-1)^k \times x^{2k}} {(2k)!} + \cdots$$
However, my boss is very picky. He would like to be able to specify exactly how many terms of the Taylor Series to compute. Can you help me write this function?
### Your Task
Given a floating point value \$x\$ from \$0\$ to \$2\pi\$ and a positive integer \$n\$ less than \$100\$, compute the sum of the first \$n\$ terms of the Taylor series given above for \$\cos(x)\$.
This is [code-golf](/questions/tagged/code-golf "show questions tagged 'code-golf'"), so shortest code wins. Input and output can be taken in any of the standard ways. Standard loopholes are forbidden.
### Notes
* Input can be taken in any reasonable form, as long as there is a clear separation between \$x\$ and \$n\$.
* Input and output should be floating-point values, at least as accurate as calculating the formula using single-precision IEEE floating point numbers with some standard rounding rule.
* If it makes sense for the language being used, computations may be done using exact rational quantities, but the input and output shall still be in decimal form.
### Examples
```
 x  |  n | Output
----+----+--------------
0.0 |  1 | 1.0
0.5 |  1 | 1.0
0.5 |  2 | 0.875
0.5 |  4 | 0.87758246...
0.5 |  9 | 0.87758256...
2.0 |  2 | -1.0
2.0 |  5 | -0.4158730...
```
  
[Answer]
# [Operation Flashpoint](https://en.wikipedia.org/wiki/Operation_Flashpoint:_Cold_War_Crisis) scripting language, ~~165~~ 157 bytes
```
F={x=_this select 0;n=_this select 1;i=0;r=0;while{i<n*2}do{r=r+x^i/(i call{c=_this;j=c-1;while{j>0}do{c=c*j;j=j-1};if(c<1)then{c=1};c})*(-1)^(i/2);i=i+2};r}
```
**Call with:**
```
hint format["%1\n%2\n%3\n%4\n%5\n%6\n%7",
    [0.0, 1] call f,
    [0.5, 1] call f,
    [0.5, 2] call f,
    [0.5, 4] call f,
    [0.5, 9] call f,
    [2.0, 2] call f,
    [2.0, 5] call f]
```
**Output:**
[![enter image description here](https://i.stack.imgur.com/wMyqs.jpg)](https://i.stack.imgur.com/wMyqs.jpg)
> 
> Input and output should be floating-point values, at least as accurate as calculating the formula using single-precision IEEE floating point numbers with some standard rounding rule.
> 
> 
> 
I'm fairly sure that the numbers are single-precision IEEE floating point numbers, even though in the printed output the longer decimals are not that precise. It is the printing that rounds the numbers like that, actually the numbers are more precise.
For instance, `a=1.00001;b=1.000011;hint format["%1\n%2\n%3", a, b, a==b]` will output this:
```
1.00001
1.00001
false
```
So clearly the actual precision of the numbers is greater than the printed precision.
[Answer]
# [05AB1E](https://github.com/Adriandmen/05AB1E), ~~14~~ 11 bytes
```
FIn(NmN¬∑!/O
```
[Try it online!](https://tio.run/##MzBNTDJM/f/fzTNPwy/X79B2RX3///8tuQz0TAE "05AB1E ‚Äì TIO Nexus")
**Explanation**
```
F                # for N in [0 ... n] do
 In              # push (x^2)
   (             # negate
    Nm           # raise to the Nth power
      N¬∑!        # push (2*N)!
         /       # divide
          O      # sum
```
[Answer]
# [MATL](https://github.com/lmendo/MATL), 14 bytes
```
U_iqE:2ep/YpsQ
```
[Try it online!](https://tio.run/nexus/matl#@x8an1noamWUWqAfWVAc@P@/kZ4BlykA "MATL ‚Äì TIO Nexus") Or [verify all test cases](https://tio.run/nexus/matl#S/gfGp9Z6GpllFqgH1lQHPjfJeS/AZchl4GeKZQ0ApMmYNISyANBUwA).
### Explanation with example
All numbers have double precision (this is the default).
Consider inputs `x = 2.0`, `n = 5`.
```
U_     % Implicitly input x. Square and negate
       % STACK: -4
iqE    % Input n. Subtract 1, multiply by 2
       % STACK: -4, 8
:      % Range
       % STACK: -4, [1 2 3 4 5 6 7 8]
2e     % Reshape into a 2-row matrix
       % STACK: -4, [1 3 5 7;
       %             2 4 6 8]
p      % Product of each column
       % STACK: -4, [2 12 30 56]
/      % Divide, element-wise
       % STACK: [-2 -0.333333333333333 -0.133333333333333 -0.0714285714285714]
Yp     % Cumulative product of array
       % STACK: [-2 0.666666666666667 -0.0888888888888889 0.00634920634920635]
s      % Sum of array
       % STACK: -1.41587301587302
Q      % Add 1. Implicitly display
       % STACK: -0.41587301587302
```
[Answer]
# Mathematica, ~~49~~ ~~41~~ ~~39~~ 31 bytes
```
Sum[(-#^2)^k/(2k)!,{k,0,#2-1}]&
```
Old, more "fun" version: (39 bytes)
```
Normal@Series[Cos@k,{k,0,2#2-2}]/.k->#&
```
*Saved 10 bytes thanks to @Pavel and 8 thanks to @Greg Martin!*
[Answer]
# [Jelly](https://github.com/DennisMitchell/jelly), ~~12~~ 11 [bytes](https://github.com/DennisMitchell/jelly/wiki/Code-page)
```
·∏∂·∏§¬µ‚Åπ*√∑!_2/S
```
**[Try it online!](https://tio.run/nexus/jelly#ARwA4///4bi24bikwrXigbkqw7chXzIvU////zX/Mi4w)**
### How?
```
·∏∂·∏§¬µ‚Åπ*√∑!_2/S - Main link: n, x           e.g. 5, 2.0
·∏∂           - lowered range(n)              [0,1,2,3,4]
 ·∏§          - double (vectorises)           [0,2,4,6,8]
  ¬µ         - monadic chain separation (call that i)
   ‚Åπ        - link's right argument         2.0
    *       - exponentiate(i) (vectorises)  [1.0,4.0,16.0,64.0,256.0]
      !     - factorial(i) (vectorises)     [1,  2,  24,  720, 40320]
     √∑      - divide (vectorises)           [1.0,2.0,0.6666666666666666,0.08888888888888889,0.006349206349206349]
        2/  - pairwise reduce by:
       _    -     subtraction               [-1.0,0.5777777777777777,0.006349206349206349]
         S  - sum                           -0.41587301587301617
```
[Answer]
## **Jelly, 22 bytes**
```
-*√∞√ó√∏‚Åπ*‚Å∏¬≤√∞√∑√∏‚Å∏·∏§!
‚Å∏R‚Äô√á‚Ç¨S
```
This is a full program which takes n as the first argument and x as the second.
Explanation:
```
              Creates a function to compute each term in the series. 
Its argument we will call k, eg k=3 computes 3rd term. Take x=2 for example.
-*           Computes (-1)^k. Eg -1
√∞√ó√∏        Multiplies by the quantity of
‚Åπ             x.  
*             to the power of
‚Å∏             k
¬≤             ...squared. Eg -1 √ó (2¬≥)¬≤ 
√∞√∑√∏        divides by the quantity of
‚Å∏              k
·∏§             doubled
!               ...factorial. Eg -1 √ó (2¬≥)¬≤/(6!).
                Main link, first argument n and second argument n. Eg n=4, x=2.
‚Å∏R            Creates range(n). Eg [1,2,3,4]
‚Äô                Decrements each element. Eg [0,1,2,3]
√á‚Ç¨            Maps the above function over each element. Eg [1,-2,0.666,-0.0889]
S               Sum all all of the elements.  Eg -0.422.
```
[Answer]
## Python, 54 bytes
```
f=lambda x,n,t=1,p=1:n and t+f(x,n-1,-t*x*x/p/-~p,p+2)
```
If using Python 2, be sure to pass x as a float, not an integer, but I my understanding is that it doesn't matter if you're using Python 3.
[Answer]
# TI-Basic, ~~41~~ 40 bytes
```
Prompt X,N
sum(seq((-(X+1E-49)2)^Q/((2Q)!),Q,0,N-1
```
`1E-49` is added to the angle because TI-Basic throws an error for 0^0, it's just large enough to not cause the error, and it is not large enough to change the answer.
[Answer]
# [R](https://www.r-project.org/), ~~70~~ ~~64~~ 47 bytes
```
function(x,n,m=1:n-1)sum((-x^2)^m/gamma(2*m+1))
```
[Try it online!](https://tio.run/##bcs7CoAwDADQ3ZMk2ioNdlDoVZTiD4dE8QPevs7arg/eEfZp9HL1w3auMrkw3zJc6ybwKFHsTCva4HkzgH46wo6rxTN7oJwLg/jtUFplMIuMElYnrPkbxZWUxSy8 "R ‚Äì Try It Online")
* uses `gamma(n+1)` instead of `factorial(n)`
* `1:n-1` is equivalent to `0:(n-1)`
Thanks to Dominic van Essen for golfing 17 bytes off by using a basic R feature: vectorization!
[Answer]
# C, 96 bytes
**Recursive** [Live](http://ideone.com/agZrkz)
```
f(n){return n?n*f(n-1):1;}float c(n,x)float x;{return n?c(n-1,x)+pow(-1,n)*pow(x,2*n)/f(2*n):1;}
```
**Detailed**
```
f(n) // factorial(n)
{
    return n ?   // n != 0 ?
        n*f(n-1) // n! = n * (n-1)!
    : 1;         // 0! = 1
}
float c(n,x)float x; // cos(x) with n+1 terms
{
    return n ?        // n != 0 ?
        c(n-1, x)     // cos(x) (n-1)th term
        + pow(-1, n)  // + (-1)^n
        * pow(x, 2*n) // * x^(2n)
        / f(2 * n)    // / (2n)!
    : 1;              // cos(x) at n=0
}
```
**Progressive Recursive, 133 bytes** [Live](http://ideone.com/lxXauH)
```
#define F float
#define c(x,n) 1+g(1,n,x,1,1,1)
F g(F i,F n,F x,F s,F p,F f){s=-s;p*=x*x;f*=i;return i<n?g(i+1,n,x,s,p,f)+s/2*p/f:0;}
```
**Detailed**
```
#define F float // shorthand float
#define c(x,n) 1+g(1,n,x,1,1,1) // macro function
F g(F i,F n,F x,F s,F p,F f)
{
    s = -s;   // (-1)^n = (-1) * (-1)^(n-1)
    p *= x*x; // x^(2n) =  x^2 * x^(2(n-1))
    f *= i;   //    2n! =    2 * (1*2*..*n)
    return i < n ?       // i = 0 .. n-1
        g(i+1,n,x,s,p,f) // next term
        + s / 2 * p / f  // s*p/2f = s/2*p/f
        : 0;             // don't compute nth term
}
```
[Answer]
## JavaScript (ES6), 46 bytes
```
f=
x=>g=(n,t=1,p=0)=>n&&t+g(--n,-t*x*x/++p/++p,p)
```
```
<div oninput=o.textContent=f(x.value)(n.value)><input id=x><input type=number min=1 value=1 id=n><pre id=o>1
```
Takes curried inputs (x)(n).
[Answer]
## C, 71 bytes
using Horner scheme
```
float f(n,x)float x;{float y;for(n+=n;n;)y=1-(y*x*x/n--)/n--;return y;}
```
Ungolfed version:
```
float f(n,x) float x;
{
  float y = 0.0;
  for(n = 2*n; n>0; n -= 2)
  {
    y = 1-y*x*x/n/(n-1);
  }
  return y;
}
```
[Answer]
# [oK](http://johnearnest.github.io/ok), 38 bytes
This also works in **k**, but takes **39 bytes** because one `'` has to be written as `/:` instead (at least, in kmac 2016.06.28 it does).
```
{+/(y#1 -1)*{(*/y#x)%*/1+!y}.'x,'2*!y}
```
Explanation:
Let's start with the middle bit. `(*/y#x)` is exponentiation, it is equivalent to `x^y`. `*/1+!y` would be `y!`, or `y` factorial. `%` is division. Therefore the function in the middle is `middle(x,y) = (x^y)/(y!)`.
Now the bit on the right, to which the function above gets applied. `2*!y` is `{0, 2, 4, ..., 2*(y-1)}`. `x,'` prepends `x` to every item in that list, turning it into `{(x, 0), (x, 2), (x, 4), ..., (x, 2*(y-1))}`. The `.'` then applies `middle` to every pair of numbers (`map`, essentially).
Finally, `(y#1 -1)*` multiplies the result by 1 or -1 (alternating), and `+/` takes the sum.
[Answer]
# Haskell, 71 Bytes
```
f x n=sum$map(\i->(-1)^i*x^(2*i)/fromIntegral(product[1..2*i]))[0..n-1]
```
This is a pretty boring answer that's not too hard to decipher. The `fromIntegral` really bites, though. (The `/` operator requires operands of the same numeric type in Haskell, and coercing between numeric types is not allowed without a wordy function.)
[Answer]
# [Jelly](https://github.com/DennisMitchell/jelly), 12 bytes
```
¬≤N*·∏∂}¬©√∑¬Æ·∏§!¬§S
```
[Try it online!](https://tio.run/nexus/jelly#@39ok5/Wwx3bag@tPLz90LqHO5YoHloS/P/onsPL9R81rfGG4sj//6MN9Ax0FAxjdRSALFNklhGcZQJnWYJYRiAdRnCWaSwA "Jelly ‚Äì TIO Nexus")
### How it works
```
¬≤N*·∏∂}¬©√∑¬Æ·∏§!¬§S  Main link. Left argument: x. Right argument: n
¬≤             Square; yield x¬≤.
 N            Negate; yield -x¬≤.
     ¬©         Call the link to the left and copy the result to the register.
   ·∏∂}          Call unlength on the right argument, yielding [0, 1, ..., n-1].
  *           Yield [1, -x¬≤, ..., (-x¬≤)**(n-1)].
          ¬§   Combine the three links to the left into a niladic chain.
       ¬Æ        Yield the value in the register, [0, 1, ..., n-1].
        ·∏§       Unhalve; yield [0, 2, ..., 2n-2].
         !      Factorial; yield [0!, 2!, ..., (2n-2)!].
      √∑         Division; yield [1/0!, -x¬≤/2!, ..., (-x¬≤)**(n-1)/(2n-2)!].
           S  Take the sum.
```
[Answer]
# Pyth, 16 bytes
```
sm_Fc^vzyd.!yddU
```
Accepts `n` first, then `x`. [Example run.](http://pyth.herokuapp.com/?code=sm_Fc%5Evzyd.%21yddU&input=2%0A0.5&debug=0)
[Answer]
# [Haskell](https://www.haskell.org/), 61 bytes
```
x#n=sum[(-1*x^2)^i/fromIntegral(product[1..2*i])|i<-[0..n-1]]
```
This seemed different enough from the other Haskell solution to warrant a separate answer. Implementation should be pretty self-explanatory‚Äîcall with `x#n` where `x` is the number the cosine of which is to be computed and `n` is the order of the partial sum to be taken.
[Answer]
# [Pyt](https://github.com/mudkip201/pyt), ~~37~~ ~~34~~ 33 bytes
```
‚Üê‚Üêƒêƒê‚Üî3»ò1~‚áπ≈ô‚Åª^04»ò‚áπ≈ô‚Åª^¬≤*0‚Üî≈ô‚Åª2*!+/+∆©
```
[Answer]
# J, ~~26~~ 24 Bytes
```
+/@:(!@]%~^*_1^2%~])2*i.
```
-2 bytes thanks to @cole
I originally planned to use a cyclic gerund to alternate between adding and subtracting, but couldn't get it to work.
### Explanation:
```
                    2*i.     | Integers from 0 to 2(n-1)
    (              )         | Dyadic train:
            _1^-:@]          | -1 to the power of the left argument
          ^*                 | Times left arg to the power of right arg
     !@]%~                   | Divided by the factorial of the right arg
+/@:                         | Sum
```
[Answer]
# [Perl 6](http://perl6.org/), 53 bytes
```
{(sum (1,*i*$^x...*)[^2*$^n] ¬ª/¬ª(1,|[\*] 1..*)).re}
```
[Try it online!](https://tio.run/##K0gtyjH7n1upoJamYPu/WqO4NFdBw1BHK1NLJa5CT09PSzM6zgjIzotVOLRb/9BuoFxNdIxWrIIhSE5Tryi19n9afpGCjYGegYKhnQ6IYYpgGMEYJjCGJYhhBFRsBGOY2ino2ilEq1ToKKgAranmUlAoTqxUUFKpUKgBigCJ6jQgDZStqFWy5qr9DwA "Perl 6 ‚Äì Try It Online")
This actually computes the complex exponential *eiŒ∏* for twice the number of requested terms and then takes the real part.
[Answer]
# [Julia](http://julialang.org/), 37 bytes
```
x$n=1>(n-=1)||x$n+(-x^2)^n/prod(1:2n)
```
[Try it online!](https://tio.run/##yyrNyUw0rPj/v0Ilz9bQTiNP19ZQs6YGyNPW0K2IM9KMy9MvKMpP0TC0MsrT/O9QnJFfrmCgZ6BiwAVjm6oYIrGNkNgmSGxLKNsIqNcIiW36HwA "Julia 1.0 ‚Äì Try It Online")
[Answer]
# MATLAB with Symbolic Math Toolbox, 57 bytes
```
@(x,n)eval(subs(taylor(sym('cos(x)'),'Order',2*n),'x',x))
```
This defines an anonymous function with that takes `double` inputs `x`,`n` and outputs the result as a `double`.
Example (tested on R2015b):
```
>> @(x,n)eval(subs(taylor(sym('cos(x)'),'Order',2*n),'x',x))
ans = 
    @(x,n)eval(subs(taylor(sym('cos(x)'),'Order',2*n),'x',x))
>> f = ans; format long; f(0,1), f(0.5,1), f(0.5,2), f(0.5,4), f(0.5,9), f(2,2), f(2,5)
ans =
     1
ans =
     1
ans =
   0.875000000000000
ans =
   0.877582465277778
ans =
   0.877582561890373
ans =
    -1
ans =
  -0.415873015873016
```
[Answer]
# JavaScript ES7 60 bytes
```
x=>a=n=>--n?(-1)**n*x**(2*n)/(f=m=>m?m*f(m-1):1)(2*n)+a(n):1
x=>a=n=>                                                         // Curry-d function, it basically returns another function
        --n?                                              :1  // subtract one from n. If n - 1 is 0, return 1
            (-1)**n*                                             // This generates the sign of the number
                    x**(2*n)/                                    // This creates the part before the division, basicaly x^2n
                             (f=m=>m?m*f(m-1):1)(2*n)            // This creates a recursive factorial function and calls it with 2n
                                                     +a(n)    // Recursively call the function. This adds the elements of the taylor series together
```
To use it:
Press F12, type in the function and then do
```
c(x)(n)
```
[Answer]
## C ~~144~~ 130 bytes
```
F(m){int u=1;while(m)u*=m--;return u;}float f(float x,n){float s;for(int i=1;i<n;i++)s+=pow(-1,i)*pow(x,2*i)/(F(2*i));return 1+s;}
```
Ungolfed Version:
```
//Function to calculate factorial
int F(int m)
{
  int u=1;
  while(m>1)
   u*=m--; 
  return u;
}
//actual function called in main function   
float f(float x, int n)
{
  float s=0.0;
  for(int i=1;i<=n-1;i++)
     s+=pow(-1,i)*pow(x,2*i)/(F(2*i)); 
  return 1+s;
 }
```
Thanks Kevin for saving some bytes!
[Answer]
# [Tcl](http://tcl.tk/), 126 bytes
```
proc c {x n k\ 0 r\ 0} {proc F n {expr $n?($n)*\[F $n-1]:1}
time {set r [expr $r+-1**$k*$x**(2*$k)/[F 2*$k]]
incr k} $n
set r}
```
[Try it online!](https://tio.run/##PY6xDoMwDET3fMUNGSAtLURlKEs3fgIYqogBASkKqRQpyrenDkhd/E7WnX1WLTFu5qOg4B005h4lDI0Af@xbWvrRbQZcvzKuc9F3LemiGpoqMDutI/w@Whh0p81cikoIPgvuhMgkqfxOkSSGgU1aGcyBLrAjFeKyvje4Pv1h5a0EKkL9hzzxOPFkMlnkiZqlml@7Q2XcXald06NT4I7uDyH@AA "Tcl ‚Äì Try It Online")
[Answer]
# [Stax](https://github.com/tomtheisen/stax), 12 [bytes](https://github.com/tomtheisen/stax/blob/master/docs/packed.md#packed-stax)
```
√º‚îò¬∑.‚ÅøYe√≤‚â•V√Æ√ª
```
[Run and debug it](https://staxlang.xyz/#p=81d9fa2efc596595f2568c96&i=0.0+1%0A0.5+1%0A0.5+2%0A0.5+4%0A0.5+9%0A2.0+2%0A2.0+5&a=1&m=2)
Unpacked, ungolfed, and commented, it looks like this.
```
            Input is `x n`
Z           Push a zero underneath the top.  The stack is now `x 0 n` 
D           Run the rest of the program n times.
  xJNi|*    (-x*x)^i where i is the iteration index
  iH|F/     divide that by factorial(2i)
  +         add to the running total so far
            final result is implicitly printed
```
[Run this one](https://staxlang.xyz/#c=++++++++%09Input+is+%60x+n%60%0AZ+++++++%09Push+a+zero+underneath+the+top.++The+stack+is+now+%60x+0+n%60+%0AD+++++++%09Run+the+rest+of+the+program+n+times.%0A++xJNi%7C*%09%28-x*x%29%5Ei+where+i+is+the+iteration+index%0A++iH%7CF%2F+%09divide+that+by+factorial%282i%29%0A++%2B+++++%09add+to+the+running+total+so+far%0A++++++++%09final+result+is+implicitly+printed&i=0.0+1%0A0.5+1%0A0.5+2%0A0.5+4%0A0.5+9%0A2.0+2%0A2.0+5&a=1&m=2)
[Answer]
# [Desmos](https://www.desmos.com/calculator), 36 bytes
```
\sum_{k=0}^{n-1}{(-1)^kv^{2k}/(2k)!}
```
[View it on Desmos](https://www.desmos.com/calculator/4roucxcwag)
Unfortunately, not a ton to golf here.
[Answer]
# [Wolfram Language (Mathematica)](https://www.wolfram.com/wolframscript/), 30 bytes
```
Normal[Cos@x+O@x^(2#2)]/.x->#&
```
[Try it online!](https://tio.run/##y00syUjNTSzJTE78n2b73y@/KDcxJ9o5v9ihQtvfoSJOw0jZSDNWX69C105Z7X9AUWZeSbSyrl2ag4NyrFpdcHJiXl01V7WBnoGOYa0OiGGKYBjBGCYwhiWIYQRUbARjmNZy1f4HAA "Wolfram Language (Mathematica) ‚Äì Try It Online")
Mathematica has a more (byte-)efficient way to generate `SeriesData` objects than `Series`. `O[x]^p` truncates the power series of the function at the `p`th power.
[Answer]
# [Husk](https://github.com/barbuz/Husk), 20 bytes
```
‚åä*!9ƒ∞‚Å∞·πÅ¬ß/»ØŒ†·∏£D`^o_‚ñ°‚Å∞≈Ä
```
[Try it online!](https://tio.run/##ATEAzv9odXNr///ijIoqITnEsOKBsOG5gcKnL8ivzqDhuKNEYF5vX@KWoeKBsMWA////Mv81 "Husk ‚Äì Try It Online")
Outputs sign + 9 decimal places; the initial decimal point is implicit.
14 bytes (`·πÅ¬ß/»ØŒ†·∏£D`^o_‚ñ°‚Å∞≈Ä`) to calculate the cosine, plus 6 bytes (`‚åä*!9ƒ∞‚Å∞`) to extract the decimal representation from the a rational number...
[Answer]
# MMIX, 60 bytes (15 instrs)
```
cos SETH $5,#3FF0       // E0053FF0: one = 1.0
    SETL $2,0           // E3020000: twon = 0
    SET  $3,$5          // C1030500: term = one
    FMUL $4,$0,$0       // 10040000: numer = x *. x
    SETL $0,0           // E3000000: accum = 0
    INCH $4,#8000       // E4048000: numer = -.numer
0H  FADD $0,$0,$3       // 04000003: loop: accum +.= term
    FADD $255,$2,$5     // 04FF0205: temp = twon +. 1
    FADD $2,$255,$5     // 0402FF05: twon = temp +. 1
    FMUL $255,$255,$2   // 10FFFF02: temp *.= twon
    FDIV $255,$4,$255   // 14FF04FF: temp = numer /. temp
    FMUL $3,$3,$255     // 100303FF: term *.= temp
    SUBU $1,$1,1        // 27010101: count--
    PBZ  $1,0B          // 5301FFF9: likelyif(!count) goto loop
    POP  1,0            // F8010000: return(accum)
```
This is a really simpleminded algorithm. I managed to avoid using more than three stack temporaries (`$255` is global and free for temporary use, any register numbered less than 32 is guaranteed on-stack).
A few clever tricks and misc notes:  
`SET $X,$Y` is a short version of `OR $X,$Y,0`;  
it's nice that 1.0 fits entirely in the top quarter of a float, otherwise we would have had to use another instruction to set it;  
`PBZ` is a predicted branch (faster if taken) if the register operand is 0;  
the `0H` is a local label that the `0B` refers back to;
and `POP a,b` means return `a` local variables and skip `b` instructions after the function call.
]