home > さまざまな言語で数値計算 > 級数展開・連分数展開 >

さまざまな言語で数値計算

Only Do What Only You Can Do

正弦関数 (級数展開)

級数展開(テイラー展開)を使って $ \sin x $ を求めます.

VBScript

Option Explicit

Const PI = 3.14159265359
Dim degree
For degree = 0 To 360 Step 15
    If (degree Mod 30 = 0 Or degree Mod 45 = 0) Then
        Dim radian: radian = degree * PI / 180.0
        '自作の正弦関数
        Dim d1: d1         = mySin(radian, 1, False, radian, 1.0, radian)
        '標準の正弦関数
        Dim d2: d2         = Sin(radian)
        '標準関数との差異
        WScript.StdOut.Write Right(Space(3)  & degree,                               3) & " : "
        WScript.StdOut.Write Right(Space(13) & FormatNumber(d1,      10, -1, 0, 0), 13) & " - "
        WScript.StdOut.Write Right(Space(13) & FormatNumber(d2,      10, -1, 0, 0), 13) & " = "
        WScript.StdOut.Write Right(Space(13) & FormatNumber(d1 - d2, 10, -1, 0, 0), 13) & vbNewLine
    End If
Next

'自作の正弦関数
Private Function mySin(ByVal x, ByVal n, ByVal nega, ByVal numerator, ByVal denominator, ByVal y)
    Dim m: m    = 2 * n
    denominator = denominator * (m + 1) * m
    numerator   = numerator * x * x
    Dim a: a    = numerator / denominator
    '十分な精度になったら処理を抜ける
    If (a <= 0.00000000001) Then
        mySin = y
    Else
        If Not nega Then a = -a
        mySin = y + mySin(x, n + 1, Not nega, numerator, denominator, a)
    End If
End Function
Z:\>cscript //nologo 0501.vbs
  0 :  0.0000000000 -  0.0000000000 =  0.0000000000
 30 :  0.5000000000 -  0.5000000000 = -0.0000000000
 45 :  0.7071067812 -  0.7071067812 = -0.0000000000
 60 :  0.8660254038 -  0.8660254038 =  0.0000000000
 90 :  1.0000000000 -  1.0000000000 = -0.0000000000
120 :  0.8660254038 -  0.8660254038 = -0.0000000000
135 :  0.7071067812 -  0.7071067812 = -0.0000000000
150 :  0.5000000000 -  0.5000000000 =  0.0000000000
180 : -0.0000000000 - -0.0000000000 = -0.0000000000
210 : -0.5000000000 - -0.5000000000 = -0.0000000000
225 : -0.7071067812 - -0.7071067812 =  0.0000000000
240 : -0.8660254038 - -0.8660254038 =  0.0000000000
270 : -1.0000000000 - -1.0000000000 = -0.0000000000
300 : -0.8660254038 - -0.8660254038 =  0.0000000000
315 : -0.7071067812 - -0.7071067812 = -0.0000000000
330 : -0.5000000000 - -0.5000000000 = -0.0000000000
360 :  0.0000000000 -  0.0000000000 =  0.0000000000

JScript

for (var degree = 0; degree <= 360; degree += 15)
{
    if (degree % 30 == 0 || degree % 45 == 0)
    {
        var radian = degree * Math.PI / 180.0;
        // 自作の正弦関数
        var d1     = mySin(radian, 1, false, radian, 1.0, radian);
        // 標準の正弦関数
        var d2     = Math.sin(radian);
        // 標準関数との差異
        WScript.StdOut.Write(("   "           + degree               ).slice( -3) + " : ");
        WScript.StdOut.Write(("             " + d1.toFixed(10)       ).slice(-13) + " - ");
        WScript.StdOut.Write(("             " + d2.toFixed(10)       ).slice(-13) + " = ");
        WScript.StdOut.Write(("             " + (d1 - d2).toFixed(10)).slice(-13) + "\n" );
    }
}

// 自作の正弦関数
function mySin(x, n, nega, numerator, denominator, y)
{
    var m       = 2 * n;
    denominator = denominator * (m + 1) * m;
    numerator   = numerator * x * x;
    var a       = numerator / denominator;
    // 十分な精度になったら処理を抜ける
    if (a <= 0.00000000001)
        return y;
    else
        return y + mySin(x, ++n, !nega, numerator, denominator, nega ? a : -a);
}
Z:\>cscript //nologo 0501.js
  0 :  0.0000000000 -  0.0000000000 =  0.0000000000
 30 :  0.5000000000 -  0.5000000000 = -0.0000000000
 45 :  0.7071067812 -  0.7071067812 = -0.0000000000
 60 :  0.8660254038 -  0.8660254038 =  0.0000000000
 90 :  1.0000000000 -  1.0000000000 = -0.0000000000
120 :  0.8660254038 -  0.8660254038 = -0.0000000000
135 :  0.7071067812 -  0.7071067812 = -0.0000000000
150 :  0.5000000000 -  0.5000000000 =  0.0000000000
180 : -0.0000000000 -  0.0000000000 = -0.0000000000
210 : -0.5000000000 - -0.5000000000 = -0.0000000000
225 : -0.7071067812 - -0.7071067812 =  0.0000000000
240 : -0.8660254038 - -0.8660254038 =  0.0000000000
270 : -1.0000000000 - -1.0000000000 = -0.0000000000
300 : -0.8660254038 - -0.8660254038 =  0.0000000000
315 : -0.7071067812 - -0.7071067812 = -0.0000000000
330 : -0.5000000000 - -0.5000000000 = -0.0000000000
360 :  0.0000000000 - -0.0000000000 =  0.0000000000

