Negative Zero Help

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Xander314
    New Member
    • Dec 2007
    • 2

    Negative Zero Help

    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:

    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);
            }
        }
    }
    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:
    Code:
                if(y1 != 0.00)
                {
                    y2 = -y1;
                }
                else
                {
                    y2 = y1;
                }
    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.
  • vipersniper5
    New Member
    • Dec 2007
    • 9

    #2
    I'm pretty sure the problem here is that the number your working with is not actually 0, but rather a decimal >-.1 for example -.001, the negative comes in because it is a negative number, but with the format specifiers your ignoring those extra digits. for a nice easy fix I would suggest something like:
    if(y2>-.1&&y2<0)
    y2=0;
    before your print. There are other ways (rounding/truncation) but thats nice and simple and I think all thats needed for your program.
    Hope it helps

    Comment

    • Xander314
      New Member
      • Dec 2007
      • 2

      #3
      Originally posted by vipersniper5
      I'm pretty sure the problem here is that the number your working with is not actually 0, but rather a decimal >-.1 for example -.001, the negative comes in because it is a negative number, but with the format specifiers your ignoring those extra digits. for a nice easy fix I would suggest something like:
      if(y2>-.1&&y2<0)
      y2=0;
      before your print. There are other ways (rounding/truncation) but thats nice and simple and I think all thats needed for your program.
      Hope it helps
      I removed the code that truncates the decimals and the number actually is zero. Or at least it's 0.000000. And when I do the math on paper I get zero for the problem values. This just baffles me.

      Comment

      • JosAH
        Recognized Expert MVP
        • Mar 2007
        • 11453

        #4
        Here's something to read for both of you:

        What Every Computer Scientist Should Know About Floating-Point Arithmetic.

        kind regards,

        Jos

        Comment

        • BigDaddyLH
          Recognized Expert Top Contributor
          • Dec 2007
          • 1216

          #5
          Code:
          public class PointsOnACircleV1{
              public static void main(String[] args){
                  double radius = 1.0;
                  for(double x1 = 1.0; x1 >= -1.0; x1 -= .1) {
                      double y1 = Math.sqrt(radius*radius - x1*x1);
                      double y2 = -y1;
                      if (y2 == -0.0)
                          y2 = 0.0;
                      System.out.printf("%6.2f%8.2f%14.2f%20.15f%n", x1, y1, x1, y2);
                  }
              }
          }
          The first -0.00 is because there is such a thing as negative zero. This is not a "Java Thing" but part of how floating point numbers work. The second -0.00 is because of rounding error. As you can see when you run my version of the code, the last value isn't 0, it's -0.00000002107.. .

          Comment

          Working...