Why Does the Compiler Print Negative Zero in This C Program?

  • Thread starter STEMucator
  • Start date
  • Tags
    Compiler
In summary: The concept of signed zeros is a mathematical concept, not a computing one.In summary, the conversation discusses a code written in C that calculates the values of sine and cosine using their Taylor expansions. After six terms, the approximation exceeds the computer's ability to represent it, resulting in a negative zero. The issue is resolved by changing the print format, and the conversation also delves into the concept of signed zeros and the importance of understanding the code rather than justifying one's statements.
  • #1
STEMucator
Homework Helper
2,076
140

Homework Statement



This is not really a homework problem, but something is just so far off about this I had to ask. Note this is written in C.

Homework Equations


The Attempt at a Solution



Here's some simple code that computes values of ##\sin(x)## and ##\cos(x)## using their Taylor expansions. After six terms, the approximation exceeds the computer's ability to represent it:

Code:
#include <stdio.h>
#include <stdlib.h>
#include <math.h>

long double sinxSeries(int, double);
long double cosxSeries(int, double);

int factorial(int);

int main(){
    printf("sin(pi/2) = %Lf \n",sinxSeries(6, M_PI/2));
    printf("cos(pi/2) = %Lf \n",cosxSeries(6, M_PI/2));
    return 0;
}

long double sinxSeries(int n, double x) {
    long double returnSum = 0;
    
    for(int i=0;i<n;i++)
        returnSum += pow(-1,i) * (pow(x, 2*i + 1))/(factorial(2*i + 1));

    return returnSum;
}

long double cosxSeries(int n, double x) {
    long double returnSum = 0;
    
    for(int i=0;i<n;i++)
        returnSum += pow(-1,i) * (pow(x, 2*i))/(factorial(2*i));
    
    return returnSum;
}

int factorial(int n) {
    return(n <= 1 ? 1 : n*factorial(n-1));
}

This code produces the following output (note one of the outputs is horrendous):

Code:
sin(pi/2) = 1.000000 
cos(pi/2) = -0.000000 
Program ended with exit code: 0

Negative zero?

When I change this line:

Code:
printf("cos(pi/2) = %Lf \n",cosxSeries(6, M_PI/2));

To this:

Code:
printf("cos(pi/2) = %Lf \n",cosxSeries(7, M_PI/2));

I get positive zero:

Code:
sin(pi/2) = 1.000000 
cos(pi/2) = 0.000000 
Program ended with exit code: 0

What happened here exactly?
 
Last edited:
Physics news on Phys.org
  • #2
I don't think you are getting exact positive and negative zeros here. Change the %Lf format to %Le, so you see what floating point values you are really calculating.

All calculation with float and double values are potentially inexact.
 
  • #3
AlephZero said:
I don't think you are getting exact positive and negative zeros here. Change the %Lf format to %Le, so you see what floating point values you are really calculating.

All calculation with float and double values are potentially inexact.

I found some justification: http://en.wikipedia.org/wiki/Signed_zero

The value will never be completely accurate due to truncation and round off.
 
  • #4
That integer factorial function is going to overflow as well. 13! doesn't fit in a 32 bit integer.

A more efficient way of calculating this is to compute the next term from the taylor polynomial from the previous.

You can more easily compute (-x^(n+2)) / (n+2)! from (x^n) / n! than from scratch.

Even better is http://mathworld.wolfram.com/HornersRule.html
 
  • #5
Zondrina said:
I found some justification: http://en.wikipedia.org/wiki/Signed_zero
I suggest you stop trying to justify your statement about signed zeros with irrelevant links to wikipedia, and change the print format as I suggested.

The basic rule of debugging any computer software is to find out what the code actually does, not what you think it ought to do.
 
  • #6
AlephZero said:
I suggest you stop trying to justify your statement about signed zeros with irrelevant links to wikipedia, and change the print format as I suggested.
The basic rule of debugging any computer software is to find out what the code actually does, not what you think it ought to do.
Yeah, i did that a long time ago. I already get it, like yesterday.

You don't seem to "get" my concern, but your hint to change the modifier was fine; even if it wasn't the point. It provided a visual, but not the answer. I was trying to understand the signed zero concept.

Thanks anyway.
 
  • #7
Zondrina said:
You don't seem to "get" my concern, but your hint to change the modifier was fine; even if it wasn't the point.
AlephZero's hint was exactly to the point. Much of what you wrote in your opening post is incorrect. AlephZero was trying to point you toward a better understanding of the code that you wrote.


I was trying to understand the signed zero concept.
Your code does not exhibit the signed zero concept. It exhibits what happens to a number that is non-zero that prints as zero because of the output option you chose to use.
 

FAQ: Why Does the Compiler Print Negative Zero in This C Program?

Why does the compiler give me an error message?

The compiler is designed to check your code for errors and ensure that it follows the correct syntax and rules. If the compiler encounters an error, it means that there is something wrong with your code and it cannot be executed properly. This helps to catch mistakes early on and improve the overall quality of your code.

Why does the compiler sometimes produce different results than expected?

This could be due to a variety of factors, such as incorrect syntax, unexpected input, or a bug in the compiler itself. It is important to carefully check your code and make sure it follows the correct logic and rules. If you believe there is an issue with the compiler, you can report it to the developers for further investigation.

Why does the compiler take a long time to compile my code?

The time it takes for a compiler to compile your code depends on various factors, such as the complexity of your code, the speed of your computer, and the type of compiler being used. Compiling can also take longer if there are errors or warnings that need to be addressed before the code can be successfully compiled.

Why does the compiler give me warnings?

Warnings are not as severe as errors and do not necessarily mean that your code will not be executed. However, they should still be taken seriously as they could indicate potential issues with your code that could cause problems in the future. It is important to address any warnings to improve the overall quality and reliability of your code.

Why does the compiler require specific libraries or dependencies?

Libraries and dependencies are often necessary for the compiler to properly execute your code. These are pre-written code that provide additional functionality and can save time and effort in writing your own code from scratch. The compiler needs these libraries to be able to understand and use the functions and features they provide.

Similar threads

Replies
8
Views
2K
Replies
12
Views
2K
Replies
12
Views
1K
Replies
2
Views
1K
Replies
4
Views
1K
Replies
3
Views
1K
Replies
2
Views
3K
Replies
3
Views
909
Back
Top