Path: ...!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail From: Keith Thompson Newsgroups: comp.lang.c,comp.arch Subject: Re: Radians Or Degrees? Date: Sat, 16 Mar 2024 16:19:11 -0700 Organization: None to speak of Lines: 83 Message-ID: <87o7bd3guo.fsf@nosuchdomain.example.com> References: <20240222015920.00000260@yahoo.com> <20240222233838.0000572f@yahoo.com> <3b2e86cdb0ee8785b4405ab10871c5ca@www.novabbs.org> <936a852388e7e4414cb7e529da7095ea@www.novabbs.org> <20240314112655.000011f8@yahoo.com> <87wmq32o26.fsf@nosuchdomain.example.com> <8de6385435bbdb0695a7ff213653f345@www.novabbs.org> <20240316190815.000005a2@yahoo.com> MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit Injection-Info: dont-email.me; posting-host="bef1352e676cdb1e0584d8972e1644aa"; logging-data="3301628"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19ATSOK9UXIoXPtSBDrHyVv" User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/27.2 (gnu/linux) Cancel-Lock: sha1:isKbIbUiNK/xLdmkYxzOKsjOUNo= sha1:k3Q72f1Dd22mqW6v1UpoImaUtBM= Bytes: 4791 mitchalsup@aol.com (MitchAlsup1) writes: > Michael S wrote: >> On Sat, 16 Mar 2024 01:16:25 +0000 >> mitchalsup@aol.com (MitchAlsup1) wrote: >>> Keith Thompson wrote: >>> > I can see how computing sin(x) with high precision for >>> > "reasonable" values of x would be useful, but does any of that >>> > benefit from being able to compute sin(2^53) accurately? >>> Because accurate argument reduction reduces the burden on the >>> programmer to remain within his sandbox. > >> Not really. > > Say you are a programmer and you receive a value like 2^53 from an > Input read and you wan the most accurate possible SIN( of that ). I can't think of a scenario where that would be useful (other than just doing it for the sake of doing it). If 2^53 represents a physical quantity, how likely is the actual value to be known within ±π (+/i pi for those who prefer ASCII)? If you can get better precision without too much extra cost, that's great. I don't know enough to have an opinion about what the best tradeoff is, but I presume it's going to be different depending on the application. Here's a C program that shows how precise sin(2^53) can be for types float, double, and long double (I used gcc and glibc). The nextafter functions are used to compute the nearest representable number. For long double, the value of sin() changes by about 1 part in 1600, which seems decent, but it's not nearly as precise as for values around 1.0. For float and double, the imprecision of the argument is enough to make the result practically meaningless. #include #include #include #include int main(void) { { printf("float (%zu bits, %d mantissa bits)\n", CHAR_BIT * sizeof (float), FLT_MANT_DIG); const float x = (float)(1LL<<53); const float y = nextafterf(x, x*2); printf("%.8f %.8f\n", x, sinf(x)); printf("%.8f %.8f\n", y, sinf(y)); } putchar('\n'); { printf("double (%zu bits, %d mantissa bits)\n", CHAR_BIT * sizeof (double), DBL_MANT_DIG); const double x = (double)(1LL<<53); const double y = nextafter(x, x*2); printf("%.8f %.8f\n", x, sin(x)); printf("%.8f %.8f\n", y, sin(y)); } putchar('\n'); { printf("long double (%zu bits, %d mantissa bits)\n", CHAR_BIT * sizeof (long double), LDBL_MANT_DIG); const long double x = (long double)(1LL<<53); const long double y = nextafterl(x, x*2); printf("%.8Lf %.8Lf\n", x, sinl(x)); printf("%.8Lf %.8Lf\n", y, sinl(y)); } } Output: float (32 bits, 24 mantissa bits) 9007199254740992.00000000 -0.84892595 9007200328482816.00000000 -0.34159181 double (64 bits, 53 mantissa bits) 9007199254740992.00000000 -0.84892596 9007199254740994.00000000 -0.12729655 long double (128 bits, 64 mantissa bits) 9007199254740992.00000000 -0.84892596 9007199254740992.00097656 -0.84944168 -- Keith Thompson (The_Other_Keith) Keith.S.Thompson+u@gmail.com Working, but not speaking, for Medtronic void Void(void) { Void(); } /* The recursive call of the void */