|
4 | 4 | import java.util.function.DoubleBinaryOperator;
|
5 | 5 | import java.util.function.DoubleFunction;
|
6 | 6 | import java.util.function.DoubleUnaryOperator;
|
| 7 | +import java.util.function.UnaryOperator; |
7 | 8 |
|
8 | 9 | /**
|
9 | 10 | *
|
10 | 11 | * @author aNNiMON
|
11 | 12 | */
|
12 | 13 | public final class math implements Module {
|
13 |
| - |
| 14 | + |
14 | 15 | private static final DoubleFunction<NumberValue> doubleToNumber = NumberValue::of;
|
15 | 16 |
|
16 | 17 | @Override
|
17 | 18 | public void init() {
|
18 |
| - Functions.set("abs", functionConvert(Math::abs)); |
| 19 | + Functions.set("abs", math::abs); |
19 | 20 | Functions.set("acos", functionConvert(Math::acos));
|
20 | 21 | Functions.set("asin", functionConvert(Math::asin));
|
21 | 22 | Functions.set("atan", functionConvert(Math::atan));
|
22 | 23 | Functions.set("atan2", biFunctionConvert(Math::atan2));
|
23 | 24 | Functions.set("cbrt", functionConvert(Math::cbrt));
|
24 | 25 | Functions.set("ceil", functionConvert(Math::ceil));
|
25 |
| - Functions.set("copySign", biFunctionConvert(Math::copySign)); |
| 26 | + Functions.set("copySign", math::copySign); |
26 | 27 | Functions.set("cos", functionConvert(Math::cos));
|
27 | 28 | Functions.set("cosh", functionConvert(Math::cosh));
|
28 | 29 | Functions.set("exp", functionConvert(Math::exp));
|
29 | 30 | Functions.set("expm1", functionConvert(Math::expm1));
|
30 | 31 | Functions.set("floor", functionConvert(Math::floor));
|
31 |
| - Functions.set("getExponent", functionConvert(Math::getExponent)); |
| 32 | + Functions.set("getExponent", math::getExponent); |
32 | 33 | Functions.set("hypot", biFunctionConvert(Math::hypot));
|
33 | 34 | Functions.set("IEEEremainder", biFunctionConvert(Math::IEEEremainder));
|
34 | 35 | Functions.set("log", functionConvert(Math::log));
|
35 | 36 | Functions.set("log1p", functionConvert(Math::log1p));
|
36 | 37 | Functions.set("log10", functionConvert(Math::log10));
|
37 |
| - Functions.set("max", biFunctionConvert(Math::max)); |
38 |
| - Functions.set("min", biFunctionConvert(Math::min)); |
39 |
| - Functions.set("nextAfter", biFunctionConvert(Math::nextAfter)); |
40 |
| - Functions.set("nextUp", functionConvert(Math::nextUp)); |
| 38 | + Functions.set("max", math::max); |
| 39 | + Functions.set("min", math::min); |
| 40 | + Functions.set("nextAfter", math::nextAfter); |
| 41 | + Functions.set("nextUp", functionConvert(Math::nextUp, Math::nextUp)); |
| 42 | + Functions.set("nextDown", functionConvert(Math::nextDown, Math::nextDown)); |
41 | 43 | Functions.set("pow", biFunctionConvert(Math::pow));
|
42 | 44 | Functions.set("rint", functionConvert(Math::rint));
|
43 |
| - Functions.set("round", functionConvert(Math::round)); |
44 |
| - Functions.set("signum", functionConvert(Math::signum)); |
| 45 | + Functions.set("round", math::round); |
| 46 | + Functions.set("signum", functionConvert(Math::signum, Math::signum)); |
45 | 47 | Functions.set("sin", functionConvert(Math::sin));
|
46 | 48 | Functions.set("sinh", functionConvert(Math::sinh));
|
47 | 49 | Functions.set("sqrt", functionConvert(Math::sqrt));
|
48 | 50 | Functions.set("tan", functionConvert(Math::tan));
|
49 | 51 | Functions.set("tanh", functionConvert(Math::tanh));
|
50 | 52 | Functions.set("toDegrees", functionConvert(Math::toDegrees));
|
51 | 53 | Functions.set("toRadians", functionConvert(Math::toRadians));
|
52 |
| - Functions.set("ulp", functionConvert(Math::ulp)); |
| 54 | + Functions.set("ulp", functionConvert(Math::ulp, Math::ulp)); |
53 | 55 |
|
54 | 56 | Variables.set("PI", NumberValue.of(Math.PI));
|
55 | 57 | Variables.set("E", NumberValue.of(Math.E));
|
56 | 58 | }
|
57 |
| - |
| 59 | + |
| 60 | + private static Value abs(Value... args) { |
| 61 | + Arguments.check(1, args.length); |
| 62 | + final Object raw = args[0].raw(); |
| 63 | + if (raw instanceof Double) { |
| 64 | + return NumberValue.of(Math.abs((double) raw)); |
| 65 | + } |
| 66 | + if (raw instanceof Float) { |
| 67 | + return NumberValue.of(Math.abs((float) raw)); |
| 68 | + } |
| 69 | + if (raw instanceof Long) { |
| 70 | + return NumberValue.of(Math.abs((long) raw)); |
| 71 | + } |
| 72 | + return NumberValue.of(Math.abs(args[0].asInt())); |
| 73 | + } |
| 74 | + |
| 75 | + private static Value copySign(Value... args) { |
| 76 | + Arguments.check(2, args.length); |
| 77 | + final Object raw = args[0].raw(); |
| 78 | + if (raw instanceof Float) { |
| 79 | + return NumberValue.of(Math.copySign((float) raw, ((NumberValue) args[1]).asFloat())); |
| 80 | + } |
| 81 | + return NumberValue.of(Math.copySign(args[0].asNumber(), args[1].asNumber())); |
| 82 | + } |
| 83 | + |
| 84 | + private static Value getExponent(Value... args) { |
| 85 | + Arguments.check(1, args.length); |
| 86 | + final Object raw = args[0].raw(); |
| 87 | + if (raw instanceof Float) { |
| 88 | + return NumberValue.of(Math.getExponent((float) raw)); |
| 89 | + } |
| 90 | + return NumberValue.of(Math.getExponent(args[0].asNumber())); |
| 91 | + } |
| 92 | + |
| 93 | + private static Value max(Value... args) { |
| 94 | + Arguments.check(2, args.length); |
| 95 | + final Object raw = args[0].raw(); |
| 96 | + if (raw instanceof Double) { |
| 97 | + return NumberValue.of(Math.max((double) raw, args[1].asNumber())); |
| 98 | + } |
| 99 | + if (raw instanceof Float) { |
| 100 | + return NumberValue.of(Math.max((float) raw, ((NumberValue) args[1]).asFloat())); |
| 101 | + } |
| 102 | + if (raw instanceof Long) { |
| 103 | + return NumberValue.of(Math.max((long) raw, ((NumberValue) args[1]).asLong())); |
| 104 | + } |
| 105 | + return NumberValue.of(Math.max(args[0].asInt(), args[1].asInt())); |
| 106 | + } |
| 107 | + |
| 108 | + private static Value min(Value... args) { |
| 109 | + Arguments.check(2, args.length); |
| 110 | + final Object raw = args[0].raw(); |
| 111 | + if (raw instanceof Double) { |
| 112 | + return NumberValue.of(Math.min((double) raw, args[1].asNumber())); |
| 113 | + } |
| 114 | + if (raw instanceof Float) { |
| 115 | + return NumberValue.of(Math.min((float) raw, ((NumberValue) args[1]).asFloat())); |
| 116 | + } |
| 117 | + if (raw instanceof Long) { |
| 118 | + return NumberValue.of(Math.min((long) raw, ((NumberValue) args[1]).asLong())); |
| 119 | + } |
| 120 | + return NumberValue.of(Math.min(args[0].asInt(), args[1].asInt())); |
| 121 | + } |
| 122 | + |
| 123 | + private static Value nextAfter(Value... args) { |
| 124 | + Arguments.check(2, args.length); |
| 125 | + final Object raw = args[0].raw(); |
| 126 | + if (raw instanceof Float) { |
| 127 | + return NumberValue.of(Math.nextAfter((float) raw, args[1].asNumber())); |
| 128 | + } |
| 129 | + return NumberValue.of(Math.nextAfter(args[0].asNumber(), args[1].asNumber())); |
| 130 | + } |
| 131 | + |
| 132 | + private static Value round(Value... args) { |
| 133 | + Arguments.check(1, args.length); |
| 134 | + final Object raw = args[0].raw(); |
| 135 | + if (raw instanceof Float) { |
| 136 | + return NumberValue.of(Math.round((float) raw)); |
| 137 | + } |
| 138 | + return NumberValue.of(Math.round(args[0].asNumber())); |
| 139 | + } |
| 140 | + |
| 141 | + |
58 | 142 | private static Function functionConvert(DoubleUnaryOperator op) {
|
59 | 143 | return args -> {
|
60 | 144 | Arguments.check(1, args.length);
|
61 | 145 | return doubleToNumber.apply(op.applyAsDouble(args[0].asNumber()));
|
62 | 146 | };
|
63 | 147 | }
|
64 |
| - |
| 148 | + |
| 149 | + private static Function functionConvert(DoubleUnaryOperator opDouble, UnaryOperator<Float> opFloat) { |
| 150 | + return args -> { |
| 151 | + Arguments.check(1, args.length); |
| 152 | + final Object raw = args[0].raw(); |
| 153 | + if (raw instanceof Float) { |
| 154 | + return NumberValue.of(opFloat.apply((float) raw)); |
| 155 | + } |
| 156 | + return NumberValue.of(opDouble.applyAsDouble(args[0].asNumber())); |
| 157 | + }; |
| 158 | + } |
| 159 | + |
65 | 160 | private static Function biFunctionConvert(DoubleBinaryOperator op) {
|
66 | 161 | return args -> {
|
67 | 162 | Arguments.check(2, args.length);
|
|
0 commit comments