Double::sum
is a method reference for adding doubles, ie. (a,b) -> a+b
.
Why is there not a method reference for minus in the JDK? I.e. (a,b) -> a-b
?
Double::sum
is a method reference for adding doubles, ie. (a,b) -> a+b
.
Why is there not a method reference for minus in the JDK? I.e. (a,b) -> a-b
?
The primary purpose of methods like Double.sum
is to aid use cases like reduction or Arrays.parallelPrefix
Since minus is not an associative function, it is not appropriate for this use case.
Adding 3rd party library dependencies just for the sake of such a simple method, is not recommended. Keep in mind, that you always can create your own method, if you prefer named methods over lambda expressions.
E.g.
static double minus(double a, double b) {
return a-b;
}
and using MyClass::minus
is as informative as ThirdPartyClass::minus
…
There is no inbuilt method. But I don't think, itdo any other magic than simply doing
double result = a-b;
It seems you are over thinking here :)
As such there is no inbuilt method which is used to subtract the doubles. However if you are using BigDecimal to store the decimals then you can try this:
BigDecimal a = new BigDecimal("10.6");
BigDecimal b = new BigDecimal("4.3");
BigDecimal result = a.subtract(b);
System.out.println(result);
else you can use the simple way of subtracting two numbers ie, a-b. Also to note that in Java, double values are IEEE floating point numbers