Big Error in Java or incongruency with simple operators - java

I'm beginning in Java, and could anyone explain me why Java gives me these answers?
I have a very simple class trying to learn how to round a number.
I want 2 decimals
so...
public static void main(String[] args) {
double pi1 = Math.PI;
System.out.println("pi1 = " + pi1);
double p2 ;
p2= Math.round(pi1*100)/100;
//p2= Math.round(pi1*100)
//p2=p2/100;
System.out.println("p2 = " + p2);
}
If I run this result is:
p2 = 3.0
Then I change
//p2= Math.round(pi1*100)/100;
p2 = Math.round(pi1*100);
p2 = p2/100;
Now, result is:
p2 = 3.14
as I wanted
Why with these differences? Why the first option doesn't give me 3.14
I think that I've made a correct code with 1st option.
Please, anyone could tell me why?
These things makes me don't trust Java.
Thank you.

I will assume that you know how integer division works in Java. In short, when both sides of / are integral types, like in 314 / 100, the expression evaluates to an integer too, like 3.
Math.round returns a long, which is an integral type. In your first code, you have the expression Math.round(pi1*100)/100. Math.round(...) returns an integer type, 100 is an integer literal, so integer division occurs.
However, in the second code, you first assigned the result of Math.round to p2. The long returned is implicitly converted to a double first, and stored in p2. You then wrote an expression in p2: p2/100. Here, one of the operands is double, so integer division does not occur.
Therefore, the one liner version that is the same as the second code is:
p2 = ((double)Math.round(pi1*100))/100;
You don't see the double conversion in the second code because it is done implicitly.
A note on rounding
This way rounding doubles should be used if you want to do calculations with the rounded number afterwards. If you just want to display a rounded number as output, you should use String.format, System.out.printf, or DecimalFormat. Read more about these methods here.

No mistake here, it works properly. Method Math.round(double) returns type long.
In your first variant, you receive result long 314 and divide it by 100 and get the result 3, and then assign it back to double.
In your second variant, you receive long result 314 and assign it back to double. Dividing double keeps precision as opposed to dividing long, so you get the correct result of 3.14
There for make p2= Math.round(pi1*100)/100; as
p2= Math.round(pi1*100) / 100.0;
Output -:
pi1 = 3.141592653589793
p2 = 3.14

Related

How to divide a figure and convert it to decimal but the remainder

I want to be able to divide 9245 by 1000 and get 9.245, and then push this number to my textview.
my code gives me 9.0 and not the remainder. Hope do I keep the remainder?
String playerScore1 = gameInfo.getGame_time() / 1000;
playerscore1.setText(Double.toString(playerScore1));
If getGame_time() method does not return a double/float, you need to explicitly add .0 to dividend, or cast it to double/float type.
gameInfo.getGame_time() / 1000.0;
Java by default performs integer division, which truncates the decimal part of result. To force floating point division you can also use suffix notation:
int/1000f
int/1000d
use 1000.0 then it'll convert it into Double, if you divide it by 1000 it will automatically cast it into an integer. and use data type Double for playerScore1 as it will return Double not String
Double playerScore1 = gameInfo.getGame_time() / 1000.0;
playerscore1.setText(Double.toString(playerScore1));
When using arithmetic operators, Java will always widen to the widest type used. One exception to this rules are arithmetic operations on types byte, char, and short, they are always converted to int. This is the reason why
byte b = 1;
b = b + 1;
will fail to compile due to type mismatches. But funnily enough
byte b = 1;
b += 1;
will compile and work as expected.
Back to topic: If you divide two ints, you get an int as result. If you divide an int and a float, you will get a float, ... Thus, the easiest way to make your expression behave as you want it to is to make one of the values either a float or a double.
You can denote a float literal by appending an f or F to a floating point number.
For double, you can:
just write a floating point number, they will default to double
add a ´.0´ to an integer literal to make it a double literal
append a d or D on a floating point number, to make it extra verbose that it is a double
Applying this to your code, one possible solution is:
double playerScore1 = gameInfo.getGame_time() / 1000.0;

Why use double instead of integer?

