trig_test.c revision 176379
1176379Sdas/*- 2176379Sdas * Copyright (c) 2008 David Schultz <das@FreeBSD.org> 3176379Sdas * All rights reserved. 4176379Sdas * 5176379Sdas * Redistribution and use in source and binary forms, with or without 6176379Sdas * modification, are permitted provided that the following conditions 7176379Sdas * are met: 8176379Sdas * 1. Redistributions of source code must retain the above copyright 9176379Sdas * notice, this list of conditions and the following disclaimer. 10176379Sdas * 2. Redistributions in binary form must reproduce the above copyright 11176379Sdas * notice, this list of conditions and the following disclaimer in the 12176379Sdas * documentation and/or other materials provided with the distribution. 13176379Sdas * 14176379Sdas * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 15176379Sdas * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16176379Sdas * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17176379Sdas * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 18176379Sdas * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19176379Sdas * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20176379Sdas * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21176379Sdas * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22176379Sdas * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23176379Sdas * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24176379Sdas * SUCH DAMAGE. 25176379Sdas */ 26176379Sdas 27176379Sdas/* 28176379Sdas * Tests for corner cases in trigonometric functions. Some accuracy tests 29176379Sdas * are included as well, but these are very basic sanity checks, not 30176379Sdas * intended to be comprehensive. 31176379Sdas * 32176379Sdas * The program for generating representable numbers near multiples of pi is 33176379Sdas * available at http://www.cs.berkeley.edu/~wkahan/testpi/ . 34176379Sdas */ 35176379Sdas 36176379Sdas#include <sys/cdefs.h> 37176379Sdas__FBSDID("$FreeBSD: head/tools/regression/lib/msun/test-trig.c 176379 2008-02-18 02:00:16Z das $"); 38176379Sdas 39176379Sdas#include <assert.h> 40176379Sdas#include <fenv.h> 41176379Sdas#include <float.h> 42176379Sdas#include <math.h> 43176379Sdas#include <stdio.h> 44176379Sdas 45176379Sdas#define ALL_STD_EXCEPT (FE_DIVBYZERO | FE_INEXACT | FE_INVALID | \ 46176379Sdas FE_OVERFLOW | FE_UNDERFLOW) 47176379Sdas 48176379Sdas#define LEN(a) (sizeof(a) / sizeof((a)[0])) 49176379Sdas 50176379Sdas#pragma STDC FENV_ACCESS ON 51176379Sdas 52176379Sdas/* 53176379Sdas * Test that a function returns the correct value and sets the 54176379Sdas * exception flags correctly. The exceptmask specifies which 55176379Sdas * exceptions we should check. We need to be lenient for several 56176379Sdas * reasons, but mainly because on some architectures it's impossible 57176379Sdas * to raise FE_OVERFLOW without raising FE_INEXACT. 58176379Sdas * 59176379Sdas * These are macros instead of functions so that assert provides more 60176379Sdas * meaningful error messages. 61176379Sdas * 62176379Sdas * XXX The volatile here is to avoid gcc's bogus constant folding and work 63176379Sdas * around the lack of support for the FENV_ACCESS pragma. 64176379Sdas */ 65176379Sdas#define test(func, x, result, exceptmask, excepts) do { \ 66176379Sdas volatile long double _d = x; \ 67176379Sdas assert(feclearexcept(FE_ALL_EXCEPT) == 0); \ 68176379Sdas assert(fpequal((func)(_d), (result))); \ 69176379Sdas assert(((func), fetestexcept(exceptmask) == (excepts))); \ 70176379Sdas} while (0) 71176379Sdas 72176379Sdas#define testall(prefix, x, result, exceptmask, excepts) do { \ 73176379Sdas test(prefix, x, (double)result, exceptmask, excepts); \ 74176379Sdas test(prefix##f, x, (float)result, exceptmask, excepts); \ 75176379Sdas test(prefix##l, x, result, exceptmask, excepts); \ 76176379Sdas} while (0) 77176379Sdas 78176379Sdas/* 79176379Sdas * Determine whether x and y are equal, with two special rules: 80176379Sdas * +0.0 != -0.0 81176379Sdas * NaN == NaN 82176379Sdas */ 83176379Sdasint 84176379Sdasfpequal(long double x, long double y) 85176379Sdas{ 86176379Sdas return ((x == y && signbit(x) == signbit(y)) || isnan(x) && isnan(y)); 87176379Sdas} 88176379Sdas 89176379Sdas/* 90176379Sdas * Test special cases in sin(), cos(), and tan(). 91176379Sdas */ 92176379Sdasstatic void 93176379Sdasrun_special_tests(void) 94176379Sdas{ 95176379Sdas 96176379Sdas /* Values at 0 should be exact. */ 97176379Sdas testall(tan, 0.0, 0.0, ALL_STD_EXCEPT, 0); 98176379Sdas testall(tan, -0.0, -0.0, ALL_STD_EXCEPT, 0); 99176379Sdas testall(cos, 0.0, 1.0, ALL_STD_EXCEPT, 0); 100176379Sdas testall(cos, -0.0, 1.0, ALL_STD_EXCEPT, 0); 101176379Sdas testall(sin, 0.0, 0.0, ALL_STD_EXCEPT, 0); 102176379Sdas testall(sin, -0.0, -0.0, ALL_STD_EXCEPT, 0); 103176379Sdas 104176379Sdas /* func(+-Inf) == NaN */ 105176379Sdas testall(tan, INFINITY, NAN, ALL_STD_EXCEPT, FE_INVALID); 106176379Sdas testall(sin, INFINITY, NAN, ALL_STD_EXCEPT, FE_INVALID); 107176379Sdas testall(cos, INFINITY, NAN, ALL_STD_EXCEPT, FE_INVALID); 108176379Sdas testall(tan, -INFINITY, NAN, ALL_STD_EXCEPT, FE_INVALID); 109176379Sdas testall(sin, -INFINITY, NAN, ALL_STD_EXCEPT, FE_INVALID); 110176379Sdas testall(cos, -INFINITY, NAN, ALL_STD_EXCEPT, FE_INVALID); 111176379Sdas 112176379Sdas /* func(NaN) == NaN */ 113176379Sdas testall(tan, NAN, NAN, ALL_STD_EXCEPT, 0); 114176379Sdas testall(sin, NAN, NAN, ALL_STD_EXCEPT, 0); 115176379Sdas testall(cos, NAN, NAN, ALL_STD_EXCEPT, 0); 116176379Sdas} 117176379Sdas 118176379Sdas/* 119176379Sdas * Tests to ensure argument reduction for large arguments is accurate. 120176379Sdas */ 121176379Sdasstatic void 122176379Sdasrun_reduction_tests(void) 123176379Sdas{ 124176379Sdas /* floats very close to odd multiples of pi */ 125176379Sdas static const float f_pi_odd[] = { 126176379Sdas 85563208.0f, 127176379Sdas 43998769152.0f, 128176379Sdas 9.2763667655669323e+25f, 129176379Sdas 1.5458357838905804e+29f, 130176379Sdas }; 131176379Sdas /* doubles very close to odd multiples of pi */ 132176379Sdas static const double d_pi_odd[] = { 133176379Sdas 3.1415926535897931, 134176379Sdas 91.106186954104004, 135176379Sdas 642615.9188844458, 136176379Sdas 3397346.5699258847, 137176379Sdas 6134899525417045.0, 138176379Sdas 3.0213551960457761e+43, 139176379Sdas 1.2646209897993783e+295, 140176379Sdas 6.2083625380677099e+307, 141176379Sdas }; 142176379Sdas /* long doubles very close to odd multiples of pi */ 143176379Sdas#if LDBL_MANT_DIG == 64 144176379Sdas static const long double ld_pi_odd[] = { 145176379Sdas 1.1891886960373841596e+101L, 146176379Sdas 1.07999475322710967206e+2087L, 147176379Sdas 6.522151627890431836e+2147L, 148176379Sdas 8.9368974898260328229e+2484L, 149176379Sdas 9.2961044110572205863e+2555L, 150176379Sdas 4.90208421886578286e+3189L, 151176379Sdas 1.5275546401232615884e+3317L, 152176379Sdas 1.7227465626338900093e+3565L, 153176379Sdas 2.4160090594000745334e+3808L, 154176379Sdas 9.8477555741888350649e+4314L, 155176379Sdas 1.6061597222105160737e+4326L, 156176379Sdas }; 157176379Sdas#elif LDBL_MANT_DIG == 113 158176379Sdas static const long double ld_pi_odd[] = { 159176379Sdas /* XXX */ 160176379Sdas }; 161176379Sdas#endif 162176379Sdas 163176379Sdas int i; 164176379Sdas 165176379Sdas for (i = 0; i < LEN(f_pi_odd); i++) { 166176379Sdas assert(fabs(sinf(f_pi_odd[i])) < FLT_EPSILON); 167176379Sdas assert(cosf(f_pi_odd[i]) == -1.0); 168176379Sdas assert(fabs(tan(f_pi_odd[i])) < FLT_EPSILON); 169176379Sdas 170176379Sdas assert(fabs(sinf(-f_pi_odd[i])) < FLT_EPSILON); 171176379Sdas assert(cosf(-f_pi_odd[i]) == -1.0); 172176379Sdas assert(fabs(tanf(-f_pi_odd[i])) < FLT_EPSILON); 173176379Sdas 174176379Sdas assert(fabs(sinf(f_pi_odd[i] * 2)) < FLT_EPSILON); 175176379Sdas assert(cosf(f_pi_odd[i] * 2) == 1.0); 176176379Sdas assert(fabs(tanf(f_pi_odd[i] * 2)) < FLT_EPSILON); 177176379Sdas 178176379Sdas assert(fabs(sinf(-f_pi_odd[i] * 2)) < FLT_EPSILON); 179176379Sdas assert(cosf(-f_pi_odd[i] * 2) == 1.0); 180176379Sdas assert(fabs(tanf(-f_pi_odd[i] * 2)) < FLT_EPSILON); 181176379Sdas } 182176379Sdas 183176379Sdas for (i = 0; i < LEN(d_pi_odd); i++) { 184176379Sdas assert(fabs(sin(d_pi_odd[i])) < 2 * DBL_EPSILON); 185176379Sdas assert(cos(d_pi_odd[i]) == -1.0); 186176379Sdas assert(fabs(tan(d_pi_odd[i])) < 2 * DBL_EPSILON); 187176379Sdas 188176379Sdas assert(fabs(sin(-d_pi_odd[i])) < 2 * DBL_EPSILON); 189176379Sdas assert(cos(-d_pi_odd[i]) == -1.0); 190176379Sdas assert(fabs(tan(-d_pi_odd[i])) < 2 * DBL_EPSILON); 191176379Sdas 192176379Sdas assert(fabs(sin(d_pi_odd[i] * 2)) < 2 * DBL_EPSILON); 193176379Sdas assert(cos(d_pi_odd[i] * 2) == 1.0); 194176379Sdas assert(fabs(tan(d_pi_odd[i] * 2)) < 2 * DBL_EPSILON); 195176379Sdas 196176379Sdas assert(fabs(sin(-d_pi_odd[i] * 2)) < 2 * DBL_EPSILON); 197176379Sdas assert(cos(-d_pi_odd[i] * 2) == 1.0); 198176379Sdas assert(fabs(tan(-d_pi_odd[i] * 2)) < 2 * DBL_EPSILON); 199176379Sdas } 200176379Sdas 201176379Sdas#if LDBL_MANT_DIG > 53 202176379Sdas for (i = 0; i < LEN(ld_pi_odd); i++) { 203176379Sdas assert(fabsl(sinl(ld_pi_odd[i])) < LDBL_EPSILON); 204176379Sdas assert(cosl(ld_pi_odd[i]) == -1.0); 205176379Sdas assert(fabsl(tanl(ld_pi_odd[i])) < LDBL_EPSILON); 206176379Sdas 207176379Sdas assert(fabsl(sinl(-ld_pi_odd[i])) < LDBL_EPSILON); 208176379Sdas assert(cosl(-ld_pi_odd[i]) == -1.0); 209176379Sdas assert(fabsl(tanl(-ld_pi_odd[i])) < LDBL_EPSILON); 210176379Sdas 211176379Sdas assert(fabsl(sinl(ld_pi_odd[i] * 2)) < LDBL_EPSILON); 212176379Sdas assert(cosl(ld_pi_odd[i] * 2) == 1.0); 213176379Sdas assert(fabsl(tanl(ld_pi_odd[i] * 2)) < LDBL_EPSILON); 214176379Sdas 215176379Sdas assert(fabsl(sinl(-ld_pi_odd[i] * 2)) < LDBL_EPSILON); 216176379Sdas assert(cosl(-ld_pi_odd[i] * 2) == 1.0); 217176379Sdas assert(fabsl(tanl(-ld_pi_odd[i] * 2)) < LDBL_EPSILON); 218176379Sdas } 219176379Sdas#endif 220176379Sdas} 221176379Sdas 222176379Sdas/* 223176379Sdas * Tests the accuracy of these functions over the primary range. 224176379Sdas */ 225176379Sdasstatic void 226176379Sdasrun_accuracy_tests(void) 227176379Sdas{ 228176379Sdas 229176379Sdas /* For small args, sin(x) = tan(x) = x, and cos(x) = 1. */ 230176379Sdas testall(sin, 0xd.50ee515fe4aea16p-114L, 0xd.50ee515fe4aea16p-114L, 231176379Sdas ALL_STD_EXCEPT, FE_INEXACT); 232176379Sdas testall(tan, 0xd.50ee515fe4aea16p-114L, 0xd.50ee515fe4aea16p-114L, 233176379Sdas ALL_STD_EXCEPT, FE_INEXACT); 234176379Sdas testall(cos, 0xd.50ee515fe4aea16p-114L, 1.0, 235176379Sdas ALL_STD_EXCEPT, FE_INEXACT); 236176379Sdas 237176379Sdas /* 238176379Sdas * These tests should pass for f32, d64, and ld80 as long as 239176379Sdas * the error is <= 0.75 ulp (round to nearest) 240176379Sdas */ 241176379Sdas testall(sin, 0.17255452780841205174L, 0.17169949801444412683L, 242176379Sdas ALL_STD_EXCEPT, FE_INEXACT); 243176379Sdas testall(sin, -0.75431944555904520893L, -0.68479288156557286353L, 244176379Sdas ALL_STD_EXCEPT, FE_INEXACT); 245176379Sdas testall(cos, 0.70556358769838947292L, 0.76124620693117771850L, 246176379Sdas ALL_STD_EXCEPT, FE_INEXACT); 247176379Sdas testall(cos, -0.34061437849088045332L, 0.94254960031831729956L, 248176379Sdas ALL_STD_EXCEPT, FE_INEXACT); 249176379Sdas testall(tan, -0.15862817413325692897L, -0.15997221861309522115L, 250176379Sdas ALL_STD_EXCEPT, FE_INEXACT); 251176379Sdas testall(tan, 0.38374784931303813530L, 0.40376500259976759951L, 252176379Sdas ALL_STD_EXCEPT, FE_INEXACT); 253176379Sdas 254176379Sdas /* 255176379Sdas * XXX missing: 256176379Sdas * - tests for ld128 257176379Sdas * - tests for other rounding modes (probably won't pass for now) 258176379Sdas * - tests for large numbers that get reduced to hi+lo with lo!=0 259176379Sdas */ 260176379Sdas} 261176379Sdas 262176379Sdasint 263176379Sdasmain(int argc, char *argv[]) 264176379Sdas{ 265176379Sdas 266176379Sdas printf("1..3\n"); 267176379Sdas 268176379Sdas run_special_tests(); 269176379Sdas printf("ok 1 - trig\n"); 270176379Sdas 271176379Sdas#ifndef __i386__ 272176379Sdas run_reduction_tests(); 273176379Sdas#endif 274176379Sdas printf("ok 2 - trig\n"); 275176379Sdas 276176379Sdas#ifndef __i386__ 277176379Sdas run_accuracy_tests(); 278176379Sdas#endif 279176379Sdas printf("ok 3 - trig\n"); 280176379Sdas 281176379Sdas return (0); 282176379Sdas} 283