PowerShell

# 自作の正弦関数
function mySin($x, $n, $nega, $numerator, $denominator, $y)
{
    $m           = 2 * $n
    $denominator = $denominator * ($m + 1) * $m
    $numerator   = $numerator * $x * $x
    $a           = $numerator / $denominator
    # 十分な精度になったら処理を抜ける
    if ($a -le 0.00000000001)
    {
        $y
    }
    else
    {
        if (-not $nega) { $a = -$a}
        $y + (mySin $x  ($n + 1) (-not $nega) $numerator $denominator $a)
    }
}

foreach ($i in 0..24)
{
    $degree = $i * 15
    if ($degree % 30 -eq 0 -or $degree % 45 -eq 0)
    {
        $radian = $degree * [Math]::PI / 180.0
        # 自作の正弦関数
        $d1     = mySin $radian 1 $false $radian 1.0 $radian
        # 標準の正弦関数
        $d2     = [Math]::Sin($radian)
        # 標準関数との差異
        Write-Host ([string]::format("{0,3:D} : {1,13:F10} - {2,13:F10} = {3,13:F10}", $degree, $d1, $d2, $d1 - $d2))
    }
}
Z:\>powershell -file 0501.ps1
  0 :  0.0000000000 -  0.0000000000 =  0.0000000000
 30 :  0.5000000000 -  0.5000000000 =  0.0000000000
 45 :  0.7071067812 -  0.7071067812 =  0.0000000000
 60 :  0.8660254038 -  0.8660254038 =  0.0000000000
 90 :  1.0000000000 -  1.0000000000 =  0.0000000000
120 :  0.8660254038 -  0.8660254038 =  0.0000000000
135 :  0.7071067812 -  0.7071067812 =  0.0000000000
150 :  0.5000000000 -  0.5000000000 =  0.0000000000
180 :  0.0000000000 -  0.0000000000 =  0.0000000000
210 : -0.5000000000 - -0.5000000000 =  0.0000000000
225 : -0.7071067812 - -0.7071067812 =  0.0000000000
240 : -0.8660254038 - -0.8660254038 =  0.0000000000
270 : -1.0000000000 - -1.0000000000 =  0.0000000000
300 : -0.8660254038 - -0.8660254038 =  0.0000000000
315 : -0.7071067812 - -0.7071067812 =  0.0000000000
330 : -0.5000000000 - -0.5000000000 =  0.0000000000
360 :  0.0000000000 -  0.0000000000 =  0.0000000000

Perl

use Math::Trig qw'deg2rad tan';

# 自作の正弦関数
sub mySin
{
    my ($x, $n, $nega, $numerator, $denominator, $y) = @_;

    $m           = 2 * $n;
    $denominator = $denominator * ($m + 1) * $m;
    $numerator   = $numerator * $x * $x;
    $a           = $numerator / $denominator;
    # 十分な精度になったら処理を抜ける
    if ($a <= 0.00000000001)
    {
        $y;
    }
    else
    {
        $y + mySin($x, ++$n, !$nega, $numerator, $denominator, $nega ? $a : -$a);
    }
}

for $i (0..24)
{
    $degree = $i * 15;
    if ($degree % 30 == 0 || $degree % 45 == 0)
    {
        $radian = deg2rad($degree);
        # 自作の正弦関数
        $d1     = mySin($radian, 1, 0, $radian, 1.0, $radian);
        # 標準の正弦関数
        $d2     = sin($radian);
        # 標準関数との差異
        printf("%3d : %13.10f - %13.10f = %13.10f\n", $degree, $d1, $d2, $d1 - $d2);
    }
}
Z:\>perl 0501.pl
  0 :  0.0000000000 -  0.0000000000 =  0.0000000000
 30 :  0.5000000000 -  0.5000000000 = -0.0000000000
 45 :  0.7071067812 -  0.7071067812 = -0.0000000000
 60 :  0.8660254038 -  0.8660254038 =  0.0000000000
 90 :  1.0000000000 -  1.0000000000 = -0.0000000000
120 :  0.8660254038 -  0.8660254038 = -0.0000000000
135 :  0.7071067812 -  0.7071067812 = -0.0000000000
150 :  0.5000000000 -  0.5000000000 =  0.0000000000
180 : -0.0000000000 -  0.0000000000 = -0.0000000000
210 : -0.5000000000 - -0.5000000000 = -0.0000000000
225 : -0.7071067812 - -0.7071067812 =  0.0000000000
240 : -0.8660254038 - -0.8660254038 =  0.0000000000
270 : -1.0000000000 - -1.0000000000 = -0.0000000000
300 : -0.8660254038 - -0.8660254038 =  0.0000000000
315 : -0.7071067812 - -0.7071067812 = -0.0000000000
330 : -0.5000000000 - -0.5000000000 = -0.0000000000
360 :  0.0000000000 -  0.0000000000 =  0.0000000000

PHP

<?php
# 自作の正弦関数
function mySin($x, $n, $nega, $numerator, $denominator, $y)
{
    $m           = 2 * $n;
    $denominator = $denominator * ($m + 1) * $m;
    $numerator   = $numerator * $x * $x;
    $a           = $numerator / $denominator;
    # 十分な精度になったら処理を抜ける
    if ($a <= 0.00000000001)
        return $y;
    else
        return $y + mySin($x, ++$n, !$nega, $numerator, $denominator, $nega ? $a : -$a);
}

