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

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

Only Do What Only You Can Do

双曲線正弦関数 (級数展開)

級数展開(テイラー展開)で $ \sinh x $ を求めます.

VBScript

Option Explicit

Dim i
For i = 0 To 20
    Dim x:  x  = i - 10
    '自作の双曲線正弦関数
    Dim d1: d1 = mySinh(x, 1, x, 1.0, x)
    '標準の双曲線正弦関数はない
    Dim d2: d2 = (Exp(x) - Exp(-x)) / 2
    '標準関数との差異
    WScript.StdOut.Write Right(Space(3)  & x,                                    3) & " : "
    WScript.StdOut.Write Right(Space(17) & FormatNumber(d1,      10, -1, 0, 0), 17) & " - "
    WScript.StdOut.Write Right(Space(17) & FormatNumber(d2,      10, -1, 0, 0), 17) & " = "
    WScript.StdOut.Write Right(Space(13) & FormatNumber(d1 - d2, 10, -1, 0, 0), 13) & vbNewLine
Next

'自作の双曲線正弦関数
Private Function mySinh(ByVal x, ByVal n, 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 (Abs(a) <= 0.00000000001) Then
        mySinh = y
    Else
        mySinh = y + mySinh(x, n + 1, numerator, denominator, a)
    End If
End Function
Z:\>cscript //nologo 0508.vbs
-10 : -11013.2328747034 - -11013.2328747034 =  0.0000000000
 -9 :  -4051.5419020828 -  -4051.5419020828 =  0.0000000000
 -8 :  -1490.4788257895 -  -1490.4788257896 =  0.0000000000
 -7 :   -548.3161232732 -   -548.3161232732 =  0.0000000000
 -6 :   -201.7131573703 -   -201.7131573703 =  0.0000000000
 -5 :    -74.2032105778 -    -74.2032105778 =  0.0000000000
 -4 :    -27.2899171971 -    -27.2899171971 =  0.0000000000
 -3 :    -10.0178749274 -    -10.0178749274 =  0.0000000000
 -2 :     -3.6268604078 -     -3.6268604078 =  0.0000000000
 -1 :     -1.1752011936 -     -1.1752011936 =  0.0000000000
  0 :      0.0000000000 -      0.0000000000 =  0.0000000000
  1 :      1.1752011936 -      1.1752011936 = -0.0000000000
  2 :      3.6268604078 -      3.6268604078 = -0.0000000000
  3 :     10.0178749274 -     10.0178749274 = -0.0000000000
  4 :     27.2899171971 -     27.2899171971 = -0.0000000000
  5 :     74.2032105778 -     74.2032105778 = -0.0000000000
  6 :    201.7131573703 -    201.7131573703 = -0.0000000000
  7 :    548.3161232732 -    548.3161232732 = -0.0000000000
  8 :   1490.4788257895 -   1490.4788257896 = -0.0000000000
  9 :   4051.5419020828 -   4051.5419020828 = -0.0000000000
 10 :  11013.2328747034 -  11013.2328747034 = -0.0000000000

JScript

for (var x = -10; x <= 10; x++)
{
    // 自作の双曲線正弦関数
    var d1 = mySinh(x, 1, x, 1.0, x);
    // 標準の双曲線正弦関数はない
    var d2 = (Math.exp(x) - Math.exp(-x)) / 2;
    // 標準関数との差異
    WScript.StdOut.Write(("   "               + x                    ).slice( -3) + " : ");
    WScript.StdOut.Write(("                 " + d1.toFixed(10)       ).slice(-17) + " - ");
    WScript.StdOut.Write(("                 " + d2.toFixed(10)       ).slice(-17) + " = ");
    WScript.StdOut.Write(("             "     + (d1 - d2).toFixed(10)).slice(-13) + "\n" );
}

// 自作の双曲線正弦関数
function mySinh(x, n, numerator, denominator, y) 
{
    var m       = 2 * n;
    denominator = denominator * (m + 1) * m;
    numerator   = numerator   * x * x;
    var a       = numerator / denominator;
    // 十分な精度になったら処理を抜ける
    if (Math.abs(a) <= 0.00000000001) 
        return y;
    else
        return y + mySinh(x, ++n, numerator, denominator, a);
}
Z:\>cscript //nologo 0508.js
-10 : -11013.2328747034 - -11013.2328747034 =  0.0000000000
 -9 :  -4051.5419020828 -  -4051.5419020828 =  0.0000000000
 -8 :  -1490.4788257895 -  -1490.4788257896 =  0.0000000000
 -7 :   -548.3161232732 -   -548.3161232732 =  0.0000000000
 -6 :   -201.7131573703 -   -201.7131573703 =  0.0000000000
 -5 :    -74.2032105778 -    -74.2032105778 =  0.0000000000
 -4 :    -27.2899171971 -    -27.2899171971 =  0.0000000000
 -3 :    -10.0178749274 -    -10.0178749274 =  0.0000000000
 -2 :     -3.6268604078 -     -3.6268604078 =  0.0000000000
 -1 :     -1.1752011936 -     -1.1752011936 =  0.0000000000
  0 :      0.0000000000 -      0.0000000000 =  0.0000000000
  1 :      1.1752011936 -      1.1752011936 = -0.0000000000
  2 :      3.6268604078 -      3.6268604078 = -0.0000000000
  3 :     10.0178749274 -     10.0178749274 = -0.0000000000
  4 :     27.2899171971 -     27.2899171971 = -0.0000000000
  5 :     74.2032105778 -     74.2032105778 = -0.0000000000
  6 :    201.7131573703 -    201.7131573703 = -0.0000000000
  7 :    548.3161232732 -    548.3161232732 = -0.0000000000
  8 :   1490.4788257895 -   1490.4788257896 = -0.0000000000
  9 :   4051.5419020828 -   4051.5419020828 = -0.0000000000
 10 :  11013.2328747034 -  11013.2328747034 = -0.0000000000

PowerShell

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

foreach ($i in 0..20)
{
    $x = $i - 10
    # 自作の双曲線正弦関数
    $d1 = mySinh $x 1 $x 1.0 $x
    # 標準の双曲線正弦関数
    $d2 = [Math]::Sinh($x)
    # 標準関数との差異
    Write-Host ([string]::format("{0,3:D} : {1,17:F10} - {2,17:F10} = {3,13:F10}", $x, $d1, $d2, $d1 - $d2))
}
Z:\>powershell -file 0508.ps1
-10 : -11013.2328747034 - -11013.2328747034 =  0.0000000000
 -9 :  -4051.5419020828 -  -4051.5419020828 =  0.0000000000
 -8 :  -1490.4788257895 -  -1490.4788257896 =  0.0000000000
 -7 :   -548.3161232732 -   -548.3161232732 =  0.0000000000
 -6 :   -201.7131573703 -   -201.7131573703 =  0.0000000000
 -5 :    -74.2032105778 -    -74.2032105778 =  0.0000000000
 -4 :    -27.2899171971 -    -27.2899171971 =  0.0000000000
 -3 :    -10.0178749274 -    -10.0178749274 =  0.0000000000
 -2 :     -3.6268604078 -     -3.6268604078 =  0.0000000000
 -1 :     -1.1752011936 -     -1.1752011936 =  0.0000000000
  0 :      0.0000000000 -      0.0000000000 =  0.0000000000
  1 :      1.1752011936 -      1.1752011936 =  0.0000000000
  2 :      3.6268604078 -      3.6268604078 =  0.0000000000
  3 :     10.0178749274 -     10.0178749274 =  0.0000000000
  4 :     27.2899171971 -     27.2899171971 =  0.0000000000
  5 :     74.2032105778 -     74.2032105778 =  0.0000000000
  6 :    201.7131573703 -    201.7131573703 =  0.0000000000
  7 :    548.3161232732 -    548.3161232732 =  0.0000000000
  8 :   1490.4788257895 -   1490.4788257896 =  0.0000000000
  9 :   4051.5419020828 -   4051.5419020828 =  0.0000000000
 10 :  11013.2328747034 -  11013.2328747034 =  0.0000000000

Perl

use Math::Trig;

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

    $m           = 2 * $n;
    $denominator = $denominator * ($m + 1) * $m;
    $numerator   = $numerator * $x * $x;
    $a           = $numerator / $denominator;

    # 十分な精度になったら処理を抜ける
    if (abs($a) <= 0.00000000001)
    {
        $y;
    }
    else
    {
        $y + mySinh($x, ++$n, $numerator, $denominator, $a);
    }
}

