I am writing a program to find the coordinates of points on a unit circle. Everything works fine except for a minor aesthetic issue. Here is the code and then I'll discuss the problem:
When x1 = 1.0 or -1.0, y2 = -0.0. Obviously this is the same thing as 0.0 mathematically, but it just doesn't look good in the output. An suggestions? I've tried using a if-else loop like this to remedy the problem:
This will fix the problem for the first zero but not the second one. It completely baffles me. Hope that's all clear. Any help would be greatly appreciated.
Code:
public class PointsOnACircleV1 { public static void main(String[] args) { double radius = 1.0; double y1 = 0.0; double y2 = 0.0; for(double x1 = 1.0; x1 >= -1.0; x1 -= .1) { y1 = Math.sqrt(Math.pow(radius,2) - Math.pow(x1, 2)); y2 = -y1; System.out.printf("%6.2f%8.2f%14.2f%8.2f%n", x1, y1, x1, y2); } } }
Code:
if(y1 != 0.00) { y2 = -y1; } else { y2 = y1; }
Comment