foreach (range(0, 24) as $i)
{
    $degree = $i * 15;
    if ($degree % 30 == 0 || $degree % 45 == 0)
    {
        $radian = deg2rad($degree);
        # 自作の正弦関数
        $d1     = mySin($radian, 1, false, $radian, 1.0, $radian);
        # 標準の正弦関数
        $d2     = sin($radian);
        # 標準関数との差異
        printf("%3d : %13.10f - %13.10f = %13.10f\n", $degree, $d1, $d2, $d1 - $d2);
    }
}
?>
Z:\>php 0501.php
  0 :  0.0000000000 -  0.0000000000 =  0.0000000000
 30 :  0.5000000000 -  0.5000000000 = -0.0000000000
 45 :  0.7071067812 -  0.7071067812 = -0.0000000000
 60 :  0.8660254038 -  0.8660254038 =  0.0000000000
 90 :  1.0000000000 -  1.0000000000 = -0.0000000000
120 :  0.8660254038 -  0.8660254038 = -0.0000000000
135 :  0.7071067812 -  0.7071067812 = -0.0000000000
150 :  0.5000000000 -  0.5000000000 =  0.0000000000
180 : -0.0000000000 -  0.0000000000 = -0.0000000000
210 : -0.5000000000 - -0.5000000000 = -0.0000000000
225 : -0.7071067812 - -0.7071067812 =  0.0000000000
240 : -0.8660254038 - -0.8660254038 =  0.0000000000
270 : -1.0000000000 - -1.0000000000 = -0.0000000000
300 : -0.8660254038 - -0.8660254038 =  0.0000000000
315 : -0.7071067812 - -0.7071067812 = -0.0000000000
330 : -0.5000000000 - -0.5000000000 = -0.0000000000
360 :  0.0000000000 - -0.0000000000 =  0.0000000000

Python

# coding: Shift_JIS

import math
 
# 自作の正弦関数
def mySin(x, n, nega, numerator, denominator, y):
    m           = 2 * n
    denominator = denominator * (m + 1) * m
    numerator   = numerator * x * x
    a           = numerator / denominator
    # 十分な精度になったら処理を抜ける
    if (a <= 0.00000000001):
        return y
    else:
        return y + mySin(x, n + 1, not nega, numerator, denominator, a if nega else -a)
 
for i in range(0, 25):
    degree = i * 15
    if degree % 30 == 0 or degree % 45 == 0:
        radian = math.radians(degree)
        # 自作の正弦関数
        d1     = mySin(radian, 1, False, radian, 1.0, radian)
        # 標準の正弦関数
        d2     = math.sin(radian)
        # 標準関数との差異
        print "%3d : %13.10f - %13.10f = %13.10f" % (degree, d1, d2, d1 - d2)
Z:\>python 0501.py
  0 :  0.0000000000 -  0.0000000000 =  0.0000000000
 30 :  0.5000000000 -  0.5000000000 = -0.0000000000
 45 :  0.7071067812 -  0.7071067812 = -0.0000000000
 60 :  0.8660254038 -  0.8660254038 =  0.0000000000
 90 :  1.0000000000 -  1.0000000000 = -0.0000000000
120 :  0.8660254038 -  0.8660254038 = -0.0000000000
135 :  0.7071067812 -  0.7071067812 = -0.0000000000
150 :  0.5000000000 -  0.5000000000 =  0.0000000000
180 : -0.0000000000 -  0.0000000000 = -0.0000000000
210 : -0.5000000000 - -0.5000000000 = -0.0000000000
225 : -0.7071067812 - -0.7071067812 =  0.0000000000
240 : -0.8660254038 - -0.8660254038 =  0.0000000000
270 : -1.0000000000 - -1.0000000000 = -0.0000000000
300 : -0.8660254038 - -0.8660254038 =  0.0000000000
315 : -0.7071067812 - -0.7071067812 = -0.0000000000
330 : -0.5000000000 - -0.5000000000 = -0.0000000000
360 :  0.0000000000 - -0.0000000000 =  0.0000000000

Ruby

# 自作の正弦関数
def mySin(x, n, nega, numerator, denominator, y)
    m           = 2 * n
    denominator = denominator * (m + 1) * m
    numerator   = numerator * x * x
    a           = numerator / denominator
    # 十分な精度になったら処理を抜ける
    if (a <= 0.00000000001)
        y
    else
        y + mySin(x, n + 1, !nega, numerator, denominator, nega ? a : -a)
    end
end

(1..24).each do |i|
    degree = i * 15
    if degree % 30 == 0 || degree % 45 == 0
        radian = degree * Math::PI / 180
        # 自作の正弦関数
        d1     = mySin(radian, 1, false, radian, 1.0, radian)
        # 標準の正弦関数
        d2     = Math.sin(radian)
        # 標準関数との差異
        printf("%3d : %13.10f - %13.10f = %13.10f\n", degree, d1, d2, d1 - d2)
    end
end
Z:\>ruby 0501.rb
  0 :  0.0000000000 -  0.0000000000 =  0.0000000000
 30 :  0.5000000000 -  0.5000000000 = -0.0000000000
 45 :  0.7071067812 -  0.7071067812 = -0.0000000000
 60 :  0.8660254038 -  0.8660254038 =  0.0000000000
 90 :  1.0000000000 -  1.0000000000 = -0.0000000000
