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

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));

Related

Heavy number corruption in floating point division in Java? [duplicate]

This question already has answers here:
why does my output is wrong in exponents in java [duplicate]
(1 answer)
What does the ^ operator do in Java?
(19 answers)
Closed 2 years ago.
I have been trying to do something simple with java floating point division, have read several articles on some minimal changes on values but nothing like I am having below.
This is the expected:
float f = 1789773f / 1000000f;
System.out.printf("\n%.2f", f);
Outputs: 1,79
This is what happens when I work with variables:
int CLOCK=1789773;
System.out.printf("\n%.2f", (float)CLOCK/(10^6));
Outputs: 13410,48
What is going on? I had to use the literal '1000000f' instead of 10^6 to make it work. Also, I thought that casting the one of the division elements with (float) would set everything as float and I would not end doing integer math.
10^6 is not a million. It's 12, because ^ is the bitwise xor operator rather than exponentiation.
Use 1e6f, or just 1000000f.
Try using this instead :
int CLOCK = 1789773;
float fclock = (float) (CLOCK/(Math.pow(10,6)));
System.out.printf("\n%.2f", fclock);
float CLOCK=1789773f;
System.out.printf("\n%.2f", CLOCK/Math.pow(10,6));
OR
int CLOCK=1789773;
System.out.printf("\n%.2f",(float) (CLOCK/Math.pow(10,6)));
Try to use Math.pow(10,6), it's working fine for me.

Conversion of integer division to double [duplicate]

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".

java : multiplying 2 doubles [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 7 years ago.
double multiply()
{
double x=(2/3)*3.14*1.02;
System.out.print(x);
double y=0.666*3.14*1.02; /*(2/3)=0.666...*/
System.out.print(y);
}
Output:
x=0.0
y=SomeNumber
please explain this?
(2/3) is 0.
because both are integers. To solve this, use a cast or make it clear that your number is not an integer:
double x=(2/3d)*3.14*1.02;
Now you have an integer divided by a double, which results in a double.
Some more to read about this:
http://www.mathcs.emory.edu/~cheung/Courses/170/Syllabus/04/mixed.html
(2/3) is computed first (because of the parentheses), and in integer arithmetic (since the number literals are of type int). The fractional part is discarded.
It is therefore an int type with a value of 0. The entire expression is therefore zero.
The obvious remedy is to remove the parentheses and write 2.0 / 3.0 instead. Some folk prefer an explicit cast, but I find that ugly.
2/3 = 0 because they don't have explicit cast to double they are integers. The whole expression becomes: double x=0*3.14*1.02;
which is 0.
because data type of both 2 and 3 is int and int/int gives you int which in your case 2/3 is 0.
Try using 2.0/3 or 2/3.0 you will get the required answer.
Suffix every number with a 'd' to be sure you are dealing with doubles

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.

java double precision [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Precision of Floating Point
double a=0.000001,b=50000;
b=a*b;
System.out.println("double:"+b); // -> double:0.049999999999999996
float a=0.000001f,b=50000;
b=a*b;
System.out.println("float"+b); // -> float0.05
I have used double in most part of my code and today I found this problem.
How should I handle this?
Context:
double []refValues= {100,75,50,25,15,5,1};
double bInMilliGram=b*1000;
double pickedVal=0;
for(double ref:refValues)
if(ref<=bInMilliGram) {
pickedVal=ref;
break;
}
System.out.println("bInMilliGram:"+bInMilliGram+", pickedVal:"+pickedVal);
o/p: -> bInMilliGram:49.99999999999999, pickedVal:25.0
If you need arbitrarily good precision, use the java.math.BigDecimal class.
It is not a problem. It is how double works. You do not have to handle it and care about it. The precision of double is enough. Think, the difference between you number and the expected result is in the 19 position after decimal point.
The only conclusion from this fact is never try to compare floating point values using == - the results may be confusing.

Categories