xzel23 commented on code in PR #1092: URL: https://github.com/apache/poi/pull/1092#discussion_r3322085145
########## poi/src/main/java/org/apache/poi/util/MathUtil.java: ########## @@ -0,0 +1,47 @@ +/* ==================================================================== + Licensed to the Apache Software Foundation (ASF) under one or more + contributor license agreements. See the NOTICE file distributed with + this work for additional information regarding copyright ownership. + The ASF licenses this file to You under the Apache License, Version 2.0 + (the "License"); you may not use this file except in compliance with + the License. You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +==================================================================== */ + +package org.apache.poi.util; + +/** + * Utility methods for dealing with conversions + * + * @since 6.0.0 + */ +public class MathUtil { + private MathUtil() {} + + public static int safeFloatToInt(float f) { + if (f > Integer.MAX_VALUE || f < Integer.MIN_VALUE) { + throw new IllegalArgumentException("Value out of range: " + f); + } + return (int) f; + } + + public static int safeDoubleToInt(double d) { + if (Double.isNaN(d)) { Review 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: ```java if (!Double.isFinite(d)) { // DO NOT REPLACE WITH Double.isInfinite(d) !!! throw new IllegalArgumentException("Cannot convert number to int: " + d); } ``` ########## poi/src/main/java/org/apache/poi/ss/util/ImageUtils.java: ########## @@ -317,6 +298,6 @@ private static int getDimFromCell(double imgSize, int startCell, int startD, int } } - return (int)Math.rint(targetSize); + return MathUtil.safeDoubleToInt(Math.rint(targetSize)); Review 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. ########## poi/src/main/java/org/apache/poi/sl/draw/DrawPaint.java: ########## @@ -94,7 +94,7 @@ public Color getColor() { return new Color(color.getRed(), color.getGreen(), color.getBlue()); } @Override - public int getAlpha() { return (int)Math.round(color.getAlpha()*100000./255.); } + public int getAlpha() { return Math.toIntExact(Math.round(color.getAlpha()*100000./255.)); } Review Comment: I think this would be a good candidate for the new MathUtil class: ```java 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. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