120 :  0.8660254038 -  0.8660254038 = -0.0000000000
135 :  0.7071067812 -  0.7071067812 = -0.0000000000
150 :  0.5000000000 -  0.5000000000 =  0.0000000000
180 : -0.0000000000 -  0.0000000000 = -0.0000000000
210 : -0.5000000000 - -0.5000000000 = -0.0000000000
225 : -0.7071067812 - -0.7071067812 =  0.0000000000
240 : -0.8660254038 - -0.8660254038 =  0.0000000000
270 : -1.0000000000 - -1.0000000000 = -0.0000000000
300 : -0.8660254038 - -0.8660254038 =  0.0000000000
315 : -0.7071067812 - -0.7071067812 = -0.0000000000
330 : -0.5000000000 - -0.5000000000 = -0.0000000000
360 :  0.0000000000 - -0.0000000000 =  0.0000000000

Groovy

Pascal

Program Pas0501(arg);
{$MODE delphi}

uses
    SysUtils, Math;

// 自作の正弦関数
function mySin(x:Double; n:Integer; nega:Boolean; numerator:Double; denominator:Double; y:Double):Double;
var
    m: Integer;
    a: Double;
begin
    m           := 2 * n;
    denominator := denominator * (m + 1) * m;
    numerator   := numerator   * x * x;
    a           := numerator / denominator;
    // 十分な精度になったら処理を抜ける
    if (a <= 0.00000000001) then
        result := y
    else
    begin
        if not nega then a := -a;
        result := y + mySin(x, n + 1, not nega, numerator, denominator, a);
    end;
end;

var
    i:      Integer;
    degree: Integer;
    radian: Double;
    d1:     Double;
    d2:     Double;
begin
    for i := 0 to 24 do
    begin
        degree := i * 15;
        if (degree mod 30 = 0) or (degree mod 45 = 0) then
        begin
            radian := DegToRad(degree);
            // 自作の正弦関数
            d1 := mySin(radian, 1, false, radian, 1.0, radian);
            // 標準の正弦関数
            d2 := Sin(radian);
            // 標準関数との差異
            writeln(format('%3d : %13.10f - %13.10f = %13.10f', [degree, d1, d2, d1 - d2]));
        end;
    end;
end.
Z:\>fpc Pas0501.pp -v0
Free Pascal Compiler version 2.6.2 [2013/02/12] for i386
Copyright (c) 1993-2012 by Florian Klaempfl and others

Z:\>Pas0501
  0 :  0.0000000000 -  0.0000000000 =  0.0000000000
 30 :  0.5000000000 -  0.5000000000 =  0.0000000000
 45 :  0.7071067812 -  0.7071067812 =  0.0000000000
 60 :  0.8660254038 -  0.8660254038 =  0.0000000000
 90 :  1.0000000000 -  1.0000000000 =  0.0000000000
120 :  0.8660254038 -  0.8660254038 =  0.0000000000
135 :  0.7071067812 -  0.7071067812 =  0.0000000000
150 :  0.5000000000 -  0.5000000000 =  0.0000000000
180 :  0.0000000000 -  0.0000000000 =  0.0000000000
210 : -0.5000000000 - -0.5000000000 =  0.0000000000
225 : -0.7071067812 - -0.7071067812 =  0.0000000000
240 : -0.8660254038 - -0.8660254038 =  0.0000000000
270 : -1.0000000000 - -1.0000000000 =  0.0000000000
300 : -0.8660254038 - -0.8660254038 =  0.0000000000
315 : -0.7071067812 - -0.7071067812 =  0.0000000000
330 : -0.5000000000 - -0.5000000000 =  0.0000000000
360 :  0.0000000000 -  0.0000000000 =  0.0000000000

Ada

VB.NET

Module VB0501
    Public Sub Main()
        For degree As Integer = 0 To 360 Step 15
            If (degree Mod 30 = 0 OrElse degree Mod 45 = 0) Then
                Dim radian As Double = degree * Math.PI / 180.0
                '自作の正弦関数
                Dim d1 As Double     = mySin(radian, 1, False, radian, 1.0, radian)
                '標準の正弦関数
                Dim d2 As Double     = Math.Sin(radian)
                '標準関数との差異
                Console.WriteLine(String.Format("{0,3:D} : {1,13:F10} - {2,13:F10} = {3,13:F10}", degree, d1, d2, d1 - d2))
            End If
        Next
    End Sub

    '自作の正弦関数
    Private Function mySin(ByVal x As Double, ByVal n As Integer, ByVal nega As Boolean, ByVal numerator As Double, ByVal denominator As Double, ByVal y As Double) As Double
        Dim m As Integer = 2 * n
        denominator      = denominator * (m + 1) * m
        numerator        = numerator   * x * x
        Dim a As Double  = numerator / denominator
        '十分な精度になったら処理を抜ける
        If (a <= 0.00000000001) Then
            Return y
        Else
            Return y + mySin(x, n + 1, Not nega, numerator, denominator, If(nega, a, -a))
        End If
    End Function
End Module
Z:\>vbc -nologo VB0501.vb

Z:\>VB0501
  0 :  0.0000000000 -  0.0000000000 =  0.0000000000
 30 :  0.5000000000 -  0.5000000000 =  0.0000000000
 45 :  0.7071067812 -  0.7071067812 =  0.0000000000
 60 :  0.8660254038 -  0.8660254038 =  0.0000000000
 90 :  1.0000000000 -  1.0000000000 =  0.0000000000
120 :  0.8660254038 -  0.8660254038 =  0.0000000000
135 :  0.7071067812 -  0.7071067812 =  0.0000000000
150 :  0.5000000000 -  0.5000000000 =  0.0000000000
180 :  0.0000000000 -  0.0000000000 =  0.0000000000
210 : -0.5000000000 - -0.5000000000 =  0.0000000000
225 : -0.7071067812 - -0.7071067812 =  0.0000000000
240 : -0.8660254038 - -0.8660254038 =  0.0000000000
270 : -1.0000000000 - -1.0000000000 =  0.0000000000
300 : -0.8660254038 - -0.8660254038 =  0.0000000000
315 : -0.7071067812 - -0.7071067812 =  0.0000000000
330 : -0.5000000000 - -0.5000000000 =  0.0000000000
360 :  0.0000000000 -  0.0000000000 =  0.0000000000

C#

using System;

public class CS0501
{
    public static void Main()
    {
        for (int degree = 0; degree <= 360; degree += 15)
        {
            if (degree % 30 == 0 || degree % 45 == 0)
            {
                double radian = degree * Math.PI / 180.0;
                // 自作の正弦関数
                double d1     = mySin(radian, 1, false, radian, 1.0, radian);
                // 標準の正弦関数
                double d2     = Math.Sin(radian);
                // 標準関数との差異
                Console.WriteLine(string.Format("{0,3:D} : {1,13:F10} - {2,13:F10} = {3,13:F10}", degree, d1, d2, d1 - d2));
            }
        }
    }

    // 自作の正弦関数
    private static double mySin(double x, int n, bool nega, double numerator, double denominator, double y)
    {
        int m       = 2 * n;
        denominator = denominator * (m + 1) * m;
        numerator   = numerator   * x * x;
        double a    = numerator / denominator;
        // 十分な精度になったら処理を抜ける
        if (a <= 0.00000000001) 
            return y;
        else
            return y + mySin(x, ++n, !nega, numerator, denominator, nega ? a : -a);
    }
}
Z:\>csc -nologo CS0501.cs

Z:\>CS0501
  0 :  0.0000000000 -  0.0000000000 =  0.0000000000
 30 :  0.5000000000 -  0.5000000000 =  0.0000000000
 45 :  0.7071067812 -  0.7071067812 =  0.0000000000
 60 :  0.8660254038 -  0.8660254038 =  0.0000000000
 90 :  1.0000000000 -  1.0000000000 =  0.0000000000
120 :  0.8660254038 -  0.8660254038 =  0.0000000000
135 :  0.7071067812 -  0.7071067812 =  0.0000000000
150 :  0.5000000000 -  0.5000000000 =  0.0000000000
180 :  0.0000000000 -  0.0000000000 =  0.0000000000
210 : -0.5000000000 - -0.5000000000 =  0.0000000000
225 : -0.7071067812 - -0.7071067812 =  0.0000000000
240 : -0.8660254038 - -0.8660254038 =  0.0000000000
270 : -1.0000000000 - -1.0000000000 =  0.0000000000
300 : -0.8660254038 - -0.8660254038 =  0.0000000000
315 : -0.7071067812 - -0.7071067812 =  0.0000000000
330 : -0.5000000000 - -0.5000000000 =  0.0000000000
360 :  0.0000000000 -  0.0000000000 =  0.0000000000

Java

public class Java0501 {
    public static void main(String []args) {
        for (int degree = 0; degree <= 360; degree += 15) {
            if (degree % 30 == 0 || degree % 45 == 0) {
                double radian = Math.toRadians(degree);
                // 自作の正弦関数
                double d1     = mySin(radian, 1, false, radian, 1.0, radian);
                // 標準の正弦関数
                double d2     = Math.sin(radian);
                // 標準関数との差異
                System.out.println(String.format("%3d : %13.10f - %13.10f = %13.10f", degree, d1, d2, d1 - d2));
            }
        }
    }

    // 自作の正弦関数
    private static double mySin(double x, int n, boolean nega, double numerator, double denominator, double y) {
        int m       = 2 * n;
        denominator = denominator * (m + 1) * m;
        numerator   = numerator   * x * x;
        double a    = numerator / denominator;
        // 十分な精度になったら処理を抜ける
        if (a <= 0.00000000001) 
            return y;
        else
            return y + mySin(x, ++n, !nega, numerator, denominator, nega ? a : -a);
    }
}
Z:\>javac Java0501.java

Z:\>java Java0501
  0 :  0.0000000000 -  0.0000000000 =  0.0000000000
 30 :  0.5000000000 -  0.5000000000 = -0.0000000000
 45 :  0.7071067812 -  0.7071067812 = -0.0000000000
 60 :  0.8660254038 -  0.8660254038 =  0.0000000000
 90 :  1.0000000000 -  1.0000000000 = -0.0000000000
120 :  0.8660254038 -  0.8660254038 = -0.0000000000
135 :  0.7071067812 -  0.7071067812 = -0.0000000000
150 :  0.5000000000 -  0.5000000000 =  0.0000000000
180 : -0.0000000000 -  0.0000000000 = -0.0000000000
210 : -0.5000000000 - -0.5000000000 = -0.0000000000
225 : -0.7071067812 - -0.7071067812 =  0.0000000000
240 : -0.8660254038 - -0.8660254038 =  0.0000000000
270 : -1.0000000000 - -1.0000000000 = -0.0000000000
300 : -0.8660254038 - -0.8660254038 =  0.0000000000
315 : -0.7071067812 - -0.7071067812 = -0.0000000000
330 : -0.5000000000 - -0.5000000000 = -0.0000000000
360 :  0.0000000000 - -0.0000000000 =  0.0000000000

C++

#include <iostream>
#include <iomanip>
#include <math>
using namespace std;

