summaryrefslogtreecommitdiff log msg author committer range
path: root/src/math/__cos.c
diff options
 context: 12345678910152025303540 space: includeignore mode: unifiedssdiffstat only
author committer Rich Felker 2012-03-13 01:17:53 -0400 Rich Felker 2012-03-13 01:17:53 -0400 b69f695acedd4ce2798ef9ea28d834ceccc789bd (patch) eafd98b9b75160210f3295ac074d699f863d958e /src/math/__cos.c d46cf2e14cc4df7cc75e77d7009fcb6df1f48a33 (diff) musl-b69f695acedd4ce2798ef9ea28d834ceccc789bd.tar.gz
first commit of the new libm!
thanks to the hard work of Szabolcs Nagy (nsz), identifying the best (from correctness and license standpoint) implementations from freebsd and openbsd and cleaning them up! musl should now fully support c99 float and long double math functions, and has near-complete complex math support. tgmath should also work (fully on gcc-compatible compilers, and mostly on any c99 compiler). based largely on commit 0376d44a890fea261506f1fc63833e7a686dca19 from nsz's libm git repo, with some additions (dummy versions of a few missing long double complex functions, etc.) by me. various cleanups still need to be made, including re-adding (if they're correct) some asm functions that were dropped.
Diffstat (limited to 'src/math/__cos.c')
-rw-r--r--src/math/__cos.c72
1 files changed, 72 insertions, 0 deletions
 diff --git a/src/math/__cos.c b/src/math/__cos.cnew file mode 100644index 00000000..ba439857--- /dev/null+++ b/src/math/__cos.c@@ -0,0 +1,72 @@+/* origin: FreeBSD /usr/src/lib/msun/src/k_cos.c */+/*+ * ====================================================+ * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.+ *+ * Developed at SunSoft, a Sun Microsystems, Inc. business.+ * Permission to use, copy, modify, and distribute this+ * software is freely granted, provided that this notice+ * is preserved.+ * ====================================================+ */+/*+ * __cos( x, y )+ * kernel cos function on [-pi/4, pi/4], pi/4 ~ 0.785398164+ * Input x is assumed to be bounded by ~pi/4 in magnitude.+ * Input y is the tail of x.+ *+ * Algorithm+ * 1. Since cos(-x) = cos(x), we need only to consider positive x.+ * 2. if x < 2^-27 (hx<0x3e400000 0), return 1 with inexact if x!=0.+ * 3. cos(x) is approximated by a polynomial of degree 14 on+ * [0,pi/4]+ * 4 14+ * cos(x) ~ 1 - x*x/2 + C1*x + ... + C6*x+ * where the remez error is+ *+ * | 2 4 6 8 10 12 14 | -58+ * |cos(x)-(1-.5*x +C1*x +C2*x +C3*x +C4*x +C5*x +C6*x )| <= 2+ * | |+ *+ * 4 6 8 10 12 14+ * 4. let r = C1*x +C2*x +C3*x +C4*x +C5*x +C6*x , then+ * cos(x) ~ 1 - x*x/2 + r+ * since cos(x+y) ~ cos(x) - sin(x)*y+ * ~ cos(x) - x*y,+ * a correction term is necessary in cos(x) and hence+ * cos(x+y) = 1 - (x*x/2 - (r - x*y))+ * For better accuracy, rearrange to+ * cos(x+y) ~ w + (tmp + (r-x*y))+ * where w = 1 - x*x/2 and tmp is a tiny correction term+ * (1 - x*x/2 == w + tmp exactly in infinite precision).+ * The exactness of w + tmp in infinite precision depends on w+ * and tmp having the same precision as x. If they have extra+ * precision due to compiler bugs, then the extra precision is+ * only good provided it is retained in all terms of the final+ * expression for cos(). Retention happens in all cases tested+ * under FreeBSD, so don't pessimize things by forcibly clipping+ * any extra precision in w.+ */++#include "libm.h"++static const double+one = 1.00000000000000000000e+00, /* 0x3FF00000, 0x00000000 */+C1 = 4.16666666666666019037e-02, /* 0x3FA55555, 0x5555554C */+C2 = -1.38888888888741095749e-03, /* 0xBF56C16C, 0x16C15177 */+C3 = 2.48015872894767294178e-05, /* 0x3EFA01A0, 0x19CB1590 */+C4 = -2.75573143513906633035e-07, /* 0xBE927E4F, 0x809C52AD */+C5 = 2.08757232129817482790e-09, /* 0x3E21EE9E, 0xBDB4B1C4 */+C6 = -1.13596475577881948265e-11; /* 0xBDA8FAE9, 0xBE8838D4 */++double __cos(double x, double y)+{+ double hz,z,r,w;++ z = x*x;+ w = z*z;+ r = z*(C1+z*(C2+z*C3)) + w*w*(C4+z*(C5+z*C6));+ hz = 0.5*z;+ w = one-hz;+ return w + (((one-w)-hz) + (z*r-x*y));+}
{"ft_lang_label":"__label__en","ft_lang_prob":0.61546975,"math_prob":0.97534394,"size":3979,"snap":"2021-43-2021-49","text_gpt3_token_len":1467,"char_repetition_ratio":0.12503144,"word_repetition_ratio":0.027108435,"special_character_ratio":0.47800955,"punctuation_ratio":0.147651,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.97492725,"pos_list":[0],"im_url_duplicate_count":[null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2021-12-02T09:49:22Z\",\"WARC-Record-ID\":\"<urn:uuid:fb21b748-40a3-46bb-b70a-6717f076f687>\",\"Content-Length\":\"12283\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:1286fc06-44f3-4acc-913d-436745892e46>\",\"WARC-Concurrent-To\":\"<urn:uuid:1b6975d8-7a94-4ce0-ba1a-d1093a0e7f16>\",\"WARC-IP-Address\":\"45.63.0.111\",\"WARC-Target-URI\":\"https://git.musl-libc.org/cgit/musl/commit/src/math/__cos.c?id=b69f695acedd4ce2798ef9ea28d834ceccc789bd\",\"WARC-Payload-Digest\":\"sha1:4EW2TN4L7X6UETZZSQ22LUOCRQC5J2XR\",\"WARC-Block-Digest\":\"sha1:UAE3PIKMALLWYVRNJ7QN6OTLE2LZ74OQ\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2021/CC-MAIN-2021-49/CC-MAIN-2021-49_segments_1637964361253.38_warc_CC-MAIN-20211202084644-20211202114644-00114.warc.gz\"}"}