Why don't I get positive values when trying to divide two integers (without using multiplication, div, mod, log)?

huangapple go评论140阅读模式
英文:

Why don't I get positive values when trying to divide two integers (without using multiplication, div, mod, log)?

问题

int divide() 方法中,Math.abs() 对上下边界值返回负值。即使我将其与 *-1 相乘,负数仍然不会转换为正值(div 显示为负数),但当我在 main() 方法中尝试使用 Math.abs() 时,相同的方法有效。

public class Mass {
    public static int divide(int dividend, int divisor) {
        int ans = 0;
        long val = Math.abs(divisor);
        long div = Math.abs(dividend);
        System.out.println(div);
        for (int i = 1; i <= div; i++) {
            val--;
            if (val == 0) {
                ans++;
                val = Math.abs(divisor);
            }
        }
        if ((dividend < 0 && divisor > 0) || (dividend > 0 && divisor < 0)) {
            ans = ans * (-1);
        }

        return ans;
    }

    public static void main(String[] args) {
        int a = divide(-2147483648, -2);

        System.out.println(a);
    }
}

为什么我在处理负数值时遇到问题?

英文:

In the int divide() method, Math.abs() is returning negative values for upper and lower boundary values. Even if I multiply it with *-1 the negative is not converting into positive value (div is showing a negative value), but the same methods works when I tried Math.abs() in main() method.

public class Mass {
    public static int divide(int dividend, int divisor) {
           int ans =0;
        long val  = Math.abs(divisor);
        long  div = Math.abs(dividend);
            System.out.println(div);
        for(int i =1;i&lt;=div;i++){
            val--;
            if(val ==0){
                ans++;
                val = Math.abs(divisor);
            }
        }
        if((dividend&lt;0 &amp;&amp; divisor &gt;0 ) || (dividend &gt;0 &amp;&amp; divisor&lt;0) ){
            ans = ans *(-1);
        }

        return ans;
    }

    public static void main(String[] args) {
       int a= divide(-2147483648, -2);
    
       System.out.println(a);
    }
}

Why do I have trouble with negative values?

答案1

得分: 2

在Java中,int有32位,所以最小值是-2147483648,最大值是2147483647。这意味着当你计算abs(min)时会发生溢出。

我认为实际上解决这个问题的最简单方法是使用负数,类似这样:

public int divide(int dividend, int divisor) {
    if (divisor == 0 || dividend == Integer.MIN_VALUE && divisor == -1) throw new IllegalArgumentException();
    if (dividend == 0) return 0;
    if (divisor == 1) return dividend; // dividend = MIN_VALUE时需要这个
    boolean negative = false;
    if (dividend > 0) dividend = -dividend; else negative = true;
    if (divisor > 0) divisor = -divisor; else negative = !negative;
    int result = 0;
    for (; dividend <= divisor; result++) dividend -= divisor;
    return negative ? -result : result;
}
英文:

ints in Java have 32 bits, so the min is -2147483648 and the max is 2147483647. This means you get overflow when you do abs(min).

I think actually the easiest way to fix this is just working with negative numbers, something like this:

public int divide(int dividend, int divisor) {
    if (divisor == 0 || dividend == Integer.MIN_VALUE &amp;&amp; divisor == -1) throw new IllegalArgumentException();
    if (dividend == 0) return 0;
    if (divisor == 1) return dividend; // required for dividend = MIN_VALUE
    boolean negative = false;
    if (dividend &gt; 0) dividend = -dividend; else negative = true;
    if (divisor &gt; 0) divisor = -divisor; else negative = !negative;
    int result = 0;
    for (; dividend &lt;= divisor; result++) dividend -= divisor;
    return negative ? -result : result;
}

答案2

得分: 1

//仅适用于整数

public class MathSkills {

    public static void main(String[] args) {
    	// TODO Auto-generated method stub
	    System.out.println(dividend(100, 4));

    }
	public static int dividend(int a, int b) {
	    int ans=0;
    	for(;a>=b;ans++) {
	    	a=a-b;
    	}
	
    	return ans;
	}

}
英文:
//works for integers only

public class MathSkills {

    public static void main(String[] args) {
    	// TODO Auto-generated method stub
	    System.out.println(dividend(100,4));

    }
	public static int dividend(int a, int b) {
	    int ans=0;
    	for(;a&gt;=b;ans++) {
	    	a=a-b;
    	}
	
    	return ans;
	}

}

huangapple
  • 本文由 发表于 2023年3月8日 16:18:18
  • 转载请务必保留本文链接:https://go.coder-hub.com/75670689.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定