for $i (0..20)
{
    $x  = $i - 10;
    # 自作の双曲線正弦関数
    $d1 = mySinh($x, 1, $x, 1.0, $x);
    # 標準の双曲線正弦関数
    $d2 = sinh($x);
    # 標準関数との差異
    printf("%3d : %17.10f - %17.10f = %13.10f\n", $x, $d1, $d2, $d1 - $d2);
}
Z:\>perl 0508.pl
-10 : -11013.2328747034 - -11013.2328747034 =  0.0000000000
 -9 :  -4051.5419020828 -  -4051.5419020828 =  0.0000000000
 -8 :  -1490.4788257895 -  -1490.4788257896 =  0.0000000000
 -7 :   -548.3161232732 -   -548.3161232732 =  0.0000000000
 -6 :   -201.7131573703 -   -201.7131573703 =  0.0000000000
 -5 :    -74.2032105778 -    -74.2032105778 =  0.0000000000
 -4 :    -27.2899171971 -    -27.2899171971 =  0.0000000000
 -3 :    -10.0178749274 -    -10.0178749274 =  0.0000000000
 -2 :     -3.6268604078 -     -3.6268604078 =  0.0000000000
 -1 :     -1.1752011936 -     -1.1752011936 =  0.0000000000
  0 :      0.0000000000 -      0.0000000000 =  0.0000000000
  1 :      1.1752011936 -      1.1752011936 = -0.0000000000
  2 :      3.6268604078 -      3.6268604078 = -0.0000000000
  3 :     10.0178749274 -     10.0178749274 = -0.0000000000
  4 :     27.2899171971 -     27.2899171971 = -0.0000000000
  5 :     74.2032105778 -     74.2032105778 = -0.0000000000
  6 :    201.7131573703 -    201.7131573703 = -0.0000000000
  7 :    548.3161232732 -    548.3161232732 = -0.0000000000
  8 :   1490.4788257895 -   1490.4788257896 = -0.0000000000
  9 :   4051.5419020828 -   4051.5419020828 = -0.0000000000
 10 :  11013.2328747034 -  11013.2328747034 = -0.0000000000

PHP

