Quantcast
Channel: Programming Forums
Viewing all articles
Browse latest Browse all 51036

Floating Point Precision/Comparison Queries

$
0
0
So here is the problem question that I was presented with:

Posted Image

The only non-standard code I used was the one to clear the screen. But I have a major question here. This is the first time I am writing anything that requires precise data for computation. Here goes the code:

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
double factorial(double);
int checkdiff(double, double);
int main()
{
    printf("5th Decimal Sin(x) Accuracy\n");
    int m, flag=0;
    double n, x=0.0, val=0.0, factVal, powVal, sinValue;
    printf("Please input a floating point number: ");
    scanf("%lf", &x);
    printf("Input Specified: %lf\n", x);
    sinValue=sin(x);
    printf("Sine Value = %lf\n\n", sinValue);
    
    /*
    sin(x)=x-x^3/3!+x^5/5!-x^7/7!+... (with x in radians)
    looping to forrm sin(x) equation of nth term= x^n/n!, +- alternately
    */

    for(n = 1.0, m = 2; ; n+=2.0, m++)
    {
        powVal=pow(x, n);
        factVal=factorial(n);
        if((m%2)==0)
        {
            val+=powVal/(factVal);
            printf("{\nFactorial Value is:%lf\nPower Value = %lf\nValue in +ve field = %lf\n}\n", factVal, powVal, val);
            flag = checkdiff(sinValue, val);
            if(flag == 1)
            {
                break;
            }
            else
            {
                continue;
            }

        }


        else
        {
            val-=powVal/(factVal);
            printf("{\nFactorial Value is:%lf\nPower Value = %lf\nValue in -ve field = %lf\n}\n", factVal, powVal, val);
            flag=checkdiff(sinValue, val);
            if(flag == 1)
            {
                break;
            }
            else
            {
                continue;
            }
        }
    }
    return 0;
}
double factorial(double n)
 {
 	int retval = 1, i;
 	for (i = n; i > 1; --i)
 		retval *= i;
 	return retval;
 }

int checkdiff(double sinValue, double val)
 {
     double newVal=sinValue-val;
     printf("Difference of Sine Value = %lf and value = %lf is %lf\n", sinValue, val, newVal);
     if((newVal>=-0.000009 && newVal <= -0.000001) || (newVal>=0.000001 && newVal <= 0.000009))
     {
      // system("cls"); -> Non-Standard Function, but overlooking SE/OS issues.
      printf("%.5lf is the required solution", val);
      return 1;
     } // we know sin(x) lies between -1 to 1

     return -1;
 }



I wrote the code trying to be as standard compliant (with all warnings from compiler turned on) gave no errors or warnings:

mingw32-gcc.exe -Wall  -g  -Wall    -c "C:\####\Code\C-Code\Fifth-Decimal-Sin(x)-Accuracy\main.c" -o obj\Debug\main.o
Process terminated with status 0 (0 minutes, 0 seconds)
0 errors, 0 warnings (0 minutes, 0 seconds)



Although my instructor said my results were acceptable for the values he needed it for (0.5, 0.6 etc), I am pretty sure that I am quite confused with floating point comparison. I know we cannot compare floating point 0.0 values so what I did was a range check. Is there any more precise method to achieve this in C? Also I am a bit confused with the (x.y)f terminology. How exactly does C round off/truncate the digits? Is the x the total number of digits and y the number of digits after the radix point? I really need a thorough understanding of the floating point arithmetic as handled in C.

Edit: I read something about using an epsilon to compare floats/doubles. What does that mean? Thanks.

Thank You very much much for any improvement suggestions, resource links etc.

Viewing all articles
Browse latest Browse all 51036

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>