Search code examples
cmath.h

sin() output of a double coming out as 0.00000. (C language)


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

int main(void)
{
    int i;
    double interval;
    
    for(i=0;i<30;i++) 
        {
        interval = i/10.0;
        printf("sin(%lf) = %lf \t", interval, abs(sin(interval)));
        };
    printf("\n+++++++++\n");
    return 0;
}

the sin value of each interval term is coming out to be 0.000000. example sin(0.60000) = 0.000000


Solution

  • The abs function is an integer function. Which means you have undefined behavior since you use the mismatching format specifier %lf.

    For floating point you need to use fabs.