[英]Java/Pawn Maths formula returns 0
could someone explain me what am I doing wrong here, and how can I make this formula to be working? 有人可以解释一下我在这里做错了什么,如何使该公式起作用? right now it returns 0 in Java and Pawn.
现在,它在Java和Pawn中返回0。 But it works in PHP, which isnt making much sense for me.
但是它可以在PHP中工作,这对我来说意义不大。
int test = (65 / 65 / (1 + 25) * 10000);
In Java, integer division will truncate the results. 在Java中,整数除法将截断结果。 For example,
5/2
will truncate 2.5
to 2
. 例如,
5/2
将截断2.5
到2
。
To ensure you're using float
with numeric constants, add a .0
on the end, as in: 为了确保您使用带有数字常量的
float
,请在末尾添加.0
,如下所示:
int test = (65.0 / 65.0 / (1.0 + 25.0) * 10000.0);
Java operations for multiplication and division is left to right, so your expression is really: Java乘法和除法运算从左到右,因此您的表达式实际上是:
(((65 / 65) / 26) * 1000) // Clarifying parenthesis
((1 / 26) * 1000)
(0 * 1000) // integer division!
0
To avoid this, you just need to ensure the operations are casted to a double. 为了避免这种情况,您只需要确保将操作转换为两倍即可。
The simple way would be just changing the first value to a double, either by: 简单的方法是将第一个值更改为双精度值,方法是:
int test = (65D / 65 / (1 + 25) * 10000);
Or 要么
int test = (65.0 / 65 / (1 + 25) * 10000);
However, if you are refactoring your code later, you might want to change more than just the first value. 但是,如果稍后要重构代码,则可能要更改的内容不只是第一个值。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.