double mySin(double x, int n, bool nega, double numerator, double denominator, double y);

int main()
{
    for (int degree = 0; degree <= 360; degree += 15)
    {
        if (degree % 30 == 0 || degree % 45 == 0)
        {
            double radian = degree * M_PI / 180.0;
            // 自作の正弦関数
            double d1     = mySin(radian, 1, false, radian, 1.0, radian);
            // 標準の正弦関数
            double d2     = sin(radian);
            // 標準関数との差異
            cout << setw(3)  << degree << ":";
            cout << setw(14) << fixed << setprecision(10) << d1 << " - ";
            cout << setw(14) << fixed << setprecision(10) << d2 << " = ";
            cout << setw(14) << fixed << setprecision(10) << d1 - d2 << endl;
        }
    }
    return 0;
}

// 自作の正弦関数
double mySin(double x, int n, bool nega, double numerator, double denominator, double y)
{
    int m       = 2 * n;
    denominator = denominator * (m + 1) * m;
    numerator   = numerator   * x * x;
    double a    = numerator / denominator;
    // 十分な精度になったら処理を抜ける
    if (a <= 0.00000000001) 
        return y;
    else
        return y + mySin(x, ++n, !nega, numerator, denominator, nega ? a : -a);
}
Z:\>bcc32 CP0501.cpp
Borland C++ 5.5.1 for Win32 Copyright (c) 1993, 2000 Borland
CP0501.cpp:
Turbo Incremental Link 5.00 Copyright (c) 1997, 2000 Borland

Z:\>CP0501
  0:  0.0000000000 -   0.0000000000 =   0.0000000000
 30:  0.5000000000 -   0.5000000000 =  -0.0000000000
 45:  0.7071067812 -   0.7071067812 =  -0.0000000000
 60:  0.8660254038 -   0.8660254038 =   0.0000000000
 90:  1.0000000000 -   1.0000000000 =  -0.0000000000
120:  0.8660254038 -   0.8660254038 =  -0.0000000000
135:  0.7071067812 -   0.7071067812 =  -0.0000000000
150:  0.5000000000 -   0.5000000000 =   0.0000000000
180: -0.0000000000 -   0.0000000000 =  -0.0000000000
210: -0.5000000000 -  -0.5000000000 =  -0.0000000000
225: -0.7071067812 -  -0.7071067812 =   0.0000000000
240: -0.8660254038 -  -0.8660254038 =   0.0000000000
270: -1.0000000000 -  -1.0000000000 =  -0.0000000000
300: -0.8660254038 -  -0.8660254038 =   0.0000000000
315: -0.7071067812 -  -0.7071067812 =  -0.0000000000
330: -0.5000000000 -  -0.5000000000 =  -0.0000000000
360:  0.0000000000 -  -0.0000000000 =   0.0000000000

Objective-C

#import <Foundation/Foundation.h>

double mySin(double x, int n, bool nega, double numerator, double denominator, double y);

int main()
{
    int degree;
    for (degree = 0; degree <= 360; degree += 15)
    {
        if (degree % 30 == 0 || degree % 45 == 0)
        {
            double radian = degree * M_PI / 180.0;
            // 自作の正弦関数
            double d1     = mySin(radian, 1, false, radian, 1.0, radian);
            // 標準の正弦関数
            double d2     = sin(radian);
            // 標準関数との差異
            printf("%03d : %+13.10f - %+13.10f = %+13.10f\n", degree, d1, d2, d1 - d2);
        }
    }
    return 0;
}

// 自作の正弦関数
double mySin(double x, int n, bool nega, double numerator, double denominator, double y)
{
    int m       = 2 * n;
    denominator = denominator * (m + 1) * m;
    numerator   = numerator   * x * x;
    double a    = numerator / denominator;
    // 十分な精度になったら処理を抜ける
    if (a <= 0.00000000001) 
        return y;
    else
        return y + mySin(x, ++n, !nega, numerator, denominator, nega ? a : -a);
}
Compiling the source code....
$gcc `gnustep-config --objc-flags` -L/usr/GNUstep/System/Library/Libraries -lgnustep-base main.m -o demo -lm -pthread -lgmpxx -lreadline 2>&1

Executing the program....
$demo 
000 : +0.0000000000 - +0.0000000000 = +0.0000000000
030 : +0.5000000000 - +0.5000000000 = -0.0000000000
045 : +0.7071067812 - +0.7071067812 = -0.0000000000
060 : +0.8660254038 - +0.8660254038 = +0.0000000000
090 : +1.0000000000 - +1.0000000000 = -0.0000000000
120 : +0.8660254038 - +0.8660254038 = -0.0000000000
135 : +0.7071067812 - +0.7071067812 = -0.0000000000
150 : +0.5000000000 - +0.5000000000 = +0.0000000000
180 : -0.0000000000 - +0.0000000000 = -0.0000000000
210 : -0.5000000000 - -0.5000000000 = -0.0000000000
225 : -0.7071067812 - -0.7071067812 = +0.0000000000
240 : -0.8660254038 - -0.8660254038 = +0.0000000000
270 : -1.0000000000 - -1.0000000000 = -0.0000000000
300 : -0.8660254038 - -0.8660254038 = +0.0000000000
315 : -0.7071067812 - -0.7071067812 = -0.0000000000
330 : -0.5000000000 - -0.5000000000 = -0.0000000000
360 : +0.0000000000 - -0.0000000000 = +0.0000000000

D

Go

Scala

対話型実行環境を起動

