Trouble with math in java [duplicate] - java

This question already has answers here:
Division of integers in Java [duplicate]
(7 answers)
Closed 8 years ago.
It's been a long day for me and I don't know if I can't do second grade math or if I'm doing something wrong in terms of how to do math in java. I'm not new to java, I started about a year and a half ago, but like I said, it's been a long day.
Here is my code:
System.out.println(5 / 150 * 100);
I expect to get something like "3.3333" or at least "3", but I get "0" instead. Why is that and how do I fix it?

Your second grade math is perfectly correct. However, 5 / 150 = 0.03 will become zero because its type is int. Then multiplying 0 with 100 won't change anything.
Use floats or doubles and you'll get the right result. Which of these two you use, depends on your needs. If you need a very precise value (a freaking lot of 3s behind the point) use double because it has - as its name tells you - two times the precision of a float.

All of your operand is int value and it will result in int value.
Try to change your operand to float value.
Try this:
System.out.println(5f / 150 * 100);
System.out.println(5 / 150f * 100);

Related

Floating point determinism - Java makes wrong output, C++ correct [duplicate]

This question already has answers here:
Is floating point math broken?
(31 answers)
Closed 6 years ago.
I'm facing a huge problem and I have no idea what's causing it and how to fix this. I'm creating a game where I draw objects in Java. For drawing I use linear interpolation which uses 'alpha' value to move the object with a specific time value in one frame.
Here is how the interpolation algorithm looks like:
draw.x = current.getX() * alpha + previous.getX() * (1.0f - alpha);
All of the above values are floats (alpha, current, prev and draw).
Let's assume, that we're not moving and our current.getX() and previous.getX() are 100.0f floats.
Alpha value is random (it's not random, but it's a number in 0.0 - 1.0 range) - also float.
Let's take alpha = 0,4352343
draw.x = 100.f * 0.4352343 + 100.f * (1.0f - 0.4352343) == 43,52343 + 56,47657 == 100
which is perfect, cause we're not moving - it should be 100 (our previous and current positions are both 100).
This is how math works, no matter what value alpha would be - it will always return 100, right?
NO!
In C++ it does - it works perfectly, I have tested it for 30 minutes with an output of 10 million cases - it always returns 100.0.
But in Java - it doesn't. For 80.000 cases there are ~400-500 cases where draw.x (return value from our function) becomes 99.99999999999.
It's completly unacceptable cause when I draw, I need to use int, so I have to cast, so it gives 99 value and you can guess what happens with the animation.
I have just one question - how is this possible, that above algorithm can give different values in Java and C++ and how to fix this in Java?
It has to do with rounding errors, although I'm uncertain why they come about. To fix it, you can use
right answer =Math.round(answer);

How to round off fractional part in java [duplicate]

This question already has answers here:
How to round a number to n decimal places in Java
(39 answers)
Closed 7 years ago.
I have to divide 495/116533, i tried using long and double; but it's returning 0.0;
Once if i able to capture that value (0.004247) then i can use round of methods;
Please help me how to capture complete value( which data type it supports?)
For 4 decimals do:
(double) (Math.round(value * 10000) / 10000)
The number of zeroes is how many decimals you want to round to.
Essentially what you need to do is to first multiply your number by a factor of X so that only the section you want to show is in front of the decimal place, then you can round it using the Math.round function. After that, just divide it back by X in order to put the decimal back in the right spot.

Solving A math equation using Java [duplicate]

This question already has answers here:
Why does division result in zero instead of a decimal?
(5 answers)
Closed 8 years ago.
I have hit a snag in my program when this calculates I get result of 0.0
y = 1/6*Math.pow(x,3)+1/2*Math.pow(x,2)-1/3*x;
I have tried writing the equation in chunks so I can add the results up after calculation but the result just keeps ending up being 0.0 and I don't know why. Is this a syntactical error or is there a rule that I'm missing about java?.
When you divide two integers Java truncates the result to an integer. If you want a fractional result you need to use floating point numbers. 1/2 is 0; 1.0/2.0 is 0.5.
y = 1.0/6.0*Math.pow(x,3) + 1.0/2.0*Math.pow(x,2) - 1.0/3.0*x;
y = 1/6*Math.pow(x,3)+1/2*Math.pow(x,2)-1/3*x;
Here you are doing division of two integers, which would result in 0. Make one/both of the values to decimal (1.0/6.0 etc) and then try this. It should give the correct result. The reason is that, 1/6 will be corrected to the closest integer value, which is 0.

Why does this Java division print out zero? [duplicate]

This question already has answers here:
Division of integers in Java [duplicate]
(7 answers)
Closed 9 years ago.
I have the following line of code:
System.out.println(5/9);
I expect to see 0.555 as a result, but instead it prints out zero. Can someone help me understand why this happens? I am currently learning programming and appreciate the help.
Thanks!
This happens because what you are unknowingly doing is Integer Division.
To make calculations fast, computer uses Integer division method when there's no decimal number involved, and hence decimal values are lost.
Try this out:
System.out.println(5.0 / 9.0);
or
System.out.println(5.0 / 9);
or
System.out.println(5 / 9.0);
or
System.out.println((float) 5 / 9);
or
System.out.println(5 / (float) 9);
You trying to divide 5 by 9.Both are integers.So the answer is also an integer.So return 0 as answer.So try like System.out.println(5.0/9); or assign values to float variables and go with that
Integer divide by Integer returns Integer
Do like this
System.out.println(5.0/9);
This is an integer division because both operands are of type integer. An integer division gives an integer result obtained by truncation.
When Integer division is ran in Java (Integer / Integer), the number is calculated then rounded down to the while number. Therefore, 5/9 is evaluated as 0.55555 then rounded down to 0.

division problems [duplicate]

This question already has answers here:
Closed 12 years ago.
Possible Duplicate:
Why does (360 / 24) / 60 = 0 … in Java
This line of code:
System.out.println ("array[j], "+array[j]+", divided by sum, "+sum+", equals: array[j]/sum: "+ array[j]/sum) ;
is yeilding this line of text:
array[j], 21, divided by sum, 100, equals: array[j]/sum: 0
why is it doing this? (everything is right eccept that the answer should be .21)
Are you sure that your array is not integer ?
if it's, try using double.
I'm assuming that aray is an int[] and sum is an int. In this case, Java will perform integer division, which results in 0 in this case.
Others noted the cause. To fix, (double) aray[j]/sum.
Dividing integers will get you an integer answer rounded down to the first whole number. If you want a decimal result, you have to make it 21.0/100.0.

Categories

Resources