language: C++ 4.7.2 (gcc-4.7.2)
date: 731 days 12 hours ago
link:
visibility: public
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#include <math.h>
#include <stdio.h>
 
// P Found using maxima
//
// y(x) := 4 * x * (%pi-x) / (%pi^2) ; 
// z(x) := (1-p)*y(x) + p * y(x)^2;
// e(x) := z(x) - sin(x);
// solve( diff( integrate( e(x)^2, x, 0, %pi/2 ), p ) = 0, p ),numer;
// 
// [p = .2248391013559941]
 
double fast_sin1(double x)
{
  const double A = 4.0/(M_PI*M_PI);
  const double P =  0.2248391013559941;
  double y = A* x * ( M_PI - x );
 
  
  return y* ( (1-P)  + y * P );
}
 
// P and Q found using maxima
//
// y(x) := 4 * x * (%pi-x) / (%pi^2) ; 
// zz(x) := (1-p-q)*y(x) + p * y(x)^2 + q * y(x)^3
// ee(x) := zz(x) - sin(x)
// solve( [ integrate( diff(ee(x)^2, p ), x, 0, %pi/2 ) = 0, integrate( diff(ee(x)^2,q), x, 0, %pi/2 ) = 0 ] , [p,q] ),numer;
//
// [[p = .1952403377008734, q = .01915214119105392]]
 
