Advertisement
Yarukinasu

math.cfg

Mar 27th, 2011
313
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // math.cfg by |FOX|Yarukinasu
  2.  
  3. // mathematical constants
  4. E = 2.718281828
  5. PI = 3.141592653
  6.  
  7. // equal to
  8. == = [ result (= $arg1 $arg2) ]
  9.  
  10. // bitwise AND
  11. & = [ result (&& (arg1) (arg2)) ]
  12.  
  13. // bitwise inclusive OR
  14. | = [ result (|| (arg1) (arg2)) ]
  15.  
  16. // bitwise exclusive OR
  17. ^ = [ result (!= (arg1) (arg2)) ]
  18.  
  19. // returns the absolute value
  20. abs = [ if (< $arg1) [ result (* $arg1 -1) ] [ result $arg1 ] ]
  21. absf = [ if (<f $arg1) [ result (*f $arg1 -1) ] [ result (+f $arg1) ] ]
  22.  
  23. div= = [ $arg1 = (div (getalias $arg1) $arg2) ]
  24. div=f = [ $arg1 = (divf (getalias $arg1) $arg2) ]
  25.  
  26. // natural logarithm
  27. log = [ result (divf (log2 $arg1) (log2 $E)) ]
  28.  
  29. // common logarithm
  30. log10 = [ result (divf (log2 $arg1) (log2 10)) ]
  31.  
  32. // binary logarithm
  33. log2 = [
  34.   tmpval = $arg1
  35.   tmpres = 0
  36.   while [ (< $tmpval 1) ] [
  37.     -=f tmpres 1
  38.     *=f tmpval 2 ]
  39.   while [ (>= $tmpval 2) ] [
  40.     +=f tmpres 1
  41.     div=f tmpval 2 ]
  42.   tmpfp = 1
  43.   while [ (>=f $tmpfp 0.000001) ] [
  44.     div=f tmpfp 2
  45.     *=f tmpval $tmpval
  46.     if (>= $tmpval 2) [
  47.       div=f tmpval 2
  48.       +=f tmpres $tmpfp ] ]
  49.   result $tmpres ]
  50.  
  51. // returns the higher number
  52. max = [ if (>=f $arg1 $arg2) [ result $arg1 ] [ result $arg2 ] ]
  53.  
  54. // returns the lower number
  55. min = [ if (<=f $arg1 $arg2) [ result $arg1 ] [ result $arg2 ] ]
  56.  
  57. // converts to radians
  58. toradians = [ result (*f (divf $arg1 180) $PI) ]
  59.  
  60. // converts to degrees
  61. todegrees = [ result (divf (*f $arg1 180) $PI) ]
  62.  
  63. // greatest common divisor
  64. gcd = [ // Euclidean algorithm
  65.   tmpa = $arg1
  66.   tmpb = $arg2
  67.   tmpc = 0
  68.   while [ (!= $tmpb 0) ] [
  69.     tmpc = $tmpb
  70.     tmpb = (mod $tmpa $tmpb)
  71.     tmpa = $tmpc ]
  72.   result $tmpa ]
  73.  
  74. // least common multiple
  75. lcm = [
  76.   args = $arg1
  77.   loop i (- $numargs 1) [ args = (concat $args (getalias (format "arg%1" (+ $i 2)))) ]
  78.   if (= (listlen $args) 2) [
  79.     result (div (* (at $args) (at $args 1)) (gcd (at $args) (at $args 1))) ] [
  80.     tmpargs = (at $args 1)
  81.     loop i (- (listlen $args) 2) [ tmpargs = (concat $tmpargs (at $args (+ $i 2))) ]
  82.     result (lcm (at $args) (lcm $tmpargs)) ] ]
  83.  
  84. // returns the factorial
  85. factorial = [ // iterative algorithm
  86.   tmpproduct = 1
  87.   loop i $arg1 [ *= tmpproduct (+ $i 1) ]
  88.   result $tmpproduct ]
  89.  
  90. // returns the power
  91. pow = [ // iterative algorithm
  92.   if $arg2 [
  93.     tmpbase = $arg1
  94.     tmpexp = (- (abs $arg2) 1)
  95.     loop i $tmpexp [ *=f tmpbase $arg1 ]
  96.     if (> $arg2) [ result $tmpbase ] [ result (divf 1 $tmpbase) ] ] [ result 1 ] ]
  97.  
  98. // returns the square root
  99. sqrt = [ result (pow 2 (divf (log2 $arg1) 2)) ]
  100.  
  101. // returns the sine
  102. sin = [ // Taylor series
  103.   tmparg = (toradians $arg1)
  104.   tmpres = $tmparg
  105.   adding = 0
  106.   loop i 5 [
  107.     tmpa = (- (* (+ $i 2) 2) 1)
  108.     tmpb = (divf (pow $tmparg $tmpa) (factorial $tmpa))
  109.     if $adding [ +=f tmpres $tmpb; adding = 0 ] [ -=f tmpres $tmpb; adding = 1 ] ]
  110.   result $tmpres ]
  111.  
  112. // returns the cosine
  113. cos = [ result (sin (- 90 $arg1)) ]
  114.  
  115. // returns the tangent
  116. tan = [ result (divf (sin $arg1) (cos $arg1)) ]
  117.  
  118. // returns the cosecant
  119. csc = [ result (divf 1 (sin $arg1)) ]
  120.  
  121. // returns the secant
  122. sec = [ result (divf 1 (cos $arg1)) ]
  123.  
  124. // returns the cotangent
  125. cot = [ result (divf 1 (tan $arg1)) ]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement