next up previous
Next: A..5 BASIC Up: A. 計算の仕方 Previous: A..3 Mathematica

A..4 C 言語によるプログラム

C 言語によるプログラミングは習っているはずだし、 フリーの処理系もあるので、本当はこれくらい自力で出来て欲しいところ。


/*
 * computesin.c
 */

#include <stdio.h>
#include <math.h>

double halfsin(double cosx)
{
  return sqrt((1-cosx)/2);
}

double halfcos(double cosx)
{
  return sqrt((1+cosx)/2);
}

int main()
{
  double root2, root3, root5;
  double sin18, cos18, sin15, cos15, sin3, cos3, sin1p5, cos1p5;
  double sin0p75, cos0p75;

  root2 = sqrt(2.0);
  root3 = sqrt(3.0);
  root5 = sqrt(5.0);
  sin18 = (root5-1)/4;
  cos18 = sqrt((5+root5)/2)/2;
  sin15 = (root3 - 1)/(2*root2);
  cos15 = (root3 + 1)/(2*root2);
  sin3 = sin18*cos15-cos18*sin15;
  cos3 = cos18*cos15+sin18*sin15;

  sin1p5 = halfsin(cos3);
  cos1p5 = halfcos(cos3);
  sin0p75 = halfsin(cos1p5);
  cos0p75 = halfcos(cos1p5);

  printf("sin18  =%20.15f\n", sin18);
  printf("cos18  =%20.15f\n", cos18);
  printf("sin15  =%20.15f\n", sin15);
  printf("cos15  =%20.15f\n", cos15);
  printf("sin3   =%20.15f\n", sin3);
  printf("cos3   =%20.15f\n", cos3);
  printf("sin1.5 =%20.15f\n", sin1p5);
  printf("cos1.5 =%20.15f\n", cos1p5);
  printf("sin0.75=%20.15f\n", sin0p75);
  printf("cos0.75=%20.15f\n", cos0p75);

  return 0;
}

oyabun% ./computesin
sin18  =   0.309016994374947
cos18  =   0.951056516295154
sin15  =   0.258819045102521
cos15  =   0.965925826289068
sin3   =   0.052335956242944
cos3   =   0.998629534754574
sin1.5 =   0.026176948307874
cos1.5 =   0.999657324975557
sin0.75=   0.013089595571345
cos0.75=   0.999914327574007
oyabun%

Solaris では long double の精度が高いので、それを利用すると
oyabun% ./computesin-long
sin18  =   0.30901699437494742410229341718281908
cos18  =   0.95105651629515357211643933337938214
sin15  =   0.25881904510252076234889883762404831
cos15  =   0.96592582628906828674974319972889726
sin3   =   0.05233595624294383272211862960907841
cos3   =   0.99862953475457387378449205843943650
sin1.5 =   0.02617694830787315261061168555411346
cos1.5 =   0.99965732497555728003676088836767982
sin0.75=   0.01308959557134444019028420970285323
cos0.75=   0.99991432757400703224892204745488401
oyabun%


next up previous
Next: A..5 BASIC Up: A. 計算の仕方 Previous: A..3 Mathematica
Masashi Katsurada
平成17年7月19日