aboutsummaryrefslogtreecommitdiff
path: root/source/lux/math.lux
blob: a60ce512c6ce9a403a1e77467e371a2a87aa435a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
##  Copyright (c) Eduardo Julian. All rights reserved.
##  This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0.
##  If a copy of the MPL was not distributed with this file,
##  You can obtain one at http://mozilla.org/MPL/2.0/.

(;import lux
         (lux/data/number/int #open ("i:" Int/Number)))

## [Constants]
(do-template [<name> <value>]
  [(def #export <name>
     Real
     (_jvm_getstatic "java.lang.Math" <value>))]

  [e  "E"]
  [pi "PI"]
  )

## [Functions]
(do-template [<name> <method>]
  [(def #export (<name> n)
     (-> Real Real)
     (_jvm_invokestatic "java.lang.Math" <method> ["double"] [n]))]

  [cos "cos"]
  [sin "sin"]
  [tan "tan"]

  [acos "acos"]
  [asin "asin"]
  [atan "atan"]
  
  [cosh "cosh"]
  [sinh "sinh"]
  [tanh "tanh"]

  [ceil  "ceil"]
  [floor "floor"]

  [exp "exp"]
  [log "log"]
  
  [cbrt "cbrt"]
  [sqrt "sqrt"]

  [->degrees "toDegrees"]
  [->radians "toRadians"]
  )

(def #export (round n)
  (-> Real Int)
  (_jvm_invokestatic "java.lang.Math" "round" ["double"] [n]))

(do-template [<name> <method>]
  [(def #export (<name> x y)
     (-> Real Real Real)
     (_jvm_invokestatic "java.lang.Math" <method> ["double" "double"] [x y]))]

  [atan2 "atan2"]
  [pow   "pow"]
  )

(def (gcd' a b)
  (-> Int Int Int)
  (case b
    0 a
    _ (gcd' b (i:% a b))))

(def #export (gcd a b)
  (-> Int Int Int)
  (gcd' (i:abs a) (i:abs b)))

(def #export (lcm x y)
  (-> Int Int Int)
  (case [x y]
    (\or [_ 0] [0 _])
    0

    _
    (i:abs (i:* (i:/ x (gcd x y)) y))))