Conversion of integer division to double [duplicate] - java

This question already has answers here:
Division of integers in Java [duplicate]
(7 answers)
Closed 4 years ago.
I want the help to produce the result of below calculation as 1.12 but the result is coming up 1.0
double k=(112)/100;
System.out.println(k);

You are doing Integer division causing it to lose the precision:
Replace
double k=(112)/100;
with
double k=(112.0)/100;

double k-=((double)112/100) worked for me as suggested by agni
when we do division like (112/100) JVM gives int as an o/p, you are storing that 'int' in 'double' so JVM adds '.0' to the o/p causing loss of precision. so, here you have to tell the JVM to give o/p without any loss. for that reason we have to mention like `double k = (double)112/100; which is similar to "typecasting".

Related

sqrt requires int but I'm already using int [duplicate]

This question already has answers here:
Java program error: possible loss of precision
(3 answers)
Closed 6 years ago.
int Asum,temp4;
temp4=Math.sqrt(Asum);
This is part of a code I'm writing. I've declared Asum and temp4 as integers. I'm trying to get the sqrt of Asum and assign it to temp4. but for some reason, java gives me the error : Possible loss of precision. reqd=int. found= double.
I need temp4 and Asum to be an integer. and it's not necessary to get the precise decimal values, I just need the rounded off integers.
Have you tried to add an explicit cast to int so that the compiler knows that you know what you are doing?
temp4= (int) Math.sqrt(Asum);
temp4 shouldn't be integer... it could be float.
I think you need to make use of the Math.round function.
Try:
temp4=Math.round(Math.sqrt(Asum));

Explanation of result for Double.valueOf [duplicate]

This question already has answers here:
Strange floating-point behaviour in a Java program [duplicate]
(4 answers)
Closed 8 years ago.
I want to know the reason why i am getting so many decimal points while doing this sum of double numbers
System.out.println(Double.parseDouble(".56"));
double dvalue=1.12+Double.parseDouble(".56");
System.out.println(dvalue);
output is as follows
0.56 and
1.6800000000000002
why second summation is adding those decimals it should simply 1.68
It's because the addition of the doubles 1.12 and 0.56 does not yield an exact representation of 1.68 (it doesn't precisely result in 1.68). You can see the difference with this code:
System.out.println(1.12+0.56); // 1.6800000000000002
System.out.println(1.68); // 1.68

Double data type not returning desired output in java [duplicate]

This question already has answers here:
Is floating point math broken?
(31 answers)
Closed 8 years ago.
In Java, for the following code
Double d = 2.0-1.1;
System.out.println(d);
The result is
0.8999999999999999
If the program is dealing with sensitive information such as precentile/money or cents how do I solve this problem?
I also tried the following piece of code:
new BigDecimal(d)
which outputs
0.899999999999999911182158029987476766109466552734375
What should I do to get 0.90 for the above case?
Since double cannot accurately represent the result of 2 - 1.11, the precision has already been lost by the time the constructor is used.Therefore you need to chain BigDecimal using the String based constructor
BigDecimal result = new BigDecimal("2").subtract(new BigDecimal("1.1"));
The Standard Reference is What Every Computer Scientist Should Know About Floating-Point Arithmetic
A more digestable read: Floating Point Numbers

Java "Errors" with math - int vs Double [duplicate]

This question already has answers here:
Why does division by zero with floating point (or double precision) numbers not throw java.lang.ArithmeticException: / by zero in Java
(6 answers)
Closed 8 years ago.
I noticed something weird earlier today. I was writing some code that was supposed to make graphs in complex quadrants. Anyway, I typed int i = 1/0; and it wouldn't compile. When I changed the code to double i = 1.0/0.0; the code compiled fine. When I ran the code it gave an error / by 0. I was expecting that... But why does it compile fine when using doubles and not integers? I am using the Blue J IDE
Dividing an int value by zero would result in a ArithmeticException, hence the expression 1 / 0 is illegal.
The result of dividing a double value by zero is infinity or NaN *, so the expression 1.0 / 0.0 is legal.
*) See t_over's comment for specifics:

Java does not print decimal places [duplicate]

This question already has answers here:
How to make the division of 2 ints produce a float instead of another int?
(9 answers)
Closed 9 years ago.
I'm trying to do a very basic operation as follows:
double a=21/5;
System.out.println(a);
However, each time I get 4.0 as the output and not 4.2. I'm encountering this for the first time. I've been using Java for years, but never came across this obscurity.
You are using integer division, which result will always be integer
You should use something like this.
double a=(double)21/5;
You are doing integer division...
Try:
double a = 21.0/5;
Cast the division or specify one of the arguments as a decimal to force the return as a double:
double a = (double)21/5;
-or-
double a = 21.0/5;
Just cast one of the numbers to double:
double a = 21/5.0;
Force the cast to double.
double a = 21.0/5
This is called Arithmetic promotion. This means that all terms in an equation are made equal to the variable type with the highest precision. In this case double.

Categories