getting -0.0 output ?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • pereges

    getting -0.0 output ?

    what is it and why does it occur in programs performing floating point
    calculations ? Is it harmful? if yes, then what are the possible
    solutions. Btw here was my input to the program:

    input
    *******
    Enter theta, phi, r
    0
    0
    0
    output
    ********
    -0.000000 0.000000 -1.000000

    #include <stdio.h>
    #include <math.h>
    #ifndef M_PI
    #define M_PI 3.14159
    #endif
    #define deg2radian(x) ((M_PI/180) * (x))

    int main(void)
    {
    double theta, phi;
    double thetahat[3], phihat[3];
    double r;
    double rhat[3];

    printf("Enter theta, phi, r\n");
    scanf("%lf %lf %lf", &theta, &phi, &r);

    theta = deg2radian(thet a);
    phi = deg2radian(phi) ;

    thetahat[0] = -sin(theta);
    thetahat[1] = cos(theta);
    thetahat[2] = 0;

    phihat[0] = cos(theta) * cos(phi);
    phihat[1] = sin(theta) * cos(phi);
    phihat[2] = -sin(phi);

    rhat[0] = thetahat[1] * phihat[2] - thetahat[2] * phihat[1];
    rhat[1] = thetahat[2] * phihat[0] - thetahat[0] * phihat[2];
    rhat[2] = thetahat[0] * phihat[1] - thetahat[1] * phihat[0];

    printf("%f %f %f\n ", rhat[0], rhat[1] , rhat[2]);
    return 0;
    }
  • Noob

    #2
    Re: getting -0.0 output ?

    pereges wrote:
    What is -0.0?
    The following document might be of interest.


    Comment

    • lawrence.jones@siemens.com

      #3
      Re: getting -0.0 output ?

      pereges <Broli00@gmail. comwrote:
      >
      what is it [-0.00000]?
      It's a very small negative number (e.g., -0.000000000001) .

      -- Larry Jones

      I kind of resent the manufacturer's implicit assumption
      that this would amuse me. -- Calvin

      Comment

      • Richard Tobin

        #4
        Re: getting -0.0 output ?

        In article <8jo5i5-0rc.ln1@jones.h omeip.net>,
        <lawrence.jones @siemens.comwro te:
        >what is it [-0.00000]?
        It's a feature of IEEE floating point, which is more-or-less universal
        now, but not required by the C standard.
        >It's a very small negative number (e.g., -0.000000000001) .
        This is sometimes true, in the same sense that +0.0000 is sometimes a
        very small positive number, because of finite precision. But positive
        and negative zero are also used to represent a zero that results from
        operations that can be considered limits approached from above or below.
        For example, 1/inf is 0, but 1/-inf is -0. Use of -0 allows functions
        with branch cuts in the complex plane to be more consistently defined.

        -- Richard
        --
        In the selection of the two characters immediately succeeding the numeral 9,
        consideration shall be given to their replacement by the graphics 10 and 11 to
        facilitate the adoption of the code in the sterling monetary area. (X3.4-1963)

        Comment

        • user923005

          #5
          Re: getting -0.0 output ?

          On Jun 11, 7:30 am, pereges <Brol...@gmail. comwrote:
          what is it and why does it occur in programs performing floating point
          calculations  ? Is it harmful? if yes, then what are the possible
          solutions. Btw here was my input to the program:
          >
          input
          *******
          Enter theta, phi, r
          0
          0
          0
          output
          ********
          -0.000000 0.000000 -1.000000
          >
          #include <stdio.h>
          #include <math.h>
          #ifndef M_PI
          #define M_PI 3.14159
          #endif
          #define deg2radian(x) ((M_PI/180) * (x))
          >
          int main(void)
          {
            double theta, phi;
            double thetahat[3], phihat[3];
            double r;
            double rhat[3];
          >
            printf("Enter theta, phi, r\n");
            scanf("%lf %lf %lf", &theta, &phi, &r);
          >
            theta = deg2radian(thet a);
            phi = deg2radian(phi) ;
          >
            thetahat[0] = -sin(theta);
            thetahat[1] = cos(theta);
            thetahat[2] = 0;
          >
            phihat[0] = cos(theta) * cos(phi);
            phihat[1] = sin(theta) * cos(phi);
            phihat[2] = -sin(phi);
          >
            rhat[0] = thetahat[1] * phihat[2] - thetahat[2] * phihat[1];
            rhat[1] = thetahat[2] * phihat[0] - thetahat[0] * phihat[2];
            rhat[2] = thetahat[0] * phihat[1] - thetahat[1] * phihat[0];
          >
            printf("%f %f %f\n ", rhat[0], rhat[1] , rhat[2]);
          >
            return 0;
          }
          #include <stdio.h>
          #include <math.h>
          #include <float.h>
          #ifndef M_PI
          #define M_PI 3.1415926535897 932384626433832 795028841971693 993751
          #endif
          #define deg2radian(x) ((M_PI/180) * (x))


          int main(void)
          {
          double theta,
          phi;
          double thetahat[3],
          phihat[3];
          double r;
          double rhat[3];
          int converted;

          oops:
          printf("Enter theta, phi, r\n");
          converted = scanf("%lf %lf %lf", &theta, &phi, &r);
          if (converted != 3) goto oops;

          theta = deg2radian(thet a);
          phi = deg2radian(phi) ;


          thetahat[0] = -sin(theta);
          thetahat[1] = cos(theta);
          thetahat[2] = 0;


          phihat[0] = cos(theta) * cos(phi);
          phihat[1] = sin(theta) * cos(phi);
          phihat[2] = -sin(phi);


          rhat[0] = thetahat[1] * phihat[2] - thetahat[2] * phihat[1];
          rhat[1] = thetahat[2] * phihat[0] - thetahat[0] * phihat[2];
          rhat[2] = thetahat[0] * phihat[1] - thetahat[1] * phihat[0];

          printf("%*.*g %*.*g %*.*g\n ",
          DBL_DIG + 3, DBL_DIG, rhat[0],
          DBL_DIG + 3, DBL_DIG, rhat[1],
          DBL_DIG + 3, DBL_DIG, rhat[2]);
          return 0;
          }


          Comment

          Working...