1 /*
2 * Copyright (c) 1999, 2016, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation. Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
50 * more than one definition for a function (such as
51 * {@code acos}), use the "IEEE 754 core function" version
52 * (residing in a file whose name begins with the letter
53 * {@code e}). The methods which require {@code fdlibm}
54 * semantics are {@code sin}, {@code cos}, {@code tan},
55 * {@code asin}, {@code acos}, {@code atan},
56 * {@code exp}, {@code log}, {@code log10},
57 * {@code cbrt}, {@code atan2}, {@code pow},
58 * {@code sinh}, {@code cosh}, {@code tanh},
59 * {@code hypot}, {@code expm1}, and {@code log1p}.
60 *
61 * <p>
62 * The platform uses signed two's complement integer arithmetic with
63 * int and long primitive types. The developer should choose
64 * the primitive type to ensure that arithmetic operations consistently
65 * produce correct results, which in some cases means the operations
66 * will not overflow the range of values of the computation.
67 * The best practice is to choose the primitive type and algorithm to avoid
68 * overflow. In cases where the size is {@code int} or {@code long} and
69 * overflow errors need to be detected, the methods {@code addExact},
70 * {@code subtractExact}, {@code multiplyExact}, and {@code toIntExact}
71 * throw an {@code ArithmeticException} when the results overflow.
72 * For other arithmetic operations such as divide, absolute value,
73 * increment by one, decrement by one, and negation overflow occurs only with
74 * a specific minimum or maximum value and should be checked against
75 * the minimum or maximum as appropriate.
76 *
77 * @author unascribed
78 * @author Joseph D. Darcy
79 * @since 1.3
80 */
81
82 public final class StrictMath {
83
84 /**
85 * Don't let anyone instantiate this class.
86 */
87 private StrictMath() {}
88
89 /**
90 * The {@code double} value that is closer than any other to
91 * <i>e</i>, the base of the natural logarithms.
92 */
93 public static final double E = 2.7182818284590452354;
94
95 /**
818 public static long multiplyExact(long x, int y) {
819 return Math.multiplyExact(x, y);
820 }
821
822 /**
823 * Returns the product of the arguments,
824 * throwing an exception if the result overflows a {@code long}.
825 *
826 * @param x the first value
827 * @param y the second value
828 * @return the result
829 * @throws ArithmeticException if the result overflows a long
830 * @see Math#multiplyExact(long,long)
831 * @since 1.8
832 */
833 public static long multiplyExact(long x, long y) {
834 return Math.multiplyExact(x, y);
835 }
836
837 /**
838 * Returns the value of the {@code long} argument;
839 * throwing an exception if the value overflows an {@code int}.
840 *
841 * @param value the long value
842 * @return the argument as an int
843 * @throws ArithmeticException if the {@code argument} overflows an int
844 * @see Math#toIntExact(long)
845 * @since 1.8
846 */
847 public static int toIntExact(long value) {
848 return Math.toIntExact(value);
849 }
850
851 /**
852 * Returns the exact mathematical product of the arguments.
853 *
854 * @param x the first value
855 * @param y the second value
856 * @return the result
857 * @see Math#multiplyFull(int,int)
|
1 /*
2 * Copyright (c) 1999, 2019, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation. Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
50 * more than one definition for a function (such as
51 * {@code acos}), use the "IEEE 754 core function" version
52 * (residing in a file whose name begins with the letter
53 * {@code e}). The methods which require {@code fdlibm}
54 * semantics are {@code sin}, {@code cos}, {@code tan},
55 * {@code asin}, {@code acos}, {@code atan},
56 * {@code exp}, {@code log}, {@code log10},
57 * {@code cbrt}, {@code atan2}, {@code pow},
58 * {@code sinh}, {@code cosh}, {@code tanh},
59 * {@code hypot}, {@code expm1}, and {@code log1p}.
60 *
61 * <p>
62 * The platform uses signed two's complement integer arithmetic with
63 * int and long primitive types. The developer should choose
64 * the primitive type to ensure that arithmetic operations consistently
65 * produce correct results, which in some cases means the operations
66 * will not overflow the range of values of the computation.
67 * The best practice is to choose the primitive type and algorithm to avoid
68 * overflow. In cases where the size is {@code int} or {@code long} and
69 * overflow errors need to be detected, the methods {@code addExact},
70 * {@code subtractExact}, {@code multiplyExact}, {@code toIntExact},
71 * {@code incrementExact}, {@code decrementExact} and {@code negateExact}
72 * throw an {@code ArithmeticException} when the results overflow.
73 * For the arithmetic operations divide and absolute value, overflow
74 * occurs only with a specific minimum or maximum value and
75 * should be checked against the minimum or maximum as appropriate.
76 *
77 * @author unascribed
78 * @author Joseph D. Darcy
79 * @since 1.3
80 */
81
82 public final class StrictMath {
83
84 /**
85 * Don't let anyone instantiate this class.
86 */
87 private StrictMath() {}
88
89 /**
90 * The {@code double} value that is closer than any other to
91 * <i>e</i>, the base of the natural logarithms.
92 */
93 public static final double E = 2.7182818284590452354;
94
95 /**
818 public static long multiplyExact(long x, int y) {
819 return Math.multiplyExact(x, y);
820 }
821
822 /**
823 * Returns the product of the arguments,
824 * throwing an exception if the result overflows a {@code long}.
825 *
826 * @param x the first value
827 * @param y the second value
828 * @return the result
829 * @throws ArithmeticException if the result overflows a long
830 * @see Math#multiplyExact(long,long)
831 * @since 1.8
832 */
833 public static long multiplyExact(long x, long y) {
834 return Math.multiplyExact(x, y);
835 }
836
837 /**
838 * Returns the argument incremented by one, throwing an exception if the
839 * result overflows an {@code int}.
840 *
841 * @param a the value to increment
842 * @return the result
843 * @throws ArithmeticException if the result overflows an int
844 * @since 1.8
845 */
846 public static int incrementExact(int a) {
847 return Math.incrementExact(a);
848 }
849
850 /**
851 * Returns the argument incremented by one, throwing an exception if the
852 * result overflows a {@code long}.
853 *
854 * @param a the value to increment
855 * @return the result
856 * @throws ArithmeticException if the result overflows a long
857 * @since 1.8
858 */
859 public static long incrementExact(long a) {
860 return Math.incrementExact(a);
861 }
862
863 /**
864 * Returns the argument decremented by one, throwing an exception if the
865 * result overflows an {@code int}.
866 *
867 * @param a the value to decrement
868 * @return the result
869 * @throws ArithmeticException if the result overflows an int
870 * @since 1.8
871 */
872 public static int decrementExact(int a) {
873 return Math.decrementExact(a);
874 }
875
876 /**
877 * Returns the argument decremented by one, throwing an exception if the
878 * result overflows a {@code long}.
879 *
880 * @param a the value to decrement
881 * @return the result
882 * @throws ArithmeticException if the result overflows a long
883 * @since 1.8
884 */
885 public static long decrementExact(long a) {
886 return Math.decrementExact(a);
887 }
888
889 /**
890 * Returns the negation of the argument, throwing an exception if the
891 * result overflows an {@code int}.
892 *
893 * @param a the value to negate
894 * @return the result
895 * @throws ArithmeticException if the result overflows an int
896 * @since 1.8
897 */
898 public static int negateExact(int a) {
899 return Math.negateExact(a);
900 }
901
902 /**
903 * Returns the negation of the argument, throwing an exception if the
904 * result overflows a {@code long}.
905 *
906 * @param a the value to negate
907 * @return the result
908 * @throws ArithmeticException if the result overflows a long
909 * @since 1.8
910 */
911 public static long negateExact(long a) {
912 return Math.negateExact(a);
913 }
914
915 /**
916 * Returns the value of the {@code long} argument;
917 * throwing an exception if the value overflows an {@code int}.
918 *
919 * @param value the long value
920 * @return the argument as an int
921 * @throws ArithmeticException if the {@code argument} overflows an int
922 * @see Math#toIntExact(long)
923 * @since 1.8
924 */
925 public static int toIntExact(long value) {
926 return Math.toIntExact(value);
927 }
928
929 /**
930 * Returns the exact mathematical product of the arguments.
931 *
932 * @param x the first value
933 * @param y the second value
934 * @return the result
935 * @see Math#multiplyFull(int,int)
|