648
703
return (rads * 180) / PI;
708
* Take a cube root. If the argument is <code>NaN</code>, an infinity or
709
* zero, then the original value is returned. The returned result is
710
* within 1 ulp of the exact result. For a finite value, <code>x</code>,
711
* the cube root of <code>-x</code> is equal to the negation of the cube root
715
* For a square root, use <code>sqrt</code>. For other roots, use
716
* <code>pow(a, 1 / rootNumber)</code>.
719
* @param a the numeric argument
720
* @return the cube root of the argument
722
* @see #pow(double, double)
725
public static double cbrt(double a)
727
return VMMath.cbrt(a);
732
* Returns the hyperbolic cosine of the given value. For a value,
733
* <code>x</code>, the hyperbolic cosine is <code>(e<sup>x</sup> +
734
* e<sup>-x</sup>)/2</code>
735
* with <code>e</code> being <a href="#E">Euler's number</a>. The returned
736
* result is within 2.5 ulps of the exact result.
739
* If the supplied value is <code>NaN</code>, then the original value is
740
* returned. For either infinity, positive infinity is returned.
741
* The hyperbolic cosine of zero is 1.0.
744
* @param a the numeric argument
745
* @return the hyperbolic cosine of <code>a</code>.
748
public static double cosh(double a)
750
return VMMath.cosh(a);
755
* Returns <code>e<sup>a</sup> - 1. For values close to 0, the
756
* result of <code>expm1(a) + 1</code> tend to be much closer to the
757
* exact result than simply <code>exp(x)</code>. The result is within
758
* 1 ulp of the exact result, and results are semi-monotonic. For finite
759
* inputs, the returned value is greater than or equal to -1.0. Once
760
* a result enters within half a ulp of this limit, the limit is returned.
763
* For <code>NaN</code>, positive infinity and zero, the original value
764
* is returned. Negative infinity returns a result of -1.0 (the limit).
767
* @param a the numeric argument
768
* @return <code>e<sup>a</sup> - 1</code>
771
public static double expm1(double a)
773
return VMMath.expm1(a);
778
* Returns the hypotenuse, <code>a<sup>2</sup> + b<sup>2</sup></code>,
779
* without intermediate overflow or underflow. The returned result is
780
* within 1 ulp of the exact result. If one parameter is held constant,
781
* then the result in the other parameter is semi-monotonic.
784
* If either of the arguments is an infinity, then the returned result
785
* is positive infinity. Otherwise, if either argument is <code>NaN</code>,
786
* then <code>NaN</code> is returned.
789
* @param a the first parameter.
790
* @param b the second parameter.
791
* @return the hypotenuse matching the supplied parameters.
794
public static double hypot(double a, double b)
796
return VMMath.hypot(a,b);
801
* Returns the base 10 logarithm of the supplied value. The returned
802
* result is within 1 ulp of the exact result, and the results are
806
* Arguments of either <code>NaN</code> or less than zero return
807
* <code>NaN</code>. An argument of positive infinity returns positive
808
* infinity. Negative infinity is returned if either positive or negative
809
* zero is supplied. Where the argument is the result of
810
* <code>10<sup>n</sup</code>, then <code>n</code> is returned.
813
* @param a the numeric argument.
814
* @return the base 10 logarithm of <code>a</code>.
817
public static double log10(double a)
819
return VMMath.log10(a);
824
* Returns the natural logarithm resulting from the sum of the argument,
825
* <code>a</code> and 1. For values close to 0, the
826
* result of <code>log1p(a)</code> tend to be much closer to the
827
* exact result than simply <code>log(1.0+a)</code>. The returned
828
* result is within 1 ulp of the exact result, and the results are
832
* Arguments of either <code>NaN</code> or less than -1 return
833
* <code>NaN</code>. An argument of positive infinity or zero
834
* returns the original argument. Negative infinity is returned from an
838
* @param a the numeric argument.
839
* @return the natural logarithm of <code>a</code> + 1.
842
public static double log1p(double a)
844
return VMMath.log1p(a);
849
* Returns the sign of the argument as follows:
852
* <li>If <code>a</code> is greater than zero, the result is 1.0.</li>
853
* <li>If <code>a</code> is less than zero, the result is -1.0.</li>
854
* <li>If <code>a</code> is <code>NaN</code>, the result is <code>NaN</code>.
855
* <li>If <code>a</code> is positive or negative zero, the result is the
859
* @param a the numeric argument.
860
* @return the sign of the argument.
863
public static double signum(double a)
876
* Returns the sign of the argument as follows:
879
* <li>If <code>a</code> is greater than zero, the result is 1.0f.</li>
880
* <li>If <code>a</code> is less than zero, the result is -1.0f.</li>
881
* <li>If <code>a</code> is <code>NaN</code>, the result is <code>NaN</code>.
882
* <li>If <code>a</code> is positive or negative zero, the result is the
886
* @param a the numeric argument.
887
* @return the sign of the argument.
890
public static float signum(float a)
903
* Returns the hyperbolic sine of the given value. For a value,
904
* <code>x</code>, the hyperbolic sine is <code>(e<sup>x</sup> -
905
* e<sup>-x</sup>)/2</code>
906
* with <code>e</code> being <a href="#E">Euler's number</a>. The returned
907
* result is within 2.5 ulps of the exact result.
910
* If the supplied value is <code>NaN</code>, an infinity or a zero, then the
911
* original value is returned.
914
* @param a the numeric argument
915
* @return the hyperbolic sine of <code>a</code>.
918
public static double sinh(double a)
920
return VMMath.sinh(a);
925
* Returns the hyperbolic tangent of the given value. For a value,
926
* <code>x</code>, the hyperbolic tangent is <code>(e<sup>x</sup> -
927
* e<sup>-x</sup>)/(e<sup>x</sup> + e<sup>-x</sup>)</code>
928
* (i.e. <code>sinh(a)/cosh(a)</code>)
929
* with <code>e</code> being <a href="#E">Euler's number</a>. The returned
930
* result is within 2.5 ulps of the exact result. The absolute value
931
* of the exact result is always less than 1. Computed results are thus
932
* less than or equal to 1 for finite arguments, with results within
933
* half a ulp of either positive or negative 1 returning the appropriate
934
* limit value (i.e. as if the argument was an infinity).
937
* If the supplied value is <code>NaN</code> or zero, then the original
938
* value is returned. Positive infinity returns +1.0 and negative infinity
942
* @param a the numeric argument
943
* @return the hyperbolic tangent of <code>a</code>.
946
public static double tanh(double a)
948
return VMMath.tanh(a);
952
* Return the ulp for the given double argument. The ulp is the
953
* difference between the argument and the next larger double. Note
954
* that the sign of the double argument is ignored, that is,
955
* ulp(x) == ulp(-x). If the argument is a NaN, then NaN is returned.
956
* If the argument is an infinity, then +Inf is returned. If the
957
* argument is zero (either positive or negative), then
958
* {@link Double#MIN_VALUE} is returned.
959
* @param d the double whose ulp should be returned
960
* @return the difference between the argument and the next larger double
963
public static double ulp(double d)
967
if (Double.isInfinite(d))
968
return Double.POSITIVE_INFINITY;
969
// This handles both +0.0 and -0.0.
971
return Double.MIN_VALUE;
972
long bits = Double.doubleToLongBits(d);
973
final int mantissaBits = 52;
974
final int exponentBits = 11;
975
final long mantMask = (1L << mantissaBits) - 1;
976
long mantissa = bits & mantMask;
977
final long expMask = (1L << exponentBits) - 1;
978
long exponent = (bits >>> mantissaBits) & expMask;
980
// Denormal number, so the answer is easy.
983
long result = (exponent << mantissaBits) | 1L;
984
return Double.longBitsToDouble(result);
987
// Conceptually we want to have '1' as the mantissa. Then we would
988
// shift the mantissa over to make a normal number. If this underflows
989
// the exponent, we will make a denormal result.
990
long newExponent = exponent - mantissaBits;
996
newMantissa = 1L << -(newExponent - 1);
999
return Double.longBitsToDouble((newExponent << mantissaBits) | newMantissa);
1003
* Return the ulp for the given float argument. The ulp is the
1004
* difference between the argument and the next larger float. Note
1005
* that the sign of the float argument is ignored, that is,
1006
* ulp(x) == ulp(-x). If the argument is a NaN, then NaN is returned.
1007
* If the argument is an infinity, then +Inf is returned. If the
1008
* argument is zero (either positive or negative), then
1009
* {@link Float#MIN_VALUE} is returned.
1010
* @param f the float whose ulp should be returned
1011
* @return the difference between the argument and the next larger float
1014
public static float ulp(float f)
1018
if (Float.isInfinite(f))
1019
return Float.POSITIVE_INFINITY;
1020
// This handles both +0.0 and -0.0.
1022
return Float.MIN_VALUE;
1023
int bits = Float.floatToIntBits(f);
1024
final int mantissaBits = 23;
1025
final int exponentBits = 8;
1026
final int mantMask = (1 << mantissaBits) - 1;
1027
int mantissa = bits & mantMask;
1028
final int expMask = (1 << exponentBits) - 1;
1029
int exponent = (bits >>> mantissaBits) & expMask;
1031
// Denormal number, so the answer is easy.
1034
int result = (exponent << mantissaBits) | 1;
1035
return Float.intBitsToFloat(result);
1038
// Conceptually we want to have '1' as the mantissa. Then we would
1039
// shift the mantissa over to make a normal number. If this underflows
1040
// the exponent, we will make a denormal result.
1041
int newExponent = exponent - mantissaBits;
1043
if (newExponent > 0)
1047
newMantissa = 1 << -(newExponent - 1);
1050
return Float.intBitsToFloat((newExponent << mantissaBits) | newMantissa);