<?php
# 自作の双曲線正弦関数
function mySinh($x, $n, $numerator, $denominator, $y)
{
    $m           = 2 * $n;
    $denominator = $denominator * ($m + 1) * $m;
    $numerator   = $numerator * $x * $x;
    $a           = $numerator / $denominator;

    # 十分な精度になったら処理を抜ける
    if (abs($a) <= 0.00000000001)
        return $y;
    else
        return $y + mySinh($x, ++$n, $numerator, $denominator, $a);
}

foreach (range(0, 20) as $i)
{
    $x  = $i - 10;
    # 自作の双曲線正弦関数
    $d1 = mySinh($x, 1, $x, 1.0, $x);
    # 標準の双曲線正弦関数
    $d2 = sinh($x);
    # 標準関数との差異
    printf("%3d : %17.10f - %17.10f = %13.10f\n", $x, $d1, $d2, $d1 - $d2);
}
?>
Z:\>php 0508.php
-10 : -11013.2328747034 - -11013.2328747034 =  0.0000000000
 -9 :  -4051.5419020828 -  -4051.5419020828 =  0.0000000000
 -8 :  -1490.4788257895 -  -1490.4788257896 =  0.0000000000
 -7 :   -548.3161232732 -   -548.3161232732 =  0.0000000000
 -6 :   -201.7131573703 -   -201.7131573703 =  0.0000000000
 -5 :    -74.2032105778 -    -74.2032105778 =  0.0000000000
 -4 :    -27.2899171971 -    -27.2899171971 =  0.0000000000
 -3 :    -10.0178749274 -    -10.0178749274 =  0.0000000000
 -2 :     -3.6268604078 -     -3.6268604078 =  0.0000000000
 -1 :     -1.1752011936 -     -1.1752011936 =  0.0000000000
  0 :      0.0000000000 -      0.0000000000 =  0.0000000000
  1 :      1.1752011936 -      1.1752011936 = -0.0000000000
  2 :      3.6268604078 -      3.6268604078 = -0.0000000000
  3 :     10.0178749274 -     10.0178749274 = -0.0000000000
  4 :     27.2899171971 -     27.2899171971 = -0.0000000000
  5 :     74.2032105778 -     74.2032105778 = -0.0000000000
  6 :    201.7131573703 -    201.7131573703 = -0.0000000000
  7 :    548.3161232732 -    548.3161232732 = -0.0000000000
  8 :   1490.4788257895 -   1490.4788257896 = -0.0000000000
  9 :   4051.5419020828 -   4051.5419020828 = -0.0000000000
 10 :  11013.2328747034 -  11013.2328747034 = -0.0000000000

Python

# coding: Shift_JIS

import math
 
# 自作の双曲線正弦関数
def mySinh(x, n, numerator, denominator, y):
    m           = 2 * n
    denominator = denominator * (m + 1) * m
    numerator   = numerator * x * x
    a           = numerator / denominator
    # 十分な精度になったら処理を抜ける
    if (abs(a) <= 0.00000000001):
        return y
    else:
        return y + mySinh(x, n + 1, numerator, denominator, a)

for i in range(0, 21):
    x  = i - 10
    # 自作の双曲線正弦関数
    d1 = mySinh(x, 1, x, 1.0, x)
    # 標準の双曲線正弦関数
    d2 = math.sinh(x)
    # 標準関数との差異
    print "%3d : %17.10f - %17.10f = %13.10f" % (x, d1, d2, d1 - d2)
Z:\>python 0508.py
-10 : -11013.2328747034 - -11013.2328747034 =  0.0000000000
 -9 :  -4051.5419020828 -  -4051.5419020828 =  0.0000000000
 -8 :  -1490.4788257895 -  -1490.4788257896 =  0.0000000000
 -7 :   -548.3161232732 -   -548.3161232732 =  0.0000000000
 -6 :   -201.7131573703 -   -201.7131573703 =  0.0000000000
 -5 :    -74.2032105778 -    -74.2032105778 =  0.0000000000
 -4 :    -27.2899171971 -    -27.2899171971 =  0.0000000000
 -3 :    -10.0178749274 -    -10.0178749274 =  0.0000000000
 -2 :     -3.6268604078 -     -3.6268604078 =  0.0000000000
 -1 :     -1.1752011936 -     -1.1752011936 =  0.0000000000
  0 :      0.0000000000 -      0.0000000000 =  0.0000000000
  1 :      1.1752011936 -      1.1752011936 = -0.0000000000
  2 :      3.6268604078 -      3.6268604078 = -0.0000000000
  3 :     10.0178749274 -     10.0178749274 = -0.0000000000
  4 :     27.2899171971 -     27.2899171971 = -0.0000000000
  5 :     74.2032105778 -     74.2032105778 = -0.0000000000
  6 :    201.7131573703 -    201.7131573703 = -0.0000000000
  7 :    548.3161232732 -    548.3161232732 = -0.0000000000
  8 :   1490.4788257895 -   1490.4788257896 = -0.0000000000
  9 :   4051.5419020828 -   4051.5419020828 = -0.0000000000
 10 :  11013.2328747034 -  11013.2328747034 = -0.0000000000

Ruby

# 自作の双曲線正弦関数
def mySinh(x, n, numerator, denominator, y)
    m           = 2 * n
    denominator = denominator * (m + 1) * m
    numerator   = numerator * x * x
    a           = numerator / denominator
    # 十分な精度になったら処理を抜ける
    if (a.abs <= 0.00000000001)
        y
    else
        y + mySinh(x, n + 1, numerator, denominator, a)
    end
