Velocity Reviews > cosine function returning long double (high resolution)

cosine function returning long double (high resolution)

ratcharit@gmail.com
Guest
Posts: n/a

 03-05-2008
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)

jacob navia
Guest
Posts: n/a

 03-05-2008
http://www.velocityreviews.com/forums/(E-Mail Removed) 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)
>

If you use the lcc-win compiler you get

cos(1e-7)=
0.999999999999995000000000000004166666666666665277 77777777777802579365079365076609347442680776222876 9173214
using the extra precision floating point (qfloat).

In standard C the biggest floating point type is long double.

No way to do that in standard C.

--
jacob navia
jacob at jacob point remcomp point fr
logiciels/informatique
http://www.cs.virginia.edu/~lcc-win32

jacob navia
Guest
Posts: n/a

 03-05-2008
jacob navia wrote:
> (E-Mail Removed) 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)
>>

>
> If you use the lcc-win compiler you get
>
> cos(1e-7)=
> 0.999999999999995000000000000004166666666666665277 77777777777802579365079365076609347442680776222876 9173214
>
> using the extra precision floating point (qfloat).
>
>
> In standard C the biggest floating point type is long double.
>
> No way to do that in standard C.
>

P.S.

WAIT

This is .00000000000000499999999999999, a number that is bigger than
DBL_EPSILON.

MAYBE your problem is a printing problem?

Which format did you use to print it?

--
jacob navia
jacob at jacob point remcomp point fr
logiciels/informatique
http://www.cs.virginia.edu/~lcc-win32

jacob navia
Guest
Posts: n/a

 03-05-2008
(E-Mail Removed) 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)
>

This program:
#include <math.h>
#include <stdio.h>
int main(void)
{
printf("%40.17Lf\n",cosl(1e-7));
}

prints
0.99999999999999500

--
jacob navia
jacob at jacob point remcomp point fr
logiciels/informatique
http://www.cs.virginia.edu/~lcc-win32

Richard Tobin
Guest
Posts: n/a

 03-05-2008
In article <(E-Mail Removed)>,
<(E-Mail Removed)> wrote:

>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)

cos(1e-7) is roughly 1 - 1e-14 / 2, which is closer to 1 than to
0.999999. It should be distinguishable from 1 as an IEEE double,
but not as a float.

-- Richard

--
:wq

jacob navia
Guest
Posts: n/a

 03-05-2008
Richard Tobin wrote:
>
> cos(1e-7) is roughly 1 - 1e-14 / 2, which is closer to 1 than to
> 0.999999. It should be distinguishable from 1 as an IEEE double,
> but not as a float.
>

And surely as a long double. I think the OP has a
printing precision problem, i.e. has written
printf("%g\n",cos(1e-7));

--
jacob navia
jacob at jacob point remcomp point fr
logiciels/informatique
http://www.cs.virginia.edu/~lcc-win32

Micah Cowan
Guest
Posts: n/a

 03-05-2008
jacob navia <(E-Mail Removed)> writes:

> jacob navia wrote:
>> (E-Mail Removed) 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)
>>>

>>
>> If you use the lcc-win compiler you get
>>
>> cos(1e-7)=
>> 0.999999999999995000000000000004166666666666665277 77777777777802579365079365076609347442680776222876 9173214
>>
>> using the extra precision floating point (qfloat).
>>
>>
>> In standard C the biggest floating point type is long double.
>>
>> No way to do that in standard C.
>>

>
> P.S.
>
> WAIT
>
> This is .00000000000000499999999999999, a number that is bigger than
> DBL_EPSILON.
>
> MAYBE your problem is a printing problem?
>
> Which format did you use to print it?

That's rather what I was thinking.

\$ cat foo.c
#include <stdio.h>

int main(void)
{
double foo = 0.999999999;
printf("foo = %f\n", foo);
printf("foo = %.9f\n", foo);
return 0;
}
\$ make foo
cc foo.c -o foo
\$ ./foo
foo = 1.000000
foo = 0.999999999

--
Micah J. Cowan
Programmer, musician, typesetting enthusiast, gamer...
http://micah.cowan.name/

Micah Cowan
Guest
Posts: n/a

 03-05-2008
jacob navia <(E-Mail Removed)> writes:

> Richard Tobin wrote:
>>
>> cos(1e-7) is roughly 1 - 1e-14 / 2, which is closer to 1 than to
>> 0.999999. It should be distinguishable from 1 as an IEEE double,
>> but not as a float.
>>

>
> And surely as a long double. I think the OP has a
> printing precision problem, i.e. has written
> printf("%g\n",cos(1e-7));

Note that the C standard only requires accuracy up to 10 decimal
digits (including for long double). That's why Richard took care to
qualify his statement as "IEEE double".

--
Micah J. Cowan
Programmer, musician, typesetting enthusiast, gamer...
http://micah.cowan.name/

Martin Ambuhl
Guest
Posts: n/a

 03-06-2008
(E-Mail Removed) 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

CBFalconer
Guest
Posts: n/a

 03-06-2008
(E-Mail Removed) 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)

Most math function packages evaluate cos (or sin) via fairly long
approximation formula, involving several multiplications and
additions, maybe divisions. If the designer is smart he designs
sin to:

double sin(double x) {
if (x < MAGICVALUE) return x;
else return sinfunction(x);
}

and cos(y) just returns sin(PI/2 - y). Suitable mods for quadrants
needed. Note that MAGICVALUE depends on the actual approximation
etc. used.

--
[mail]: Chuck F (cbfalconer at maineline dot net)
[page]: <http://cbfalconer.home.att.net>

--
Posted via a free Usenet account from http://www.teranews.com

 Posting Rules You may not post new threads You may not post replies You may not post attachments You may not edit your posts BB code is On Smilies are On [IMG] code is On HTML code is OffTrackbacks are On Pingbacks are On Refbacks are Off Forum Rules

 Similar Threads Thread Thread Starter Forum Replies Last Post veryhotsausage C++ 1 07-04-2008 05:41 PM FPGA VHDL 15 02-09-2008 02:21 PM Niv VHDL 9 08-16-2006 05:25 PM Daniel Rudy C Programming 5 09-20-2005 02:37 AM Sydex C++ 12 02-17-2005 06:30 PM