add MathUtil#1092
Conversation
| } | ||
|
|
||
| public static int safeDoubleToInt(double d) { | ||
| if (Double.isNaN(d)) { |
There was a problem hiding this comment.
I don't know how much of a difference this makes (it could when used in a tight loop), but only one check is necessary. If we use Double.isFinite() instead, it will also catch NaN. Double.isFinite(Double.NaN) returns false just as Double.isInfinite(Double.NaN), the functions are not symmetric in this aspect. So this should catch both:
if (!Double.isFinite(d)) { // DO NOT REPLACE WITH Double.isInfinite(d) !!!
throw new IllegalArgumentException("Cannot convert number to int: " + d);
}| } | ||
| @Override | ||
| public int getAlpha() { return (int)Math.round(color.getAlpha()*100000./255.); } | ||
| public int getAlpha() { return Math.toIntExact(Math.round(color.getAlpha()*100000./255.)); } |
There was a problem hiding this comment.
I think this would be a good candidate for the new MathUtil class:
public static int roundToInt(double d) {
return safeDoubleToInt(Math.rint(d));
}I used your rint() version I discovered below because it will handle infinity and NaN correcty.
| } | ||
|
|
||
| return (int)Math.rint(targetSize); | ||
| return MathUtil.safeDoubleToInt(Math.rint(targetSize)); |
There was a problem hiding this comment.
I think should use a method in MathUtil (as described above). Actually using MathUtil.safeDoubleToInt(Math.rint(...)) seems like a good idea as infinity and NaN should be handled correctly.
can make more of use of it in later commits and also need to extend test coverage