Calculate e (example)

By QuickStep on Jun 27, 2005

This snippet shows you how to calculate the number e. Usage $e(N), where N is the precission. Also includes a fac alias that is used for the e calculation, but can also be used alone. NOTE: this is an example calculation

alias e {

  ; Alias for a calculation of e, requires N for precission, example:
  ; $e(5) or $e(10)
  ; If N = 10, it will already yield a very accurate result

  var %e = 0, %a = 0
  while (%a <= $$1) {
    %e = $calc(%e + (1 / $fac(%a)))
    inc %a
  }
  return %e
}

alias fac {

  ; Alias for fac-calculations, example:
  ; 2! = $fac(2)    10! = $fac(10)

  if (!$$1) return 1
  var %a = $abs($int($1)), %b = %a
  while $calc(%a - 1) {
    %b = $calc(%b * (%a - 1))
    dec %a
  }
  return $iif($$1 < 0,-) $+ %b
}

Comments

Sign in to comment.
aeros   -  Jun 29, 2005

huh?

 Respond  
Are you sure you want to unfollow this person?
Are you sure you want to delete this?
Click "Unsubscribe" to stop receiving notices pertaining to this post.
Click "Subscribe" to resume notices pertaining to this post.