Home | Info | Community | Development | myReactOS | Contact Us
ReactOS Development > Doxygencos.c
Go to the documentation of this file.
00001 /* 00002 * COPYRIGHT: See COPYING in the top level directory 00003 * PROJECT: ReactOS CRT 00004 * FILE: lib/crt/math/cos.c 00005 * PURPOSE: Generic C Implementation of cos 00006 * PROGRAMMER: Timo Kreuzer (timo.kreuzer@reactos.org) 00007 */ 00008 00009 #define PRECISION 9 00010 #define M_PI 3.141592653589793238462643 00011 00012 static double cos_off_tbl[] = {0.0, -M_PI/2., 0, -M_PI/2.}; 00013 static double cos_sign_tbl[] = {1,-1,-1,1}; 00014 00015 double 00016 cos(double x) 00017 { 00018 int quadrant; 00019 double x2, result; 00020 00021 /* Calculate the quadrant */ 00022 quadrant = (int)(x * (2./M_PI)); 00023 00024 /* Get offset inside quadrant */ 00025 x = x - quadrant * (M_PI/2.); 00026 00027 /* Normalize quadrant to [0..3] */ 00028 quadrant = quadrant & 0x3; 00029 00030 /* Fixup value for the generic function */ 00031 x += cos_off_tbl[quadrant]; 00032 00033 /* Calculate the negative of the square of x */ 00034 x2 = - (x * x); 00035 00036 /* This is an unrolled taylor series using <PRECISION> iterations 00037 * Example with 4 iterations: 00038 * result = 1 - x^2/2! + x^4/4! - x^6/6! + x^8/8! 00039 * To save multiplications and to keep the precision high, it's performed 00040 * like this: 00041 * result = 1 - x^2 * (1/2! - x^2 * (1/4! - x^2 * (1/6! - x^2 * (1/8!)))) 00042 */ 00043 00044 /* Start with 0, compiler will optimize this away */ 00045 result = 0; 00046 00047 #if (PRECISION >= 10) 00048 result += 1./(1.*2*3*4*5*6*7*8*9*10*11*12*13*14*15*16*17*18*19*20); 00049 result *= x2; 00050 #endif 00051 #if (PRECISION >= 9) 00052 result += 1./(1.*2*3*4*5*6*7*8*9*10*11*12*13*14*15*16*17*18); 00053 result *= x2; 00054 #endif 00055 #if (PRECISION >= 8) 00056 result += 1./(1.*2*3*4*5*6*7*8*9*10*11*12*13*14*15*16); 00057 result *= x2; 00058 #endif 00059 #if (PRECISION >= 7) 00060 result += 1./(1.*2*3*4*5*6*7*8*9*10*11*12*13*14); 00061 result *= x2; 00062 #endif 00063 #if (PRECISION >= 6) 00064 result += 1./(1.*2*3*4*5*6*7*8*9*10*11*12); 00065 result *= x2; 00066 #endif 00067 #if (PRECISION >= 5) 00068 result += 1./(1.*2*3*4*5*6*7*8*9*10); 00069 result *= x2; 00070 #endif 00071 result += 1./(1.*2*3*4*5*6*7*8); 00072 result *= x2; 00073 00074 result += 1./(1.*2*3*4*5*6); 00075 result *= x2; 00076 00077 result += 1./(1.*2*3*4); 00078 result *= x2; 00079 00080 result += 1./(1.*2); 00081 result *= x2; 00082 00083 result += 1; 00084 00085 /* Apply correct sign */ 00086 result *= cos_sign_tbl[quadrant]; 00087 00088 return result; 00089 } Generated on Thu May 24 2012 04:36:56 for ReactOS by
1.7.6.1
|