package scotch.data.fractional;
import static java.util.Arrays.asList;
import static scotch.runtime.RuntimeSupport.callable;
import java.util.List;
import scotch.data.double_.Double_;
import scotch.data.num.Num;
import scotch.data.ratio.Ratio;
import scotch.runtime.Callable;
import scotch.symbol.InstanceGetter;
import scotch.symbol.TypeInstance;
import scotch.symbol.TypeParameters;
import scotch.symbol.type.TypeDescriptor;
@SuppressWarnings("unused")
@TypeInstance(typeClass = "scotch.data.fractional.Fractional")
public class FractionalDouble implements Fractional<Double> {
private static final Callable<FractionalDouble> INSTANCE = callable(FractionalDouble::new);
@InstanceGetter
public static Callable<FractionalDouble> instance() {
return INSTANCE;
}
@TypeParameters
public static List<TypeDescriptor> parameters() {
return asList(Double_.TYPE);
}
private FractionalDouble() {
// intentionally empty
}
@Override
public Callable<Double> divide(Callable<Num<Double>> num, Callable<Double> left, Callable<Double> right) {
return callable(() -> left.call() / right.call());
}
@Override
public Callable<Double> fromRatio(Callable<Num<Double>> num, Callable<Ratio<Integer>> ratio) {
return callable(() -> ratio.call().getNumerator().call().doubleValue() / ratio.call().getDenominator().call().doubleValue());
}
@Override
public Callable<Double> reciprocal(Callable<Num<Double>> num, Callable<Double> operand) {
return callable(() -> 1d / operand.call());
}
}