2

Based on this java.lang.Double source code I tried to get these constants to use in an Apex port of a Java open source algorithm:

private static final Double DOUBLE_NEGATIVE_INFINITY = Double.valueOf(-1.0) / Double.valueOf(0.0);
private static final Double DOUBLE_POSITIVE_INFINITY = Double.valueOf(1.0) / Double.valueOf(0.0);
private static final Double DOUBLE_NaN = Double.valueOf(0.0) / Double.valueOf(0.0);

but all of these report a System.MathException: Divide by 0.

Is there a way to reference these values in Apex?

1

2 Answers 2

1

We don't have very many constants at all, aside from the system enums, two constants in Math (E and PI), that's about it. You'll need to adjust the algorithm to detect those conditions directly. I'd probably create some kind of enum.

public enum DOUBLE_STATE { NEGATIVE_INFINITY, POSITIVE_INFINITY, NAN }
1

I found this way of creating the infinity constants which is the main part of what I need:

final Double POSITIVE_INFINITY = Math.pow(10, 300) / Math.pow(10, -300);
final Double NEGATIVE_INFINITY = -POSITIVE_INFINITY;

allowing:

private static Boolean isFinite(Double d) {
    return d != null && d != POSITIVE_INFINITY && d != NEGATIVE_INFINITY;
}

but didn't find a way of creating NaN as these all throw exceptions rather than produce the NaN result:

0.0 / 0.0
Math.sqrt(-1)
Math.mod(10.0, 0)

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .