ra*******@gmail.com wrote:
Currently using cosine function in math.h
Currently I get:
1 = cos(1e^-7)
Is there another way for cos to return value of high accuracy say:
0.999999 = cos(1e^-7)
That is a grossly inaccurate value. Higher precision is not high accuracy:
#include <stdio.h>
#include <math.h>
#include <float.h>
int main(void)
{
int i;
double x = 1.0e-7, y = cos(x);
printf("With default precisions, cos(%g) is shown as:\n"
" (%%f) %f, (%%g) %g, (%%e) %e\n\n", x, y, y, y);
printf("With various precision up to DBL_DIG (%d), cos(%g) is\n",
DBL_DIG, x);
for (i = 0; i <= DBL_DIG; i++)
printf(" precision = %d, %.*f, %.*g, %.*e\n",
i, i, y, i, y, i, y);
x = acos(0.999999);
y = cos(x);
printf("\nWith default precisions, cos(%g) is shown as:\n"
" (%%f) %f, (%%g) %g, (%%e) %e\n\n", x, y, y, y);
printf("With various precision up to DBL_DIG (%d), cos(%g) is\n",
DBL_DIG, x);
for (i = 0; i <= DBL_DIG; i++)
printf(" precision = %d, %.*f, %.*g, %.*e\n",
i, i, y, i, y, i, y);
return 0;
}
With default precisions, cos(1e-07) is shown as:
(%f) 1.000000, (%g) 1, (%e) 1.000000e+00
With various precision up to DBL_DIG (15), cos(1e-07) is
precision = 0, 1, 1, 1e+00
precision = 1, 1.0, 1, 1.0e+00
precision = 2, 1.00, 1, 1.00e+00
precision = 3, 1.000, 1, 1.000e+00
precision = 4, 1.0000, 1, 1.0000e+00
precision = 5, 1.00000, 1, 1.00000e+00
precision = 6, 1.000000, 1, 1.000000e+00
precision = 7, 1.0000000, 1, 1.0000000e+00
precision = 8, 1.00000000, 1, 1.00000000e+00
precision = 9, 1.000000000, 1, 1.000000000e+00
precision = 10, 1.0000000000, 1, 1.0000000000e+00
precision = 11, 1.00000000000, 1, 1.00000000000e+00
precision = 12, 1.000000000000, 1, 1.000000000000e+00
precision = 13, 1.0000000000000, 1, 1.0000000000000e+00
precision = 14, 1.00000000000000, 1, 9.99999999999995e-01
precision = 15, 0.999999999999995, 0.999999999999995,
9.999999999999950e-01
With default precisions, cos(0.00141421) is shown as:
(%f) 0.999999, (%g) 0.999999, (%e) 9.999990e-01
With various precision up to DBL_DIG (15), cos(0.00141421) is
precision = 0, 1, 1, 1e+00
precision = 1, 1.0, 1, 1.0e+00
precision = 2, 1.00, 1, 1.00e+00
precision = 3, 1.000, 1, 1.000e+00
precision = 4, 1.0000, 1, 1.0000e+00
precision = 5, 1.00000, 1, 9.99999e-01
precision = 6, 0.999999, 0.999999, 9.999990e-01
precision = 7, 0.9999990, 0.999999, 9.9999900e-01
precision = 8, 0.99999900, 0.999999, 9.99999000e-01
precision = 9, 0.999999000, 0.999999, 9.999990000e-01
precision = 10, 0.9999990000, 0.999999, 9.9999900000e-01
precision = 11, 0.99999900000, 0.999999, 9.99999000000e-01
precision = 12, 0.999999000000, 0.999999, 9.999990000000e-01
precision = 13, 0.9999990000000, 0.999999, 9.9999900000000e-01
precision = 14, 0.99999900000000, 0.999999, 9.99999000000000e-01
precision = 15, 0.999999000000000, 0.999999, 9.999990000000000e-01