end

(0..20).each do |i|
    x = i - 10
    # 自作の双曲線正弦関数
    d1     = mySinh(x, 1, x, 1.0, x)
    # 標準の双曲線正弦関数
    d2     = Math.sinh(x)
    # 標準関数との差異
    printf("%3d : %17.10f - %17.10f = %13.10f\n", x, d1, d2, d1 - d2)
end
Z:\>ruby 0508.rb
-10 : -11013.2328747034 - -11013.2328747034 =  0.0000000000
 -9 :  -4051.5419020828 -  -4051.5419020828 =  0.0000000000
 -8 :  -1490.4788257895 -  -1490.4788257896 =  0.0000000000
 -7 :   -548.3161232732 -   -548.3161232732 =  0.0000000000
 -6 :   -201.7131573703 -   -201.7131573703 =  0.0000000000
 -5 :    -74.2032105778 -    -74.2032105778 =  0.0000000000
 -4 :    -27.2899171971 -    -27.2899171971 =  0.0000000000
 -3 :    -10.0178749274 -    -10.0178749274 =  0.0000000000
 -2 :     -3.6268604078 -     -3.6268604078 =  0.0000000000
 -1 :     -1.1752011936 -     -1.1752011936 =  0.0000000000
  0 :      0.0000000000 -      0.0000000000 =  0.0000000000
  1 :      1.1752011936 -      1.1752011936 = -0.0000000000
  2 :      3.6268604078 -      3.6268604078 = -0.0000000000
  3 :     10.0178749274 -     10.0178749274 = -0.0000000000
  4 :     27.2899171971 -     27.2899171971 = -0.0000000000
  5 :     74.2032105778 -     74.2032105778 = -0.0000000000
  6 :    201.7131573703 -    201.7131573703 = -0.0000000000
  7 :    548.3161232732 -    548.3161232732 = -0.0000000000
  8 :   1490.4788257895 -   1490.4788257896 = -0.0000000000
  9 :   4051.5419020828 -   4051.5419020828 = -0.0000000000
 10 :  11013.2328747034 -  11013.2328747034 = -0.0000000000

Groovy

Pascal

Program Pas0508(arg);
{$MODE delphi}

uses
    SysUtils, Math;

