getting -0.0 output ?

P

pereges

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(theta);
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;
}
 
L

lawrence.jones

pereges said:
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
 
R

Richard Tobin

what is it [-0.00000]?
[/QUOTE]

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
 
U

user923005

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(theta);
  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.1415926535897932384626433832795028841971693993751
#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(theta);
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;
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,067
Latest member
HunterTere

Latest Threads

Top