Z:\>scala
Welcome to Scala version 2.10.2 (Java HotSpot(TM) Client VM, Java 1.7.0_21).
Type in expressions to have them evaluated.
Type :help for more information.

級数展開(テイラー展開)で sin x を求める

// 自作の正弦関数
def mySin(x:Double, n:Int, nega:Boolean, numerator:Double, denominator:Double, y:Double):Double = {
    val m     = 2 * n
    val denom = denominator * (m + 1) * m
    val num   = numerator   * x * x
    val a     = num / denom
    // 十分な精度になったら処理を抜ける
    if (a <= 0.00000000001) 
        y
    else
        y + mySin(x, n + 1, !nega, num, denom, if (nega) a else -a)
}
(0 to 24).
    map(_ * 15).
    filter(n => (n % 30 == 0) || (n % 45 == 0)).
    foreach { degree =>
        val radian = Math.toRadians(degree)
        // 自作の正弦関数
        val d1 = mySin(radian, 1, false, radian, 1.0, radian)
        // 標準の正弦関数
        val d2 = Math.sin(radian)
        // 標準関数との差異
        println("%3d : %13.10f - %13.10f = %13.10f".format(degree, d1, d2, d1 - d2));
    }
  0 :  0.0000000000 -  0.0000000000 =  0.0000000000
 30 :  0.5000000000 -  0.5000000000 = -0.0000000000
 45 :  0.7071067812 -  0.7071067812 = -0.0000000000
 60 :  0.8660254038 -  0.8660254038 =  0.0000000000
 90 :  1.0000000000 -  1.0000000000 = -0.0000000000
120 :  0.8660254038 -  0.8660254038 = -0.0000000000
135 :  0.7071067812 -  0.7071067812 = -0.0000000000
150 :  0.5000000000 -  0.5000000000 =  0.0000000000
180 : -0.0000000000 -  0.0000000000 = -0.0000000000
210 : -0.5000000000 - -0.5000000000 = -0.0000000000
225 : -0.7071067812 - -0.7071067812 =  0.0000000000
240 : -0.8660254038 - -0.8660254038 =  0.0000000000
270 : -1.0000000000 - -1.0000000000 = -0.0000000000
300 : -0.8660254038 - -0.8660254038 =  0.0000000000
315 : -0.7071067812 - -0.7071067812 = -0.0000000000
330 : -0.5000000000 - -0.5000000000 = -0.0000000000
360 :  0.0000000000 - -0.0000000000 =  0.0000000000

終了

scala> :quit

F#

対話型実行環境を起動

Z:\>fsi
Microsoft (R) F# 2.0 Interactive build 4.0.40219.1
Copyright (c) Microsoft Corporation. All Rights Reserved.

For help type #help;;

級数展開(テイラー展開)で sin x を求める

// 自作の正弦関数
let rec mySin (x:double) (n:int) (nega:bool) (numerator:double) (denominator:double) (y:double):double =
    let m     = 2 * n
    let denom = denominator * (double (m + 1)) * (double m)
    let num   = numerator   * x * x
    let a     = num / denom
    // 十分な精度になったら処理を抜ける
    if a <= 0.00000000001 then
        y
    else
        y + (mySin x (n + 1) (not nega) num denom (if nega then a else -a) )
[0..24]
|> List.map    (fun n -> n * 15)
|> List.filter (fun n -> (n % 30 = 0) || (n % 45 = 0))
|> List.iter
    (fun degree -> 
        let radian = (double degree) * System.Math.PI / 180.0
        // 自作の正弦関数
        let d1 = (mySin radian 1 false radian 1.0 radian)
        // 標準の正弦関数
        let d2 = System.Math.Sin(radian)
        // 標準関数との差異
        printfn "%3d : %13.10f - %13.10f = %13.10f" degree d1 d2 (d1 - d2)
    )
  0 :  0.0000000000 -  0.0000000000 =  0.0000000000
 30 :  0.5000000000 -  0.5000000000 =  0.0000000000
 45 :  0.7071067812 -  0.7071067812 =  0.0000000000
 60 :  0.8660254038 -  0.8660254038 =  0.0000000000
 90 :  1.0000000000 -  1.0000000000 =  0.0000000000
120 :  0.8660254038 -  0.8660254038 =  0.0000000000
135 :  0.7071067812 -  0.7071067812 =  0.0000000000
150 :  0.5000000000 -  0.5000000000 =  0.0000000000
180 :  0.0000000000 -  0.0000000000 =  0.0000000000
210 : -0.5000000000 - -0.5000000000 =  0.0000000000
225 : -0.7071067812 - -0.7071067812 =  0.0000000000
240 : -0.8660254038 - -0.8660254038 =  0.0000000000
270 : -1.0000000000 - -1.0000000000 =  0.0000000000
300 : -0.8660254038 - -0.8660254038 =  0.0000000000
315 : -0.7071067812 - -0.7071067812 =  0.0000000000
330 : -0.5000000000 - -0.5000000000 =  0.0000000000
360 :  0.0000000000 -  0.0000000000 =  0.0000000000
val it : unit = ()

終了

> #quit;;

Clojure

対話型実行環境を起動

Z:\>java -cp C:\ProgramFiles\clojure-1.5.1\clojure-1.5.1.jar clojure.main
Clojure 1.5.1

級数展開(テイラー展開)で sin x を求める

;自作の正弦関数
(defn mySin [x n nega numerator denominator y]
    (def m     (* 2 n))
    (def denom (* denominator (* (+ m 1) m)))
    (def nume  (* numerator (* x x)))
    (def a     (/ nume denom))
    ;十分な精度になったら処理を抜ける
    (if (<= a 0.00000000001)
        y
        (+ y (mySin x (+ n 1) (not nega) nume denom (if nega a (- a))))))
