Skip to content

Commit 2e2b65c

Browse files
authored
Merge pull request #46 from javecs/feature/math-ref
ビルトイン関数を追加しました。
2 parents 82eb929 + 48bb7dd commit 2e2b65c

3 files changed

Lines changed: 30 additions & 10 deletions

File tree

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -107,9 +107,9 @@
107107
108108
関数名 | 説明 | 例 | 結果
109109
--------| --------|--------|--------
110-
 sin | サイン。三角関数の正弦。 | sin(1) | 0.8414709848078965
111-
 cos | コサイン。三角関数の余弦。 | cos(1) | 0.5403023058681398
112-
 tan | タンジェント。三角関数の正接。 | tan(1) | 1.5574077246549023
110+
[sin](https://docs.oracle.com/javase/jp/8/docs/api/java/lang/Math.html#sin-double-) | 指定された角度の正弦(サイン)を返します。| sin(1) | 0.8414709848078965
111+
[cos](https://docs.oracle.com/javase/jp/8/docs/api/java/lang/Math.html#cos-double-) | 指定された角度の余弦(コサイン)を返します。| cos(1) | 0.5403023058681398
112+
[tan](https://docs.oracle.com/javase/jp/8/docs/api/java/lang/Math.html#tan-double-) | 指定された角度の正接(タンジェント)を返します。| tan(1) | 1.5574077246549023
113113
114114
- 関数名の文字は、大文字と小文字を区別しません。
115115
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package xyz.javecs.tools.expr
2+
3+
fun builtIn() = HashMap<String, (Double) -> Double>().apply {
4+
put("sin", { x -> Math.sin(x) })
5+
put("cos", { x -> Math.cos(x) })
6+
put("tan", { x -> Math.tan(x) })
7+
put("asin", { x -> Math.asin(x) })
8+
put("acos", { x -> Math.acos(x) })
9+
put("atan", { x -> Math.atan(x) })
10+
put("exp", { x -> Math.exp(x) })
11+
put("log", { x -> Math.log(x) })
12+
put("log10", { x -> Math.log10(x) })
13+
put("sqrt", { x -> Math.sqrt(x) })
14+
put("cbrt", { x -> Math.cbrt(x) })
15+
put("ceil", { x -> Math.ceil(x) })
16+
put("floor", { x -> Math.floor(x) })
17+
put("rint", { x -> Math.rint(x) })
18+
put("abs", { x -> Math.abs(x) })
19+
put("ulp", { x -> Math.ulp(x) })
20+
put("signum", { x -> Math.signum(x) })
21+
put("sinh", { x -> Math.sinh(x) })
22+
put("cosh", { x -> Math.cosh(x) })
23+
put("tanh", { x -> Math.tanh(x) })
24+
put("expm1", { x -> Math.expm1(x) })
25+
put("log1p", { x -> Math.log1p(x) })
26+
}

src/main/kotlin/xyz/javecs/tools/expr/Calculator.kt

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import kotlin.collections.HashMap
55
class Calculator(expressions: Array<String> = emptyArray()) : EvalContext {
66
private val evaluator = EvalVisitor(this)
77
private val symbol: HashMap<String, Double> = HashMap()
8-
private val function: HashMap<String, (Double) -> Double> = HashMap()
8+
private val function = builtIn()
99
private var expression = Expression()
1010
var value: Number = expression.value
1111
get() = expression.getValue()
@@ -14,12 +14,6 @@ class Calculator(expressions: Array<String> = emptyArray()) : EvalContext {
1414
for (expr in expressions) {
1515
eval(expr)
1616
}
17-
18-
function.apply {
19-
put("sin", { x -> Math.sin(x) })
20-
put("cos", { x -> Math.cos(x) })
21-
put("tan", { x -> Math.tan(x) })
22-
}
2317
}
2418

2519
fun eval(expr: String = ""): Calculator {

0 commit comments

Comments
 (0)