// 自作の双曲線正弦関数
function mySinh(x:Double; n:Integer; 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 (Abs(a) <= 0.00000000001) then
        result := y
    else
        result := y + mySinh(x, n + 1, numerator, denominator, a);
end;

var
    i:  Integer;
    x:  Integer;
    d1: Double;
    d2: Double;
begin
    for i := 0 to 20 do
    begin
        x  := i - 10;
        // 自作の双曲線正弦関数
        d1 := mySinh(x, 1, x, 1.0, x);
        // 標準の双曲線正弦関数
        d2 := Sinh(x);
        // 標準関数との差異
        writeln(format('%3d : %17.10f - %17.10f = %13.10f', [x, d1, d2, d1 - d2]));
    end;
end.
Z:\>fpc Pas0508.pp -v0
Free Pascal Compiler version 2.6.2 [2013/02/12] for i386
Copyright (c) 1993-2012 by Florian Klaempfl and others

Z:\>Pas0508
-10 : -11013.2328747034 - -11013.2328747034 =  0.0000000000
 -9 :  -4051.5419020828 -  -4051.5419020828 =  0.0000000000
 -8 :  -1490.4788257895 -  -1490.4788257896 =  0.0000000000
 -7 :   -548.3161232732 -   -548.3161232732 =  0.0000000000
 -6 :   -201.7131573703 -   -201.7131573703 =  0.0000000000
 -5 :    -74.2032105778 -    -74.2032105778 =  0.0000000000
 -4 :    -27.2899171971 -    -27.2899171971 =  0.0000000000
 -3 :    -10.0178749274 -    -10.0178749274 =  0.0000000000
 -2 :     -3.6268604078 -     -3.6268604078 =  0.0000000000
 -1 :     -1.1752011936 -     -1.1752011936 =  0.0000000000
  0 :      0.0000000000 -      0.0000000000 =  0.0000000000
  1 :      1.1752011936 -      1.1752011936 =  0.0000000000
  2 :      3.6268604078 -      3.6268604078 =  0.0000000000
  3 :     10.0178749274 -     10.0178749274 =  0.0000000000
  4 :     27.2899171971 -     27.2899171971 =  0.0000000000
  5 :     74.2032105778 -     74.2032105778 =  0.0000000000
  6 :    201.7131573703 -    201.7131573703 =  0.0000000000
  7 :    548.3161232732 -    548.3161232732 =  0.0000000000
  8 :   1490.4788257895 -   1490.4788257896 =  0.0000000000
  9 :   4051.5419020828 -   4051.5419020828 =  0.0000000000
 10 :  11013.2328747034 -  11013.2328747034 =  0.0000000000

Ada

VB.NET

Module VB0508
    Public Sub Main()
        For i As Integer = 0 To 20
            Dim x As Integer = i - 10
            '自作の双曲線正弦関数
            Dim d1 As Double = mySinh(x, 1, x, 1.0, x)
            '標準の双曲線正弦関数
            Dim d2 As Double = Math.Sinh(x)
            '標準関数との差異
            Console.WriteLine(String.Format("{0,3:D} : {1,17:F10} - {2,17:F10} = {3,13:F10}", x, d1, d2, d1 - d2))
        Next
    End Sub

    '自作の双曲線正弦関数
    Private Function mySinh(ByVal x As Double, ByVal n As Integer, 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 (Math.Abs(a) <= 0.00000000001) Then
            Return y
        Else
            Return y + mySinh(x, n + 1, numerator, denominator, a)
        End If
    End Function
End Module
Z:\>vbc -nologo VB0508.vb

Z:\>VB0508
-10 : -11013.2328747034 - -11013.2328747034 =  0.0000000000
 -9 :  -4051.5419020828 -  -4051.5419020828 =  0.0000000000
 -8 :  -1490.4788257895 -  -1490.4788257896 =  0.0000000000
 -7 :   -548.3161232732 -   -548.3161232732 =  0.0000000000
 -6 :   -201.7131573703 -   -201.7131573703 =  0.0000000000
 -5 :    -74.2032105778 -    -74.2032105778 =  0.0000000000
 -4 :    -27.2899171971 -    -27.2899171971 =  0.0000000000
 -3 :    -10.0178749274 -    -10.0178749274 =  0.0000000000
 -2 :     -3.6268604078 -     -3.6268604078 =  0.0000000000
 -1 :     -1.1752011936 -     -1.1752011936 =  0.0000000000
  0 :      0.0000000000 -      0.0000000000 =  0.0000000000
  1 :      1.1752011936 -      1.1752011936 =  0.0000000000
  2 :      3.6268604078 -      3.6268604078 =  0.0000000000
  3 :     10.0178749274 -     10.0178749274 =  0.0000000000
  4 :     27.2899171971 -     27.2899171971 =  0.0000000000
  5 :     74.2032105778 -     74.2032105778 =  0.0000000000
  6 :    201.7131573703 -    201.7131573703 =  0.0000000000
  7 :    548.3161232732 -    548.3161232732 =  0.0000000000
  8 :   1490.4788257895 -   1490.4788257896 =  0.0000000000
  9 :   4051.5419020828 -   4051.5419020828 =  0.0000000000
 10 :  11013.2328747034 -  11013.2328747034 =  0.0000000000

C#

using System;

public class CS0508
{
    public static void Main()
    {
        for (int x = -10; x <= 10; x++)
        {
            // 自作の双曲線正弦関数
            double d1 = mySinh(x, 1, x, 1.0, x);
            // 標準の双曲線正弦関数
            double d2 = Math.Sinh(x);
            // 標準関数との差異
            Console.WriteLine(string.Format("{0,3:D} : {1,17:F10} - {2,17:F10} = {3,13:F10}", x, d1, d2, d1 - d2));
        }
    }

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

Z:\>CS0508
-10 : -11013.2328747034 - -11013.2328747034 =  0.0000000000
 -9 :  -4051.5419020828 -  -4051.5419020828 =  0.0000000000
 -8 :  -1490.4788257895 -  -1490.4788257896 =  0.0000000000
 -7 :   -548.3161232732 -   -548.3161232732 =  0.0000000000
 -6 :   -201.7131573703 -   -201.7131573703 =  0.0000000000
 -5 :    -74.2032105778 -    -74.2032105778 =  0.0000000000
 -4 :    -27.2899171971 -    -27.2899171971 =  0.0000000000
 -3 :    -10.0178749274 -    -10.0178749274 =  0.0000000000
 -2 :     -3.6268604078 -     -3.6268604078 =  0.0000000000
 -1 :     -1.1752011936 -     -1.1752011936 =  0.0000000000
  0 :      0.0000000000 -      0.0000000000 =  0.0000000000
  1 :      1.1752011936 -      1.1752011936 =  0.0000000000
  2 :      3.6268604078 -      3.6268604078 =  0.0000000000
  3 :     10.0178749274 -     10.0178749274 =  0.0000000000
  4 :     27.2899171971 -     27.2899171971 =  0.0000000000
  5 :     74.2032105778 -     74.2032105778 =  0.0000000000
  6 :    201.7131573703 -    201.7131573703 =  0.0000000000
  7 :    548.3161232732 -    548.3161232732 =  0.0000000000
  8 :   1490.4788257895 -   1490.4788257896 =  0.0000000000
  9 :   4051.5419020828 -   4051.5419020828 =  0.0000000000
 10 :  11013.2328747034 -  11013.2328747034 =  0.0000000000

Java

public class Java0508 {
    public static void main(String []args) {
        for (int x = -10; x <= 10; x++) {
            // 自作の双曲線正弦関数
            double d1 = mySinh(x, 1, x, 1.0, x);
            // 標準の双曲線正弦関数
            double d2 = Math.sinh(x);
            // 標準関数との差異
            System.out.println(String.format("%3d : %17.10f - %17.10f = %13.10f", x, d1, d2, d1 - d2));
        }
    }

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

Z:\>java Java0508
-10 : -11013.2328747034 - -11013.2328747034 =  0.0000000000
 -9 :  -4051.5419020828 -  -4051.5419020828 =  0.0000000000
 -8 :  -1490.4788257895 -  -1490.4788257896 =  0.0000000000
 -7 :   -548.3161232732 -   -548.3161232732 =  0.0000000000
 -6 :   -201.7131573703 -   -201.7131573703 =  0.0000000000
 -5 :    -74.2032105778 -    -74.2032105778 =  0.0000000000
 -4 :    -27.2899171971 -    -27.2899171971 =  0.0000000000
 -3 :    -10.0178749274 -    -10.0178749274 =  0.0000000000
 -2 :     -3.6268604078 -     -3.6268604078 =  0.0000000000
 -1 :     -1.1752011936 -     -1.1752011936 =  0.0000000000
  0 :      0.0000000000 -      0.0000000000 =  0.0000000000
  1 :      1.1752011936 -      1.1752011936 = -0.0000000000
  2 :      3.6268604078 -      3.6268604078 = -0.0000000000
  3 :     10.0178749274 -     10.0178749274 = -0.0000000000
  4 :     27.2899171971 -     27.2899171971 = -0.0000000000
  5 :     74.2032105778 -     74.2032105778 = -0.0000000000
  6 :    201.7131573703 -    201.7131573703 = -0.0000000000
  7 :    548.3161232732 -    548.3161232732 = -0.0000000000
  8 :   1490.4788257895 -   1490.4788257896 = -0.0000000000
  9 :   4051.5419020828 -   4051.5419020828 = -0.0000000000
 10 :  11013.2328747034 -  11013.2328747034 = -0.0000000000

C++

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

double mySinh(double x, int n, double numerator, double denominator, double y);

int main()
{
    for (int x = -10; x <= 10; x++)
    {
        // 自作の双曲線正弦関数
        double d1 = mySinh(x, 1, x, 1.0, x);
        // 標準の双曲線正弦関数
        double d2 = sinh(x);
        // 標準関数との差異
        cout << setw(3)  << x << ":";
        cout << setw(17) << fixed << setprecision(10) << d1 << " - ";
        cout << setw(17) << fixed << setprecision(10) << d2 << " = ";
        cout << setw(17) << fixed << setprecision(10) << d1 - d2 << endl;
    }
    return 0;
}

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

Z:\>CP0508
-10:-11013.2328747034 - -11013.2328747034 =      0.0000000000
 -9: -4051.5419020828 -  -4051.5419020828 =      0.0000000000
 -8: -1490.4788257895 -  -1490.4788257896 =      0.0000000000
 -7:  -548.3161232732 -   -548.3161232732 =      0.0000000000
 -6:  -201.7131573703 -   -201.7131573703 =      0.0000000000
 -5:   -74.2032105778 -    -74.2032105778 =      0.0000000000
 -4:   -27.2899171971 -    -27.2899171971 =      0.0000000000
 -3:   -10.0178749274 -    -10.0178749274 =      0.0000000000
 -2:    -3.6268604078 -     -3.6268604078 =      0.0000000000
 -1:    -1.1752011936 -     -1.1752011936 =      0.0000000000
  0:     0.0000000000 -      0.0000000000 =      0.0000000000
  1:     1.1752011936 -      1.1752011936 =     -0.0000000000
  2:     3.6268604078 -      3.6268604078 =     -0.0000000000
  3:    10.0178749274 -     10.0178749274 =     -0.0000000000
  4:    27.2899171971 -     27.2899171971 =     -0.0000000000
  5:    74.2032105778 -     74.2032105778 =     -0.0000000000
  6:   201.7131573703 -    201.7131573703 =     -0.0000000000
  7:   548.3161232732 -    548.3161232732 =     -0.0000000000
  8:  1490.4788257895 -   1490.4788257896 =     -0.0000000000
  9:  4051.5419020828 -   4051.5419020828 =     -0.0000000000
 10: 11013.2328747034 -  11013.2328747034 =     -0.0000000000

Objective-C

#import <Foundation/Foundation.h>

double mySinh(double x, int n, double numerator, double denominator, double y);

int main()
{
    int x;
    for (x = -10; x <= 10; x++)
    {
        // 自作の双曲線正弦関数
        double d1 = mySinh(x, 1, x, 1.0, x);
        // 標準の双曲線正弦関数
        double d2 = sinh(x);
        // 標準関数との差異
        printf("%+03d : %+017.10f - %+017.10f = %+13.10f\n", x, d1, d2, d1 - d2);
    }
    return 0;
}

// 自作の双曲線正弦関数
double mySinh(double x, int n, double numerator, double denominator, double y)
{
    int m       = 2 * n;
    denominator = denominator * (m + 1) * m;
    numerator   = numerator   * x * x;
    double a    = numerator / denominator;
    // 十分な精度になったら処理を抜ける
    if (fabs(a) <= 0.00000000001) 
        return y;
    else
        return y + mySinh(x, ++n, numerator, denominator, 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 
-10 : -011013.2328747034 - -11013.2328747034 = +0.0000000000
-09 : -004051.5419020828 - -04051.5419020828 = +0.0000000000
-08 : -001490.4788257895 - -01490.4788257896 = +0.0000000000
-07 : -000548.3161232732 - -00548.3161232732 = +0.0000000000
-06 : -000201.7131573703 - -00201.7131573703 = +0.0000000000
-05 : -000074.2032105778 - -00074.2032105778 = +0.0000000000
-04 : -000027.2899171971 - -00027.2899171971 = +0.0000000000
-03 : -000010.0178749274 - -00010.0178749274 = +0.0000000000
-02 : -000003.6268604078 - -00003.6268604078 = +0.0000000000
-01 : -000001.1752011936 - -00001.1752011936 = +0.0000000000
+00 : +000000.0000000000 - +00000.0000000000 = +0.0000000000
+01 : +000001.1752011936 - +00001.1752011936 = -0.0000000000
+02 : +000003.6268604078 - +00003.6268604078 = -0.0000000000
+03 : +000010.0178749274 - +00010.0178749274 = -0.0000000000
+04 : +000027.2899171971 - +00027.2899171971 = -0.0000000000
+05 : +000074.2032105778 - +00074.2032105778 = -0.0000000000
+06 : +000201.7131573703 - +00201.7131573703 = -0.0000000000
+07 : +000548.3161232732 - +00548.3161232732 = -0.0000000000
+08 : +001490.4788257895 - +01490.4788257896 = -0.0000000000
+09 : +004051.5419020828 - +04051.5419020828 = -0.0000000000
+10 : +011013.2328747034 - +11013.2328747034 = -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.

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

// 自作の双曲線正弦関数
def mySinh(x:Double, n:Int, 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 (Math.abs(a) <= 0.00000000001) 
        y
    else
        y + mySinh(x, n + 1, num, denom, a)
}
(0 to 20).
    map(_ - 10).
    foreach { x =>
        // 自作の双曲線正弦関数
        val d1 = mySinh(x, 1, x, 1.0, x)
        // 標準の双曲線正弦関数
        val d2 = Math.sinh(x)
        // 標準関数との差異
        System.out.println("%3d : %17.10f - %17.10f = %13.10f".format(x, d1, d2, d1 - d2));
    }
-10 : -11013.2328747034 - -11013.2328747034 =  0.0000000000
 -9 :  -4051.5419020828 -  -4051.5419020828 =  0.0000000000
 -8 :  -1490.4788257895 -  -1490.4788257896 =  0.0000000000
 -7 :   -548.3161232732 -   -548.3161232732 =  0.0000000000
 -6 :   -201.7131573703 -   -201.7131573703 =  0.0000000000
 -5 :    -74.2032105778 -    -74.2032105778 =  0.0000000000
 -4 :    -27.2899171971 -    -27.2899171971 =  0.0000000000
 -3 :    -10.0178749274 -    -10.0178749274 =  0.0000000000
 -2 :     -3.6268604078 -     -3.6268604078 =  0.0000000000
 -1 :     -1.1752011936 -     -1.1752011936 =  0.0000000000
  0 :      0.0000000000 -      0.0000000000 =  0.0000000000
  1 :      1.1752011936 -      1.1752011936 = -0.0000000000
  2 :      3.6268604078 -      3.6268604078 = -0.0000000000
  3 :     10.0178749274 -     10.0178749274 = -0.0000000000
  4 :     27.2899171971 -     27.2899171971 = -0.0000000000
  5 :     74.2032105778 -     74.2032105778 = -0.0000000000
  6 :    201.7131573703 -    201.7131573703 = -0.0000000000
  7 :    548.3161232732 -    548.3161232732 = -0.0000000000
  8 :   1490.4788257895 -   1490.4788257896 = -0.0000000000
  9 :   4051.5419020828 -   4051.5419020828 = -0.0000000000
 10 :  11013.2328747034 -  11013.2328747034 = -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;;

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

// 自作の双曲線正弦関数
let rec mySinh (x:double) (n:int) (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 abs(a) <= 0.00000000001 then
        y
    else
        y + (mySinh x (n + 1) num denom a)
[0..20]
|> List.map((-) 10)
|> List.iter
    (fun i -> 
        // 自作の双曲線正弦関数
        let x  = (double i)
        let d1 = (mySinh x 1 x 1.0 x)
        // 標準の双曲線正弦関数
        let d2 = System.Math.Sinh(x)
        // 標準関数との差異
        printfn "%3d : %17.10f - %17.10f = %13.10f" i d1 d2 (d1 - d2)
    )
 10 :  11013.2328747034 -  11013.2328747034 =  0.0000000000
  9 :   4051.5419020828 -   4051.5419020828 =  0.0000000000
  8 :   1490.4788257895 -   1490.4788257896 =  0.0000000000
  7 :    548.3161232732 -    548.3161232732 =  0.0000000000
  6 :    201.7131573703 -    201.7131573703 =  0.0000000000
  5 :     74.2032105778 -     74.2032105778 =  0.0000000000
  4 :     27.2899171971 -     27.2899171971 =  0.0000000000
  3 :     10.0178749274 -     10.0178749274 =  0.0000000000
  2 :      3.6268604078 -      3.6268604078 =  0.0000000000
  1 :      1.1752011936 -      1.1752011936 =  0.0000000000
  0 :      0.0000000000 -      0.0000000000 =  0.0000000000
 -1 :     -1.1752011936 -     -1.1752011936 =  0.0000000000
 -2 :     -3.6268604078 -     -3.6268604078 =  0.0000000000
 -3 :    -10.0178749274 -    -10.0178749274 =  0.0000000000
 -4 :    -27.2899171971 -    -27.2899171971 =  0.0000000000
 -5 :    -74.2032105778 -    -74.2032105778 =  0.0000000000
 -6 :   -201.7131573703 -   -201.7131573703 =  0.0000000000
 -7 :   -548.3161232732 -   -548.3161232732 =  0.0000000000
 -8 :  -1490.4788257895 -  -1490.4788257896 =  0.0000000000
 -9 :  -4051.5419020828 -  -4051.5419020828 =  0.0000000000
-10 : -11013.2328747034 - -11013.2328747034 =  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

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

;自作の双曲線正弦関数
(defn mySinh [x n numerator denominator y]
    (def m     (* 2 n))
    (def denom (* denominator (* (+ m 1) m)))
    (def nume  (* numerator (* x x)))
    (def a     (/ nume denom))
    ;十分な精度になったら処理を抜ける
    (if (<= (. Math abs a) 0.00000000001)
        y
        (+ y (mySinh x (+ n 1) nume denom a))))
(doseq
    [i  (map #(- % 10)
        (range 0 21))]
    (do
        (def x  (double i))
        ;自作の双曲線正弦関数
        (def d1 (mySinh x 1 x 1.0 x))
        ;標準の双曲線正弦関数
        (def d2 (. Math sinh x))
        ;標準関数との差異
        (println (format "%3d : %17.10f - %17.10f = %13.10f" i d1 d2 (- d1 d2)))))
-10 : -11013.2328747034 - -11013.2328747034 =  0.0000000000
 -9 :  -4051.5419020828 -  -4051.5419020828 =  0.0000000000
 -8 :  -1490.4788257895 -  -1490.4788257896 =  0.0000000000
 -7 :   -548.3161232732 -   -548.3161232732 =  0.0000000000
 -6 :   -201.7131573703 -   -201.7131573703 =  0.0000000000
 -5 :    -74.2032105778 -    -74.2032105778 =  0.0000000000
 -4 :    -27.2899171971 -    -27.2899171971 =  0.0000000000
 -3 :    -10.0178749274 -    -10.0178749274 =  0.0000000000
 -2 :     -3.6268604078 -     -3.6268604078 =  0.0000000000
 -1 :     -1.1752011936 -     -1.1752011936 =  0.0000000000
  0 :      0.0000000000 -      0.0000000000 =  0.0000000000
  1 :      1.1752011936 -      1.1752011936 = -0.0000000000
  2 :      3.6268604078 -      3.6268604078 = -0.0000000000
  3 :     10.0178749274 -     10.0178749274 = -0.0000000000
  4 :     27.2899171971 -     27.2899171971 = -0.0000000000
  5 :     74.2032105778 -     74.2032105778 = -0.0000000000
  6 :    201.7131573703 -    201.7131573703 = -0.0000000000
  7 :    548.3161232732 -    548.3161232732 = -0.0000000000
  8 :   1490.4788257895 -   1490.4788257896 = -0.0000000000
  9 :   4051.5419020828 -   4051.5419020828 = -0.0000000000
 10 :  11013.2328747034 -  11013.2328747034 = -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.

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

-- 自作の双曲線正弦関数
mySinh::Double->Int->Double->Double->Double->Double
mySinh x n numerator denominator y =
let
    m     = 2 * n
    denom = denominator * (fromIntegral (m + 1)) * (fromIntegral m)
    num   = numerator   * x * x
    a     = num / denom
in 
    -- 十分な精度になったら処理を抜ける
    if abs(a) <= 0.00000000001 then
        y
    else
        y + (mySinh x (n + 1) num denom a)
import Text.Printf
import Control.Monad

forM_ (
    map ((-) 10) $ 
    [0..20::Int]
) $ \i -> do
    -- 自作の双曲線正弦関数
    let x  = (fromIntegral i)
    let d1 = (mySinh x 1 x 1.0 x)
    -- 標準の双曲線正弦関数
    let d2 = sinh(x)
    -- 標準関数との差異
    printf "%3d : %17.10f - %17.10f = %13.10f\n" i d1 d2 (d1- d2)
 10 :  11013.2328747034 -  11013.2328747034 = -0.0000000000
  9 :   4051.5419020828 -   4051.5419020828 = -0.0000000000
  8 :   1490.4788257895 -   1490.4788257896 = -0.0000000000
  7 :    548.3161232732 -    548.3161232732 = -0.0000000000
  6 :    201.7131573703 -    201.7131573703 = -0.0000000000
  5 :     74.2032105778 -     74.2032105778 = -0.0000000000
  4 :     27.2899171971 -     27.2899171971 = -0.0000000000
  3 :     10.0178749274 -     10.0178749274 = -0.0000000000
  2 :      3.6268604078 -      3.6268604078 = -0.0000000000
  1 :      1.1752011936 -      1.1752011936 = -0.0000000000
  0 :      0.0000000000 -      0.0000000000 =  0.0000000000
 -1 :     -1.1752011936 -     -1.1752011936 =  0.0000000000
 -2 :     -3.6268604078 -     -3.6268604078 =  0.0000000000
 -3 :    -10.0178749274 -    -10.0178749274 =  0.0000000000
 -4 :    -27.2899171971 -    -27.2899171971 =  0.0000000000
 -5 :    -74.2032105778 -    -74.2032105778 =  0.0000000000
 -6 :   -201.7131573703 -   -201.7131573703 =  0.0000000000
 -7 :   -548.3161232732 -   -548.3161232732 =  0.0000000000
 -8 :  -1490.4788257895 -  -1490.4788257896 =  0.0000000000
 -9 :  -4051.5419020828 -  -4051.5419020828 =  0.0000000000
-10 : -11013.2328747034 - -11013.2328747034 =  0.0000000000

終了

Prelude> :quit
Leaving GHCi.
inserted by FC2 system