(doseq
    [degree (filter #(or (= (mod % 30) 0) 
                         (= (mod % 45) 0))
            (map    #(* % 15)
            (range 0 25)))]
    (do
        (def radian (/ (* degree (. Math PI)) 180.0))
        ;自作の正弦関数
        (def d1     (mySin radian 1 false radian 1.0 radian))
        ;標準の正弦関数
        (def d2     (. Math sin radian))
        ;標準関数との差異
        (println (format "%3d : %13.10f - %13.10f = %13.10f" degree d1 d2 (- d1 d2)))))
  0 :  0.0000000000 -  0.0000000000 =  0.0000000000
 30 :  0.5000000000 -  0.5000000000 = -0.0000000000
 45 :  0.7071067812 -  0.7071067812 = -0.0000000000
 60 :  0.8660254038 -  0.8660254038 =  0.0000000000
 90 :  1.0000000000 -  1.0000000000 = -0.0000000000
120 :  0.8660254038 -  0.8660254038 = -0.0000000000
135 :  0.7071067812 -  0.7071067812 = -0.0000000000
150 :  0.5000000000 -  0.5000000000 =  0.0000000000
180 : -0.0000000000 -  0.0000000000 = -0.0000000000
210 : -0.5000000000 - -0.5000000000 = -0.0000000000
225 : -0.7071067812 - -0.7071067812 =  0.0000000000
240 : -0.8660254038 - -0.8660254038 =  0.0000000000
270 : -1.0000000000 - -1.0000000000 = -0.0000000000
300 : -0.8660254038 - -0.8660254038 =  0.0000000000
315 : -0.7071067812 - -0.7071067812 = -0.0000000000
330 : -0.5000000000 - -0.5000000000 = -0.0000000000
360 :  0.0000000000 - -0.0000000000 =  0.0000000000
nil

終了

user=> (. System exit 0)

Haskell

対話型実行環境を起動

Z:\>ghci
GHCi, version 7.6.3: http://www.haskell.org/ghc/  :? for help
Loading package ghc-prim ... linking ... done.
Loading package integer-gmp ... linking ... done.
Loading package base ... linking ... done.

級数展開(テイラー展開)で sin x を求める

-- 自作の正弦関数
mySin::Double->Int->Bool->Double->Double->Double->Double
mySin x n nega numerator denominator y =
let
    m     = 2 * n
    denom = denominator * (fromIntegral (m + 1)) * (fromIntegral m)
    num   = numerator   * x * x
    a     = num / denom
in 
    -- 十分な精度になったら処理を抜ける
    if a <= 0.00000000001 then
        y
    else
        y + (mySin x (n + 1) (not nega) num denom (if nega then a else -a))

対話型実行環境で複数行を渡す場合は, 以下のように ":{~:}" で括る.
さらに, 関数定義は "let {~}" で括る.

Prelude> :{
Prelude| let {
Prelude| mySin::Double->Int->Bool->Double->Double->Double->Double;
Prelude| mySin x n nega numerator denominator y =
Prelude| let
Prelude|     m     = 2 * n
Prelude|     denom = denominator * (fromIntegral (m + 1)) * (fromIntegral m)
Prelude|     num   = numerator   * x * x
Prelude|     a     = num / denom
Prelude| in
Prelude|     if a <= 0.00000000001 then
Prelude|         y
Prelude|     else
Prelude|         y + (mySin x (n + 1) (not nega) num denom (if nega then a else -a))
Prelude| }
Prelude| :}
import Text.Printf
import Control.Monad

forM_ (
    filter (\n -> (mod n 30 == 0 || mod n 45 == 0)) $ 
    map (* 15) $ 
    [0..24::Int]
) $ \degree -> do
    let radian = (fromIntegral degree) * pi / 180.0
    -- 自作の正弦関数
    let d1 = (mySin radian 1 False radian 1.0 radian)
    -- 標準の正弦関数
    let d2 = sin(radian)
    -- 標準関数との差異
    printf "%3d : %13.10f - %13.10f = %13.10f\n" degree d1 d2 (d1- d2)
  0 :  0.0000000000 -  0.0000000000 =  0.0000000000
 30 :  0.5000000000 -  0.5000000000 =  0.0000000000
 45 :  0.7071067812 -  0.7071067812 =  0.0000000000
 60 :  0.8660254038 -  0.8660254038 = -0.0000000000
 90 :  1.0000000000 -  1.0000000000 =  0.0000000000
120 :  0.8660254038 -  0.8660254038 =  0.0000000000
135 :  0.7071067812 -  0.7071067812 =  0.0000000000
150 :  0.5000000000 -  0.5000000000 = -0.0000000000
180 :  0.0000000000 - -0.0000000000 =  0.0000000000
210 : -0.5000000000 - -0.5000000000 =  0.0000000000
225 : -0.7071067812 - -0.7071067812 = -0.0000000000
240 : -0.8660254038 - -0.8660254038 = -0.0000000000
270 : -1.0000000000 - -1.0000000000 =  0.0000000000
300 : -0.8660254038 - -0.8660254038 = -0.0000000000
315 : -0.7071067812 - -0.7071067812 =  0.0000000000
330 : -0.5000000000 - -0.5000000000 =  0.0000000000
360 : -0.0000000000 -  0.0000000000 = -0.0000000000

終了

Prelude> :quit
Leaving GHCi.
inserted by FC2 system