First things first: I come from php and want to widen my horizon with java!
So I read some books. I saw that rounding a number is posible wit Math.round().
My question is as follows:
If I want to round a number as a decimal, I have to use this code:
double number;
number = 1.111222;
number = Math.round(number*100.0) / 100.0;
or
number = (digit) Math.round(number*100) / 100;
But why does
number = Math.round(number*100) / 100;
doesn't do the exact same thing???
Thx in advance,
Marc
If assuming that you mean to put a decimal point for that comma 1.111222
The problem is that 100 will cast the value to a long while 100.0 will cast it to a double. long's cannot have decimals but double's can.
Look at both cases:
Case 1 produces a double:
Math.round(1.111222*100.0) => Math.round(111.222) => 111
111/100.0 => 1.11
Case 2 produces a int long:
(I orignally thought int but was proven wrong by the output, the reason being Math.round(double) returns a long found here)
Math.round(1.111222*100) => Math.round(111) => 111
111/100 => 1 //truncated due to being a long
You can see this by running this code:
public static void main (String[] args)
{
double number = 1.111222;
System.out.println(Math.round(number*100.0)/100.0);
System.out.println(Math.round(number*100)/100);
Object a = Math.round(number*100.0)/100.0;
Object b = Math.round(number*100)/100;
System.out.println(a.getClass().getName());
System.out.println(b.getClass().getName());
}
Which prints:
1.11
1
java.lang.Double
java.lang.Long
It's clear in javadoc.
Returns the closest {#code int} to the argument, with ties rounding up.
So round(float) returns int, and round(double) returns long.
Because Math.round returns a long.
So the result of rounding (of type long) is divided by an int. Before division JVM converts both to long.
See here for Java widening conversion rules.
When you call Math.round(), the result is an integer value. When you divide two integers (e.g. / 100), Java will perform an integer division, throwing away the fractional part of the result.
When you divide an integer by a double (e.g. / 100.0), Java will first convert the integer to a double value, and then perform a floating point division, retaining the fractional part.

Why am I getting this precision error?

This is the code i am using to calculate a percentage and round it to 2 decimal places. However, at the moment, the result comes out as 45.0% rather than 45.33%
int one = 432;
int rolls = 953;
double test1 = 100 * one / rolls;
double finalValue1 = Math.round( test1 * 100.0 ) / 100.0;
Why are no decimal places showing?
as you are multiplying integers the result of test1 is integer
so you have to say
double test1= 100.0*one/rolls; or
double test1=(double)100*one/rolls
You could use String.format("%.2f",test1). If you use round, then java would round the the integer value. Thus, formatting it this way would give you the answer you seek.
The problem is with the following line:
double test1 = 100 * one / rolls;
That is because 100, one, and rolls are of type int.
When you do 100 * one, that will result in 43200. Then, we have to execute the rest of the calculation... 43200 / 953 would actually equal 45.330535152, but because these are both of type int the result will be 45. Making test1 = 45. Since test one is a double, it will actually be 45.0.
The next calculation, which uses test1 will be off because of the above, resulting in "no decimal value".
To fix this, you can change the type of one and rolls to double.
You have two problems.
First and foremost, you are performing integer division
double test1 = 100 * one / rolls;
100, one and rolls are all int. This means the result is an integer, regardless of what you've declared the return type to be. This is covered in the SO question Why the result of 1/3=0 in java?:
the result variable as double just causes an implicit conversion to occur after division.
If you want doubles, use doubles:
double one = 432.0;
double rolls = 953.0;
After fixing that, your division of Math.round( test1 * 100.0 ) / 100.0; will produce a double, but probably with more than two places of precision. It's unclear at that point if you want further rounding to a specific precision, but if you only wanted to print the two digits after the decimal you could use:
System.out.printf("%.2f", finalValue1);

different result in java how can it be rectified

Simple calculation gives different result in java.
int a=5363/12*5;
out.println(a);// result is 2230
But actually result should be 2234.5
How can this java result be rectified?
Two issues:
The expression 5363/12*5 gives an integer result (in particular, the division is integer).
The variable a is of type int (integer).
To fix:
double a=5363.0/12*5;
out.println(a);
Note that in general you can't expect to get exact results when using floating-point arithmetic. The following is a very good read: What Every Computer Scientist Should Know About Floating-Point Arithmetic.
5363, 12, and 5 are all being interpreted as ints. the calculation actually being performed here is:
5363/12 = 446.9… - truncated to the int value 446
446 * 5 = 2230
Try specifying a as a float, and indicate that the numbers in the calculation are also created as floats:
float a = 5363f/12f*5f
Take a as double.
Taking a as int will round it to the integer.
Because your all the literal numbers in the right hand side are integers (e.g. 5363 as opposed to 5363.0) expression is being calculated using integer arithmetic semantics i.e. / does whole number division. Thus 5262/12 equals 446 and 446*5 equals 2230. Also your variable a is an int which can only ever hold an integer value.
To fix this you need to do two things. Change the type of a to a decimal type e.g. float or double b) have at least one of 5363 and 12 represented as a decimal type e.g.
double a= 5363.0/12.0*5
Instead of using double you can re-order your expression.
Assuming 5363/12*5 = 5363*5/12 this will give you a closer answer. You have commented you want to round the result so instead you have to add half the value you are dividing by.
int a = (5363 * 5 + /* for rounding */ 6) / 12;
System.out.println(a);
prints
2235
An int is an Integer - nothing after the ..
You should be using
double a = 5363d/12*5;
It seems it has some int/double rounding issue:
double a=((double)5363/12)*5;
System.out.println("VALUE: "+a);
Prints:
VALUE: 2234.5833333333335
Edit: rounding the result to an integer value:
double a=((double)5363/12)*5;
long b=Math.round(a); //you can cast it to an int type if needed
System.out.println("ROUNDED: "+b);
Prints:
ROUNDED: 2235
Use double
double a = 5363/12*5;
System.out.println(a);
or
cast the integer, to prevent loss or precision.
int a = ((int) 5363/12*5);
System.out.println(a);

