Java 为什么浮点数除以整数返回 0.0?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3779604/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Why does dividing a float by an integer return 0.0?
提问by Arif Driessen
So if I have a range of numbers '0 - 1024' and I want to bring them into '0 - 255', the maths would dictate to divide the input by the maximum the input will be (1024 in this case) which will give me a number between 0.0 - 1.0. then multiply that by the destination range, (255).
因此,如果我有一个数字范围 '0 - 1024' 并且我想将它们变成 '0 - 255',那么数学将要求将输入除以输入的最大值(在这种情况下为 1024),这将给出我是一个介于 0.0 - 1.0 之间的数字。然后乘以目标范围(255)。
Which is what I want to do!
这就是我想要做的!
But for some reason in Java (using Processing) It will always return a value of 0.
但是由于某种原因,在 Java 中(使用 Processing)它总是会返回 0 值。
The code would be as simple as this
代码会像这样简单
float scale;
scale = (n/1024) * 255;
But I just get 0.0. I've tried double and int. all to no avail. WHY!?
但我只得到0.0。我试过 double 和 int。一切都无济于事。为什么!?
采纳答案by tim_yates
It's because you're doing integer division.
那是因为你在做整数除法。
Divide by a double or a float, and it will work:
除以双精度或浮点数,它将起作用:
double scale = ( n / 1024.0 ) * 255 ;
Or, if you want it as a float,
或者,如果你想要它作为一个浮点数,
float scale = ( n / 1024.0f ) * 255 ;
回答by Alexandre C.
n / 1024 is integer division, which yields an integer (ie. 0 in this case).
n / 1024 是整数除法,它产生一个整数(即在这种情况下为 0)。
Use n / 1024.0
instead.
使用n / 1024.0
来代替。
回答by Agos
You should auto-cast n to float by means of a multiplication FIRST, otherwise you're doing an integer operation and then casting the result, instead of doing the operation between floats.
您应该首先通过乘法自动将 n 转换为浮点数,否则您正在执行整数运算然后转换结果,而不是在浮点数之间进行运算。
float scale;
scale = n * 1.0 / 1024 * 255;
回答by John Kugelman
I presume n
is an int
. Because the constants 1024 and 255 are both int
s all the right-hand side calculations are being done with integer arithmetic. Meaning the result of n/1024
is being truncated to a integral value before being multiplied by 255
.
我认为n
是一个int
. 因为常量 1024 和 255 都是int
s,所以所有右侧的计算都是用整数算术完成的。这意味着 的结果n/1024
在乘以 之前被截断为整数值255
。
Any of these modifications will make the calculations work correctly:
任何这些修改都会使计算正常工作:
scale = n / 1024.0 * 255.0; // Use double constants.
scale = (double) n / 1024 * 255; // Convert n to a double.
scale = n * 255 / 1024; // Multiply before dividing.
The last one uses integer math still but switching the order of operations means you won't get the undesired truncation to 0. You'll still only get integer answers though, so you'll lose any decimal points in the answers.
最后一个仍然使用整数数学,但切换运算顺序意味着您不会将不需要的截断为 0。但是您仍然只能得到整数答案,因此您将丢失答案中的任何小数点。
回答by codaddict
In your case n/1024
results in 0 as you are doing integer division. To overcome this you can cast n
to float
. This will give you a result between 0.0
and 1.0
next you multiply with 255
and cast the result back to integer. Also you need to declare scale
as int
在您的情况下n/1024
,当您进行整数除法时,结果为 0。为了克服这一点,你可以投n
给float
。这将为您提供一个结果0.0
与1.0
下一次相乘255
并将结果转换回整数。您还需要声明scale
为int
int scale;
int n = 80;
scale = (int)(((float)n/1024) * 255);
回答by Martijn
others have given great answers already. In case you want your scale to be an integer (which makes sense if your n is an integer already), you could do
其他人已经给出了很好的答案。如果您希望您的比例是一个整数(如果您的 n 已经是一个整数,这很有意义),您可以这样做
int scale = ((255 * n)/1024);
Note that you won't hit any problems with this as long as these are the numbers, since n * 255 will always fit in an int when the maximum n = 1024.
请注意,只要这些是数字,您就不会遇到任何问题,因为当最大 n = 1024 时,n * 255 将始终适合 int。
more flexible would be
更灵活将是
int scale(int value, int old_max, int new_max){
java.math.BigInteger big_value = java.math.BigInteger.valueOf(value);
java.math.BigInteger big_old_max = java.math.BigInteger.valueOf(old_max);
java.math.BigInteger big_new_max = java.math.BigInteger.valueOf(new_max);
java.math.BigInteger mult = big_value.multiply(big_old_max);
return (int) mult.devide(big_new_max).doubleValue();
}
You won't overflow any ints this way, though I admit this is a bit verbose
你不会以这种方式溢出任何整数,尽管我承认这有点冗长
Edit:
编辑:
Basicly the same, but less clunky (though for very high numbers you might run into some precission errors)
基本相同,但不那么笨重(尽管对于非常高的数字,您可能会遇到一些精度错误)
int scale(int value, int old_max, int new_max){
double factor = (double) new_max / (double) old_max;
return factor * value;
}