开发者

Weird behavior in Eclipse

I might sound incredible what I'm experiencing right now but I have this code along another operations.

double mues1 = 0;
mues1 = (Math.pow((ab/100),2)*tam);

Where ab=4, tam=400.

Thi开发者_开发技巧s should give .64, but the variable keeps being 0.0, and it only changes it's value if the operation is bigger than 1.

If I replace the math.pow with ^2, the variable becomes 800.0 no matter which value has 'ab'.

Just to make clear, I'm debugging the code so I know how the value is.

I have restarted eclipse and my computer and it didn't help.


Is ab and int? If so, you'll need to change ab/100 to (double)ab/100 or ab/100.0. Otherwise, it will perform integer division which truncate towards 0. In other words 4/100 = 0.


The problem is that the variable ab is an integer and you are performing integer division.

According to integer division, 4/100 = 0. To get the result 0.04, declare ab as a double.

FYI, the caret character in Java performs a bitwise xor, so you probably don't want to use that.


This is happening because you are dealing with int literals, the value of 4/100 = 0. You should try to do this using float literals 4.0/100.0.

When you try to do 4^100, you are doing a "bitwise exclusive or", as that is what ^ means in Java. This is not doing what you think it is, so do not do that.


You are suffering from float/double values being demoted to integer, so any 0.nn gets truncated to 0.

try this:

mues1 = (Math.pow(((double)ab/100),2)*(double)tam);

or just declare ab and tam as double

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