今日も朝早くから「もっと Lambda」シリーズの始まりです。
Java Day Tokyo 2013 でテストパイロットを大募集していたらしいので UnaryOperator を試しがてらにちょっと確認を。
まず UnaryOperator の確認をします。
@FunctionalInterface
public interface UnaryOperator<T> extends Function<T,T>
An operation upon a single operand yielding a result. The operand and the result are of the same type. This is a specialization of Function for the case where the operand and result are of the same type.
パラメータ:
T– the type of operand toapplyand of the result
日付:
1.8
参照:
そして今回使う DoubleUnaryOperator は次のように double 専用に用意されたものです。
@FunctionalInterface
public interface DoubleUnaryOperator
An operation on a double operand yielding a double result. This is the primitive type specialization of UnaryOperator for double.
日付:
1.8
参照:
applyAsDouble(double operand) は次のようになってます。
java.util.function.DoubleUnaryOperator
public double applyAsDouble(double operand)
Returns the double result of the operation upon the double operand.
パラメータ:
operand– the operand value
戻り値:
the operation result value
それでは無限小から 1 を引いた値の表示とその値と無限小が等しいか確認するプログラムを創ってみました。
ついでに無限小から無限小を引いてみました。(普通ありえない計算ですよね(^_^;)
package jp.yucchi.tryintunaryoperator;
import java.util.function.DoubleUnaryOperator;
public class TryIntUnaryOperator {
public static void main(String[] args) {
DoubleUnaryOperator operator = f -> f - 1;
double j = operator.applyAsDouble(Double.NEGATIVE_INFINITY);
System.out.println(j);
if(Double.NEGATIVE_INFINITY == j){
System.out.println("無限小");
}else{
System.out.println("なんでやねん。");
}
DoubleUnaryOperator operator2 = f -> f - Double.NEGATIVE_INFINITY;
double k = operator2.applyAsDouble(Double.NEGATIVE_INFINITY);
System.out.println(k);
if(0 == k){
System.out.println("0");
}else{
System.out.println("むりぽ");
}
}
}
実行結果は次のように期待通りになりました。
-Infinity
無限小
NaN
むりぽ
ここでバグがでたら片言の英語で「なんでやねん?」と言ってやろうと思ったがこんな単純なバグはそうそうあるもんじゃないですね。