Understanding Casting in Java

this is my first post here and i like to thank all the people that can help me with this simple question: how the casting works in java?
I made this very simple class:
public class test {
public static void main ( String[] args )
{
System.out.println((short)(1/3));
System.out.println((int)(1/3));
System.out.println((float)(1/3));
System.out.println((double)(1/3));
}
}
and this piece of software gives me this output when executed ( official JDK 6 u26 on a 32 bit machine under linux )
0
0
0.0
0.0
the problem, or the thing that i don't understand if you would, is that the last 2 results are 0.0, i was expecting something like 0.3333333, but apparently the cast works in another way: how?
thanks
PS
I'm not so familiar with the english language, if i made some errors i apologize for this
First, the expression 1/3 is executed. This expression means "integer division of 1 by 3". And its result is the integer 0. Then this result (0) is cast to a double (or float). And of course it leads to 0.0.
You must cast one of the operands of the division to a double (or float) to transform it into a double division:
double d = ((double) 1) / 3;
or simply write
1.0 / 3
If you use the operator / on integer values, the result will be typed as an integer.
You will have to force either 1 or 3 to a floating point value - try writing 1.0/3 instead of 1/3.
In the last 2 cases you first compute 1/3, which is 0. Then you cast it to a float/double.
Use:
((float)1)/3
int is coverted into double.
(1/3) is of type int so answer will be 0.
Now 0 is casted to double or float so it has become 0.0
You need to cast the operands, not the result. This will yield the output you expected:
System.out.println(((short) 1 / (short) 3));
System.out.println((1 / 3));
System.out.println(((float) 1 / (float) 3));
System.out.println(((double) 1 / (double) 3));
Because you have the 1/3 in parentheses, it uses integer division then casts to the type specified. It would evaluate to .3333333 if you used 1.0/3.0 or (float)1/3. 1 and 3 are integer literals, so results from division will be truncated. To use float and double literals put an f or d at the end of the number (1.0f, 1.0d). Casting affects the value after it, so if your value is in parentheses, it will evaluate that, then make the cast.

Categories