double fast_sin2(double x)
{
  const double A = 4.0/(M_PI*M_PI);
  const double P = 0.1952403377008734;
  const double Q = 0.01915214119105392;
 
  double y = A* x * ( M_PI - x );
 
  
  return y*( (1-P-Q) + y*( P + y * Q ) ) ;
}
 
 
int main()
{
  printf( "x        sin(x)   fs1(x)   fs2(x)   e1(x)    e2(x)\n" );
  for(unsigned int i=0; i<100; ++i)
  {
    double x = i*(M_PI/(100-1));
    printf( "%04.6f %04.6f %04.6f %04.6f %04.6f %04.6f\n", x, sin(x), fast_sin1(x), fast_sin2(x), sin(x)-fast_sin1(x), sin(x)-fast_sin2(x) );
  }
}
  • upload with new input
  • result: Success     time: 0s    memory: 2724 kB     returned value: 0

    x        sin(x)   fs1(x)   fs2(x)   e1(x)    e2(x)
    0.000000 0.000000 0.000000 0.000000 0.000000 0.000000
    0.031733 0.031728 0.031363 0.031735 0.000365 -0.000007
    0.063467 0.063424 0.062783 0.063434 0.000641 -0.000010
    0.095200 0.095056 0.094218 0.095068 0.000838 -0.000012
    0.126933 0.126592 0.125625 0.126604 0.000968 -0.000012
    0.158666 0.158001 0.156962 0.158012 0.001040 -0.000010
    0.190400 0.189251 0.188189 0.189260 0.001062 -0.000008
    0.222133 0.220311 0.219268 0.220316 0.001043 -0.000006
    0.253866 0.251148 0.250158 0.251151 0.000990 -0.000003
    0.285599 0.281733 0.280822 0.281733 0.000910 -0.000001
    0.317333 0.312033 0.311224 0.312032 0.000809 0.000002
    0.349066 0.342020 0.341328 0.342016 0.000692 0.000004
    0.380799 0.371662 0.371098 0.371657 0.000565 0.000006
    0.412532 0.400931 0.400500 0.400924 0.000431 0.000007
    0.444266 0.429795 0.429501 0.429787 0.000294 0.000008
    0.475999 0.458227 0.458069 0.458218 0.000158 0.000008
    0.507732 0.486197 0.486172 0.486189 0.000025 0.000008
    0.539465 0.513677 0.513780 0.513670 -0.000102 0.000008
    0.571199 0.540641 0.540862 0.540634 -0.000222 0.000007
    0.602932 0.567060 0.567391 0.567054 -0.000332 0.000006
    0.634665 0.592908 0.593339 0.592903 -0.000431 0.000005
    0.666398 0.618159 0.618677 0.618155 -0.000518 0.000004
    0.698132 0.642788 0.643381 0.642785 -0.000594 0.000002
    0.729865 0.666769 0.667426 0.666768 -0.000657 0.000001
    0.761598 0.690079 0.690786 0.690079 -0.000707 -0.000000
    0.793331 0.712694 0.713438 0.712696 -0.000744 -0.000002
    0.825065 0.734592 0.735361 0.734595 -0.000769 -0.000003
    0.856798 0.755750 0.756532 0.755754 -0.000783 -0.000004
    0.888531 0.776146 0.776931 0.776152 -0.000785 -0.000005
    0.920265 0.795762 0.796538 0.795768 -0.000776 -0.000006
    0.951998 0.814576 0.815334 0.814583 -0.000758 -0.000007
    0.983731 0.832570 0.833302 0.832577 -0.000732 -0.000007
    1.015464 0.849725 0.850423 0.849733 -0.000698 -0.000007
    1.047198 0.866025 0.866683 0.866033 -0.000657 -0.000008
    1.078931 0.881453 0.882065 0.881461 -0.000611 -0.000007
    1.110664 0.895994 0.896555 0.896001 -0.000561 -0.000007
    1.142397 0.909632 0.910140 0.909639 -0.000508 -0.000007
    1.174131 0.922354 0.922808 0.922361 -0.000453 -0.000006
    1.205864 0.934148 0.934545 0.934154 -0.000398 -0.000006
    1.237597 0.945001 0.945343 0.945006 -0.000342 -0.000005
    1.269330 0.954902 0.955191 0.954907 -0.000288 -0.000005
    1.301064 0.963842 0.964079 0.963846 -0.000237 -0.000004
    1.332797 0.971812 0.972000 0.971815 -0.000188 -0.000003
    1.364530 0.978802 0.978947 0.978805 -0.000144 -0.000002
    1.396263 0.984808 0.984913 0.984810 -0.000105 -0.000002
    1.427997 0.989821 0.989893 0.989823 -0.000071 -0.000001
    1.459730 0.993838 0.993882 0.993839 -0.000044 -0.000001
    1.491463 0.996855 0.996877 0.996855 -0.000022 -0.000000
    1.523196 0.998867 0.998875 0.998867 -0.000008 -0.000000
    1.554930 0.999874 0.999875 0.999874 -0.000001 -0.000000
    1.586663 0.999874 0.999875 0.999874 -0.000001 -0.000000
    1.618396 0.998867 0.998875 0.998867 -0.000008 -0.000000
    1.650129 0.996855 0.996877 0.996855 -0.000022 -0.000000
    1.681863 0.993838 0.993882 0.993839 -0.000044 -0.000001
    1.713596 0.989821 0.989893 0.989823 -0.000071 -0.000001
    1.745329 0.984808 0.984913 0.984810 -0.000105 -0.000002
    1.777063 0.978802 0.978947 0.978805 -0.000144 -0.000002
    1.808796 0.971812 0.972000 0.971815 -0.000188 -0.000003
    1.840529 0.963842 0.964079 0.963846 -0.000237 -0.000004
    1.872262 0.954902 0.955191 0.954907 -0.000288 -0.000005
    1.903996 0.945001 0.945343 0.945006 -0.000342 -0.000005
    1.935729 0.934148 0.934545 0.934154 -0.000398 -0.000006
    1.967462 0.922354 0.922808 0.922361 -0.000453 -0.000006
    1.999195 0.909632 0.910140 0.909639 -0.000508 -0.000007
    2.030929 0.895994 0.896555 0.896001 -0.000561 -0.000007
    2.062662 0.881453 0.882065 0.881461 -0.000611 -0.000007
    2.094395 0.866025 0.866683 0.866033 -0.000657 -0.000008
    2.126128 0.849725 0.850423 0.849733 -0.000698 -0.000007
    2.157862 0.832570 0.833302 0.832577 -0.000732 -0.000007
    2.189595 0.814576 0.815334 0.814583 -0.000758 -0.000007
    2.221328 0.795762 0.796538 0.795768 -0.000776 -0.000006
    2.253061 0.776146 0.776931 0.776152 -0.000785 -0.000005
    2.284795 0.755750 0.756532 0.755754 -0.000783 -0.000004
    2.316528 0.734592 0.735361 0.734595 -0.000769 -0.000003
    2.348261 0.712694 0.713438 0.712696 -0.000744 -0.000002
    2.379994 0.690079 0.690786 0.690079 -0.000707 -0.000000
    2.411728 0.666769 0.667426 0.666768 -0.000657 0.000001
    2.443461 0.642788 0.643381 0.642785 -0.000594 0.000002
    2.475194 0.618159 0.618677 0.618155 -0.000518 0.000004
    2.506927 0.592908 0.593339 0.592903 -0.000431 0.000005
    2.538661 0.567060 0.567391 0.567054 -0.000332 0.000006
    2.570394 0.540641 0.540862 0.540634 -0.000222 0.000007
    2.602127 0.513677 0.513780 0.513670 -0.000102 0.000008
    2.633861 0.486197 0.486172 0.486189 0.000025 0.000008
    2.665594 0.458227 0.458069 0.458218 0.000158 0.000008
    2.697327 0.429795 0.429501 0.429787 0.000294 0.000008
    2.729060 0.400931 0.400500 0.400924 0.000431 0.000007
    2.760794 0.371662 0.371098 0.371657 0.000565 0.000006
    2.792527 0.342020 0.341328 0.342016 0.000692 0.000004
    2.824260 0.312033 0.311224 0.312032 0.000809 0.000002
    2.855993 0.281733 0.280822 0.281733 0.000910 -0.000001
    2.887727 0.251148 0.250158 0.251151 0.000990 -0.000003
    2.919460 0.220311 0.219268 0.220316 0.001043 -0.000006
    2.951193 0.189251 0.188189 0.189260 0.001062 -0.000008
    2.982926 0.158001 0.156962 0.158012 0.001040 -0.000010
    3.014660 0.126592 0.125625 0.126604 0.000968 -0.000012
    3.046393 0.095056 0.094218 0.095068 0.000838 -0.000012
    3.078126 0.063424 0.062783 0.063434 0.000641 -0.000010
    3.109859 0.031728 0.031363 0.031735 0.000365 -0.000007
    3.141593 0.000000 0.000000 0.000000 0.000000 0.000000