%mathpiper,def="MathExpTaylor"

// simple Taylor expansion, use only for 0<=x<1
Defun("MathExpTaylor0",{x})
[
  Local(i,aResult,term,eps);
  // Exp(x)=Sum(i=0 to Inf)  x^(i) /(i)!
  // Which incrementally becomes the algorithm:
  //
  // i <- 0
  Bind(i,0);
  // sum <- 1
  Bind(aResult,1.0);
  // term <- 1
  Bind(term,1.0);
  Bind(eps,MathIntPower(10,MathNegate(BuiltinPrecisionGet())));
  // While (term>epsilon)
  While(IsGreaterThan(AbsN(term),eps))
  [
    //   i <- i+1
    Bind(i,AddN(i,1));
    //   term <- term*x/(i)
    Bind(term,DivideN(MultiplyN(term,x),i));
    //   sum <- sum+term
    Bind(aResult,AddN(aResult,term));
  ];
  aResult;
];

%/mathpiper