0

I have written a code in c which gives me rotation of point by angle given in the form of triples.
When I compile and run for test case it gives me output as -0,7 .
Where as the same code in python gives me output as 0,7 . When I run the same code on online compiling platforms it gives me correct output.
I am using codeblocks windows 10 os.
Is there something wrong with codeblocks?
What should i do?

C code:

#include<stdio.h>
#include<math.h>
int main()
{
    double xp,yp,xq,yq,a,b,c;
    double t,xn,yn;
    int z;
    scanf("%d",&z);
  //  printf("Enter coordinates of p \n");
    scanf("%lf%lf",&xp,&yp);
   // printf("\nEnter triple \n");
    scanf("%lf%lf%lf",&a,&b,&c);
   // printf("\nEnter coordinates of q \n");
    scanf("%lf%lf",&xq,&yq);
    t=asin(b/c);
    if(z==0)
    {
    xn=xp*cos(t)-yp*sin(t)-xq*cos(t)+yq*sin(t)+xq;
    yn=xp*sin(t)+yp*cos(t)-xq*sin(t)-yq*cos(t)+yq;
    }
    else
    {
    xn=xp*cos(t)+yp*sin(t)-xq*cos(t)-yq*sin(t)+xq;
    yn=-xp*sin(t)+yp*cos(t)+xq*sin(t)-yq*cos(t)+yq;
    }
    printf("%lf     %lf",xn,yn);
    return 0;
}

Output:

0
4 7
3 4 5
2 3
-0.000000     7.000000
Process returned 0 (0x0)   execution time : 10.675 s
Press any key to continue.

https://stackoverflow.com/questions/34088742/what-is-the-purpose-of-having-both-positive-and-negative-zero-0-also-written
Aniket Bote
  • 3,456
  • 3
  • 15
  • 33

2 Answers2

3

The most likely thing here is that you don't actually have a signed -0.0, but your formatting is presenting it to you that way.

You'll get a signed negative zero in floating point if one of your calculations yields a negative subnormal number that's rounded to zero.

If you do indeed have a pure signed zero, then one workaround is to clobber it with a the ternary conditional operator as printf does reserve the right to propagate the signed zero into the output: f == 0.0 ? 0.0 : f is one such scheme or even with the flashier but obfuscated f ? f : 0.0. The C standard defines -0.0 to be equal to 0.0. Another way (acknowledge @EricPostpischil) is to add 0.0 to the value.

Bathsheba
  • 231,907
  • 34
  • 361
  • 483
  • 1
    It does print a sign for `-0.0` (for example with `printf("%f", -0.0);`) – tmlen Apr 30 '18 at 10:47
  • `printf("%lf %lf",xn ? xn : 0.0,yn ? yn : 0.0);` – Bathsheba Apr 30 '18 at 11:01
  • I dont think I have actual signed 0 but my formatting is presenting me that way as your solution in comment didnt work. What should i do now. – Aniket Bote Apr 30 '18 at 11:05
  • I used online compiler it gave me correct output for the same code.So what exactly is wrong? – Aniket Bote Apr 30 '18 at 11:10
  • 3
    There is no need to use the ternary operator to change -0 to +0. Simply add 0. (One floating-point instruction instead of test, branch, and move/load.) But I suspect that is not the actual problem. More likely the result is a small negative value. – Eric Postpischil Apr 30 '18 at 11:26
  • Actually I am getting correct output when I am using online compiling platforms.But when i am compiling in my pc its giving me negative 0. – Aniket Bote Apr 30 '18 at 11:31
2

For floating point values there are two zeroes 0.0 and -0.0. They compare as equal (e.g. -0.0 == 0.0 returns 1) but they are two distinct values. They are there for symmetry, because for any small value other than 0, the sign does make a mathematical difference. For some edge cases they make a difference. For example 1.0/0.0 == INFINITY and 1.0/-0.0 == -INFINITY. (INFINITY, -INFINITY and NAN) are also values that the floating point variables can take.

To make printf not print -0 for -0.0 and any small that would be truncated to 0 or -0, one way is to artificially put very small values to 0.0, for example:

if(abs(x) < 1e-5) x = 0.0;
tmlen
  • 8,533
  • 5
  • 31
  • 84