This question already has answers here:
Int division: Why is the result of 1/3 == 0?
(19 answers)
Closed 5 years ago.
public class Test {
public static void main(String[] args) {
int test = 1;
System.out.println((double)(Math.pow(test/++test, 2)));
}
0.0 is printed to the screen. Why? Why is the cast not working as expected?
If test is declared this way...
double test = 1;
I get what I expect to print... 0.25.
Why?
I am new to programming and I'm playing around. Reading some of the documentation at this level is next to useless.
You are doing integer division. SO change the line to
System.out.println((Math.pow(test/(double)++test, 2)));
to do double division
Related
This question already has answers here:
Math.pow errors in Java
(5 answers)
Math.pow with Command line arguments
(4 answers)
error while using math.pow in java code
(2 answers)
Closed 3 years ago.
public class MyClass {
public static void main(String args[]) {
for (double i = 1; i < 10; i++)
{
double x=1;
System.out.print(Math.pow(x * 2));
x++;
}
}
}
I'm getting errors that say this:
/MyClass.java:7: error: method pow in class Math cannot be applied to given types;
System.out.print(Math.pow(x * 2));
^
required: double,double
found: double
reason: actual and formal argument lists differ in length
and I'm not sure why. I am somewhat new to coding so I'm not quite sure what to do here. Any help will be greatly appreciated.
Try Math.pow(x,2) rather than Math.pow(x*2). The latter is incorrect usage of the pow function. Alternatively, you could just print x*x
you would also want to do a System.out.println rather than a System.out.print, as the latter would print all the numbers one after another and you would not be able to differentiate between them.
your code is not compiling because you are calling the method wrongly...
see the doc:
https://docs.oracle.com/javase/7/docs/api/java/lang/Math.html#pow(double,%20double)
power is defined as
public static double pow(double a, double b)
so you miss 1 argument...
read carefully what a and b mean in the parameters
This question already has answers here:
Division operation is giving me the wrong result [duplicate]
(3 answers)
Int division: Why is the result of 1/3 == 0?
(19 answers)
Closed 5 years ago.
Here is my code, Eclipse always return integer value of results, even if h is double. Please help me to fix this.
public static void main(String[] args) {
double h=0.0;
for(int i=1;i<=1000;i++) {
h=h+ 1/i;
}
System.out.println("Harmonic sum "+h);
System.out.println("Harmonic sum "+String.format("%.4f", h));
Result:
Harmonic sum 1.0
Harmonic sum 1,0000
You need to cast the result of your division into double
Try this out:
h=h+ (double)1/i;
I hope this is what you were looking for.
This question already has answers here:
Simple division operation returning zero?
(5 answers)
Int division: Why is the result of 1/3 == 0?
(19 answers)
Closed 5 years ago.
Hey people of Stackoverflow! I just have a question about an error that I came across while doing this lesson on Java online. So this is the code:
import java.util.Scanner;
public class GradesAndPoints {
public static void main(String[] args) {
System.out.print("Type in your score between (0-27): ");
Scanner ask = new Scanner(System.in);
int num = ask.nextInt();
int result = (num/27);
System.out.println(result);
The error is: whenever I run the code with the variable "num" being any int value, it prints out to be 0. Can someone explain to me why this error occurs and a solution I can implement to solve this?
The way you're doing this, you're diving integers. This, by definition, will get you an integer that is truncated.
5 / 10 = 0
If you turn one of them into a float (by adding a . at the end), you will get floating point division, which is what you're looking for.
5. / 10 = 0.5
5 / 10. = 0.5
5.0 / 10.0 = 0.5
This question already has answers here:
Int division: Why is the result of 1/3 == 0?
(19 answers)
Closed 6 years ago.
Why does this program show 0.0?.
Below is my code:
class new1{
public static void main (String[]args){
int discount = 15;
float discount1 = 15/100;
System.out.println(discount1);
}
}
In line float discount1 = 15/100; is evaluated using the integer division.
If you want to get expected result, you should write like this
float discount1 = 15.0/100;
This question already has answers here:
Division of integers in Java [duplicate]
(7 answers)
Closed 6 years ago.
Every time I run this test program it display, 0.0 instead of 0.5
Does anyone know how to fix this in Eclipse?
public class Test {
public static void main(String[]args){
double distance;
distance = 1/2;
System.out.println(distance);
}
}
Your division is Integer division. There is no fault of Eclipse. Try following:
distance = 1/2.0;
OR
distance = 1.0/2;
Output:0.5