有没有一种方法可以在Java中不使用Math.abs()方法来找到一个数的绝对值。
有没有一种方法可以在Java中不使用Math.abs()方法来找到一个数的绝对值。
如果你查看 Math.abs 的内部实现,你可能会找到最好的答案:
例如,对于浮点数:
/*
* Returns the absolute value of a {@code float} value.
* If the argument is not negative, the argument is returned.
* If the argument is negative, the negation of the argument is returned.
* Special cases:
* <ul><li>If the argument is positive zero or negative zero, the
* result is positive zero.
* <li>If the argument is infinite, the result is positive infinity.
* <li>If the argument is NaN, the result is NaN.</ul>
* In other words, the result is the same as the value of the expression:
* <p>{@code Float.intBitsToFloat(0x7fffffff & Float.floatToIntBits(a))}
*
* @param a the argument whose absolute value is to be determined
* @return the absolute value of the argument.
*/
public static float abs(float a) {
return (a <= 0.0F) ? 0.0F - a : a;
}
是的:
abs_number = (number < 0) ? -number : number;
Integer.MIN_VALUE
,因为它的绝对值无法表示为一个 int
)。Math.abs()
。abs()
方法的属性。 - NPEMath.abs()
。我回答中的代码对于Integer.MIN_VALUE
的行为与Math.abs(int)
完全相同。 - NPE像这样:
if (number < 0) {
number *= -1;
}
由于Java是一种静态类型语言,如果一个abs方法接受一个int参数,则应该返回一个int; 如果它接受一个float,则应该返回一个float; 对于Double,则应该返回一个Double。也许它可以始终返回double和Double等的装箱或拆箱类型。
因此,您需要为每种类型编写一个方法,但现在您面临一个新问题:对于byte、short、int、long,负值范围比正值范围大1。
那么这个方法应该返回什么呢?
byte abs (byte in) {
// @todo
}
byte abs (byte in) throws ArithmeticException {
if (in == Byte.MIN_VALUE) throw new ArithmeticException ("abs called on Byte.MIN_VALUE");
return (in < 0) ? (byte) -in : in;
}
“忽略MIN_VALUE的罕见情况”习惯不是一个选项。首先让代码能够工作 - 然后再使其更快。如果用户需要更快但有缺陷的解决方案,他应该自己编写。最简单的可能有效的解决方案是:简单,但不要过于简单。
由于代码不依赖于状态,因此该方法可以并且应该被设置为静态。这允许进行快速测试:
public static void main (String args []) {
System.out.println (abs(new Byte ( "7")));
System.out.println (abs(new Byte ("-7")));
System.out.println (abs((byte) 7));
System.out.println (abs((byte) -7));
System.out.println (abs(new Byte ( "127")));
try
{
System.out.println (abs(new Byte ("-128")));
}
catch (ArithmeticException ae)
{
System.out.println ("Integer: " + Math.abs (new Integer ("-128")));
}
System.out.println (abs((byte) 127));
System.out.println (abs((byte) -128));
}
我捕捉第一个异常并将其放入第二个异常中,只是为了演示。
编程中存在一种不好的习惯,即程序员更关注快速而不是正确的代码。真遗憾!
如果你好奇为什么负值比正值多一个,我有一个 图表供您参考。
(x + (x >> 31)) ^ (x >> 31);
然而,在 Integer.MIN_VALUE 的明显情况下,这种方法会失败,因此这是一种使用风险自负的解决方案。
如果要在Java中计算一个整数x的绝对值,且不使用Math.abs()、条件或位运算符,下面是可能的解决方案。
(int)(((long)x*x - 1)%(double)x + 1);
a%b
视为a-a/b * b
,所以结果的符号将与“a”相同,无论“b”的符号如何;(x*x-1)%x
将等于abs(x)-1
;对“long”进行类型转换是为了防止溢出,而double
允许除以零。x = Integer.MIN_VALUE
将导致溢出,因为会进行减1操作。您可以使用:
abs_num = (num < 0) ? -num : num;
abs_number = (num < 0) ? -num : num;
-num将在Integer.MIN_VALUE情况下等于num
Integer.MIN_VALUE = Integer.MIN_VALUE * -1
if (N < 0)
{
N = (-1) * N;
}
N现在将返回绝对值