Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // math.cfg by |FOX|Yarukinasu
- // mathematical constants
- E = 2.718281828
- PI = 3.141592653
- // equal to
- == = [ result (= $arg1 $arg2) ]
- // bitwise AND
- & = [ result (&& (arg1) (arg2)) ]
- // bitwise inclusive OR
- | = [ result (|| (arg1) (arg2)) ]
- // bitwise exclusive OR
- ^ = [ result (!= (arg1) (arg2)) ]
- // returns the absolute value
- abs = [ if (< $arg1) [ result (* $arg1 -1) ] [ result $arg1 ] ]
- absf = [ if (<f $arg1) [ result (*f $arg1 -1) ] [ result (+f $arg1) ] ]
- div= = [ $arg1 = (div (getalias $arg1) $arg2) ]
- div=f = [ $arg1 = (divf (getalias $arg1) $arg2) ]
- // natural logarithm
- log = [ result (divf (log2 $arg1) (log2 $E)) ]
- // common logarithm
- log10 = [ result (divf (log2 $arg1) (log2 10)) ]
- // binary logarithm
- log2 = [
- tmpval = $arg1
- tmpres = 0
- while [ (< $tmpval 1) ] [
- -=f tmpres 1
- *=f tmpval 2 ]
- while [ (>= $tmpval 2) ] [
- +=f tmpres 1
- div=f tmpval 2 ]
- tmpfp = 1
- while [ (>=f $tmpfp 0.000001) ] [
- div=f tmpfp 2
- *=f tmpval $tmpval
- if (>= $tmpval 2) [
- div=f tmpval 2
- +=f tmpres $tmpfp ] ]
- result $tmpres ]
- // returns the higher number
- max = [ if (>=f $arg1 $arg2) [ result $arg1 ] [ result $arg2 ] ]
- // returns the lower number
- min = [ if (<=f $arg1 $arg2) [ result $arg1 ] [ result $arg2 ] ]
- // converts to radians
- toradians = [ result (*f (divf $arg1 180) $PI) ]
- // converts to degrees
- todegrees = [ result (divf (*f $arg1 180) $PI) ]
- // greatest common divisor
- gcd = [ // Euclidean algorithm
- tmpa = $arg1
- tmpb = $arg2
- tmpc = 0
- while [ (!= $tmpb 0) ] [
- tmpc = $tmpb
- tmpb = (mod $tmpa $tmpb)
- tmpa = $tmpc ]
- result $tmpa ]
- // least common multiple
- lcm = [
- args = $arg1
- loop i (- $numargs 1) [ args = (concat $args (getalias (format "arg%1" (+ $i 2)))) ]
- if (= (listlen $args) 2) [
- result (div (* (at $args) (at $args 1)) (gcd (at $args) (at $args 1))) ] [
- tmpargs = (at $args 1)
- loop i (- (listlen $args) 2) [ tmpargs = (concat $tmpargs (at $args (+ $i 2))) ]
- result (lcm (at $args) (lcm $tmpargs)) ] ]
- // returns the factorial
- factorial = [ // iterative algorithm
- tmpproduct = 1
- loop i $arg1 [ *= tmpproduct (+ $i 1) ]
- result $tmpproduct ]
- // returns the power
- pow = [ // iterative algorithm
- if $arg2 [
- tmpbase = $arg1
- tmpexp = (- (abs $arg2) 1)
- loop i $tmpexp [ *=f tmpbase $arg1 ]
- if (> $arg2) [ result $tmpbase ] [ result (divf 1 $tmpbase) ] ] [ result 1 ] ]
- // returns the square root
- sqrt = [ result (pow 2 (divf (log2 $arg1) 2)) ]
- // returns the sine
- sin = [ // Taylor series
- tmparg = (toradians $arg1)
- tmpres = $tmparg
- adding = 0
- loop i 5 [
- tmpa = (- (* (+ $i 2) 2) 1)
- tmpb = (divf (pow $tmparg $tmpa) (factorial $tmpa))
- if $adding [ +=f tmpres $tmpb; adding = 0 ] [ -=f tmpres $tmpb; adding = 1 ] ]
- result $tmpres ]
- // returns the cosine
- cos = [ result (sin (- 90 $arg1)) ]
- // returns the tangent
- tan = [ result (divf (sin $arg1) (cos $arg1)) ]
- // returns the cosecant
- csc = [ result (divf 1 (sin $arg1)) ]
- // returns the secant
- sec = [ result (divf 1 (cos $arg1)) ]
- // returns the cotangent
- cot = [ result (divf 1 (tan $arg1)) ]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement