0

Well i am making this operations in a program in java

public class results{
    public static void main(String args[]){
        double x=Math.sqrt(1/8);
        double x2 = x*2;
        System.out.println(x);
        System.out.println(x2);
    }
}

The problem is the variable x. It prints 0.0 and variable x2 0.0 as well. The result of x is 0.353553. How to fix this in order to use the 0.353553 instead of the 0.0 in variable x.

Oneiros
  • 4,146
  • 6
  • 35
  • 66
Wolvy
  • 131
  • 2
  • 11

2 Answers2

4

1/8 is an integer division. The result is 0 so everything is zero. Try 1/8.0 or 0.125 instead.

Obenland
  • 774
  • 13
  • 24
0

You can solved this problem that 1d divided by 8d.

double x=Math.sqrt(1d/8d);
double x2 = x*2d;
Kushan
  • 10,118
  • 3
  • 33
  • 40