さまざまな言語で数値計算
Only Do What Only You Can Do
正接関数 (連分数展開)
連分数展開で $ \tan x $ を求めます.
VBScript
Option Explicit Const PI = 3.14159265359 Dim i For i = 0 To 180 Step 15 If (i Mod 180 <> 0) Then Dim degree: degree = i - 90 Dim radian: radian = degree * PI / 180.0 Dim x2: x2 = radian * radian '自作の正接関数 Dim d1: d1 = myTan(radian, x2, 15, 0.0) '15:必要な精度が得られる十分大きな奇数 '標準の正接関数 Dim d2: d2 = Tan(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 myTan(ByVal x, ByVal x2, ByVal n, ByVal t) t = x2 / (n - t) n = n - 2 If (n <= 1) Then myTan = x / (1 - t) Else myTan = myTan(x, x2, n, t) End If End Function
Z:\>cscript //nologo 0503.vbs -75 : -3.7320508076 - -3.7320508076 = 0.0000000000 -60 : -1.7320508076 - -1.7320508076 = 0.0000000000 -45 : -1.0000000000 - -1.0000000000 = 0.0000000000 -30 : -0.5773502692 - -0.5773502692 = 0.0000000000 -15 : -0.2679491924 - -0.2679491924 = 0.0000000000 0 : 0.0000000000 - 0.0000000000 = 0.0000000000 15 : 0.2679491924 - 0.2679491924 = 0.0000000000 30 : 0.5773502692 - 0.5773502692 = 0.0000000000 45 : 1.0000000000 - 1.0000000000 = -0.0000000000 60 : 1.7320508076 - 1.7320508076 = -0.0000000000 75 : 3.7320508076 - 3.7320508076 = -0.0000000000
JScript
for (var degree = -90; degree <= 90; degree += 15) { if ((degree + 90) % 180 != 0) { var radian = degree * Math.PI / 180.0; var x2 = radian * radian; // 自作の正接関数 var d1 = myTan(radian, x2, 15, 0.0); // 15:必要な精度が得られる十分大きな奇数 // 標準の正接関数 var d2 = Math.tan(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 myTan(x, x2, n, t) { t = x2 / (n - t); n -= 2; if (n <= 1) return x / (1 - t); else return myTan(x, x2, n, t); }
Z:\>cscript //nologo 0503.js -75 : -3.7320508076 - -3.7320508076 = 0.0000000000 -60 : -1.7320508076 - -1.7320508076 = 0.0000000000 -45 : -1.0000000000 - -1.0000000000 = 0.0000000000 -30 : -0.5773502692 - -0.5773502692 = 0.0000000000 -15 : -0.2679491924 - -0.2679491924 = 0.0000000000 0 : 0.0000000000 - 0.0000000000 = 0.0000000000 15 : 0.2679491924 - 0.2679491924 = -0.0000000000 30 : 0.5773502692 - 0.5773502692 = 0.0000000000 45 : 1.0000000000 - 1.0000000000 = -0.0000000000 60 : 1.7320508076 - 1.7320508076 = -0.0000000000 75 : 3.7320508076 - 3.7320508076 = -0.0000000000
PowerShell
# 自作の正接関数 function myTan($x, $x2, $n, $t) { $t = $x2 / ($n - $t) $n -= 2 if ($n -le 1) { $x / (1 - $t) } else { myTan $x $x2 $n $t } } foreach ($i in 0..12) { if (($i * 15) % 180 -ne 0) { $degree = $i * 15 - 90 $radian = $degree * [Math]::PI / 180.0 $x2 = $radian * $radian # 自作の正接関数 $d1 = myTan $radian $x2 15 0.0 # 15:必要な精度が得られる十分大きな奇数 # 標準の正接関数 $d2 = [Math]::Tan($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 0503.ps1 -75 : -3.7320508076 - -3.7320508076 = 0.0000000000 -60 : -1.7320508076 - -1.7320508076 = 0.0000000000 -45 : -1.0000000000 - -1.0000000000 = 0.0000000000 -30 : -0.5773502692 - -0.5773502692 = 0.0000000000 -15 : -0.2679491924 - -0.2679491924 = 0.0000000000 0 : 0.0000000000 - 0.0000000000 = 0.0000000000 15 : 0.2679491924 - 0.2679491924 = 0.0000000000 30 : 0.5773502692 - 0.5773502692 = 0.0000000000 45 : 1.0000000000 - 1.0000000000 = 0.0000000000 60 : 1.7320508076 - 1.7320508076 = 0.0000000000 75 : 3.7320508076 - 3.7320508076 = 0.0000000000
Perl
use Math::Trig qw'deg2rad tan'; # 自作の正接関数 sub myTan { my ($x, $x2, $n, $t) = @_; $t = $x2 / ($n - $t); $n -= 2; if ($n <= 1) { $x / (1 - $t); } else { myTan($x, $x2, $n, $t); } } for $i (0..12) { if (($i * 15) % 180 != 0) { $degree = $i * 15 - 90; $radian = deg2rad($degree); $x2 = $radian * $radian; # 自作の正接関数 $d1 = myTan($radian, $x2, 15, 0.0); # 15:必要な精度が得られる十分大きな奇数 # 標準の正接関数 $d2 = tan($radian); # 標準関数との差異 printf("%3d : %13.10f - %13.10f = %13.10f\n", $degree, $d1, $d2, $d1 - $d2); } }
Z:\>perl 0503.pl -75 : -3.7320508076 - -3.7320508076 = 0.0000000000 -60 : -1.7320508076 - -1.7320508076 = 0.0000000000 -45 : -1.0000000000 - -1.0000000000 = 0.0000000000 -30 : -0.5773502692 - -0.5773502692 = -0.0000000000 -15 : -0.2679491924 - -0.2679491924 = 0.0000000000 0 : 0.0000000000 - 0.0000000000 = 0.0000000000 15 : 0.2679491924 - 0.2679491924 = -0.0000000000 30 : 0.5773502692 - 0.5773502692 = 0.0000000000 45 : 1.0000000000 - 1.0000000000 = -0.0000000000 60 : 1.7320508076 - 1.7320508076 = -0.0000000000 75 : 3.7320508076 - 3.7320508076 = -0.0000000000
PHP
<?php # 自作の正接関数 function myTan($x, $x2, $n, $t) { $t = $x2 / ($n - $t); $n -= 2; if ($n <= 1) return $x / (1 - $t); else return myTan($x, $x2, $n, $t); } foreach (range(0, 12) as $i) { if (($i * 15) % 180 != 0) { $degree = $i * 15 - 90; $radian = deg2rad($degree); $x2 = $radian * $radian; # 自作の正接関数 $d1 = myTan($radian, $x2, 15, 0.0); # 15:必要な精度が得られる十分大きな奇数 # 標準の正接関数 $d2 = tan($radian); # 標準関数との差異 printf("%3d : %13.10f - %13.10f = %13.10f\n", $degree, $d1, $d2, $d1 - $d2); } } ?>
Z:\>php 0503.php -75 : -3.7320508076 - -3.7320508076 = 0.0000000000 -60 : -1.7320508076 - -1.7320508076 = 0.0000000000 -45 : -1.0000000000 - -1.0000000000 = 0.0000000000 -30 : -0.5773502692 - -0.5773502692 = 0.0000000000 -15 : -0.2679491924 - -0.2679491924 = 0.0000000000 0 : 0.0000000000 - 0.0000000000 = 0.0000000000 15 : 0.2679491924 - 0.2679491924 = -0.0000000000 30 : 0.5773502692 - 0.5773502692 = 0.0000000000 45 : 1.0000000000 - 1.0000000000 = -0.0000000000 60 : 1.7320508076 - 1.7320508076 = -0.0000000000 75 : 3.7320508076 - 3.7320508076 = -0.0000000000
Python
# coding: Shift_JIS import math # 自作の正接関数 def myTan(x, x2, n, t): t = x2 / (n - t) n -= 2 if (n <= 1): return x / (1 - t) else: return myTan(x, x2, n, t) for i in range(0, 13): if i * 15 % 180 != 0: degree = i * 15 - 90 radian = math.radians(degree) x2 = radian * radian # 自作の正接関数 d1 = myTan(radian, x2, 15, 0.0) # 15:必要な精度が得られる十分大きな奇数 # 標準の正接関数 d2 = math.tan(radian) # 標準関数との差異 print "%3d : %13.10f - %13.10f = %13.10f" % (degree, d1, d2, d1 - d2)
Z:\>python 0503.py -75 : -3.7320508076 - -3.7320508076 = 0.0000000000 -60 : -1.7320508076 - -1.7320508076 = 0.0000000000 -45 : -1.0000000000 - -1.0000000000 = 0.0000000000 -30 : -0.5773502692 - -0.5773502692 = 0.0000000000 -15 : -0.2679491924 - -0.2679491924 = 0.0000000000 0 : 0.0000000000 - 0.0000000000 = 0.0000000000 15 : 0.2679491924 - 0.2679491924 = -0.0000000000 30 : 0.5773502692 - 0.5773502692 = 0.0000000000 45 : 1.0000000000 - 1.0000000000 = -0.0000000000 60 : 1.7320508076 - 1.7320508076 = -0.0000000000 75 : 3.7320508076 - 3.7320508076 = -0.0000000000
Ruby
# 自作の正接関数 def myTan(x, x2, n, t) t = x2 / (n - t) n -= 2 if (n <= 1) x / (1 - t) else myTan(x, x2, n, t) end end (0..12).each do |i| if i * 15 % 180 != 0 degree = i * 15 - 90 radian = degree * Math::PI / 180 x2 = radian * radian # 自作の正接関数 d1 = myTan(radian, x2, 15, 0.0) # 15:必要な精度が得られる十分大きな奇数 # 標準の正接関数 d2 = Math.tan(radian) # 標準関数との差異 printf("%3d : %13.10f - %13.10f = %13.10f\n", degree, d1, d2, d1 - d2) end end
Z:\>ruby 0503.rb -75 : -3.7320508076 - -3.7320508076 = 0.0000000000 -60 : -1.7320508076 - -1.7320508076 = 0.0000000000 -45 : -1.0000000000 - -1.0000000000 = 0.0000000000 -30 : -0.5773502692 - -0.5773502692 = 0.0000000000 -15 : -0.2679491924 - -0.2679491924 = 0.0000000000 0 : 0.0000000000 - 0.0000000000 = 0.0000000000 15 : 0.2679491924 - 0.2679491924 = -0.0000000000 30 : 0.5773502692 - 0.5773502692 = 0.0000000000 45 : 1.0000000000 - 1.0000000000 = -0.0000000000 60 : 1.7320508076 - 1.7320508076 = -0.0000000000 75 : 3.7320508076 - 3.7320508076 = -0.0000000000
Groovy
Pascal
Program Pas0503(arg); {$MODE delphi} uses SysUtils, Math; // 自作の正接関数 function myTan(x:Double; x2:Double; n:Integer; t:Double):Double; begin t := x2 / (n - t); n := n - 2; if (n <= 1) then result := x / (1 - t) else result := myTan(x, x2, n, t); end; var i: Integer; degree: Integer; radian: Double; x2: Double; d1: Double; d2: Double; begin for i := 0 to 12 do begin if (i * 15 mod 180 <> 0) then begin degree := i * 15 - 90; radian := DegToRad(degree); // 自作の正接関数 x2 := radian * radian; d1 := myTan(radian, x2, 15, 0.0); // 15:必要な精度が得られる十分大きな奇数 // 標準の正接関数 d2 := Tan(radian); // 標準関数との差異 writeln(format('%3d : %13.10f - %13.10f = %13.10f', [degree, d1, d2, d1 - d2])); end; end; end.
Z:\>fpc Pas0503.pp -v0 Free Pascal Compiler version 2.6.2 [2013/02/12] for i386 Copyright (c) 1993-2012 by Florian Klaempfl and others Z:\>Pas0503 -75 : -3.7320508076 - -3.7320508076 = 0.0000000000 -60 : -1.7320508076 - -1.7320508076 = 0.0000000000 -45 : -1.0000000000 - -1.0000000000 = 0.0000000000 -30 : -0.5773502692 - -0.5773502692 = 0.0000000000 -15 : -0.2679491924 - -0.2679491924 = 0.0000000000 0 : 0.0000000000 - 0.0000000000 = 0.0000000000 15 : 0.2679491924 - 0.2679491924 = 0.0000000000 30 : 0.5773502692 - 0.5773502692 = 0.0000000000 45 : 1.0000000000 - 1.0000000000 = 0.0000000000 60 : 1.7320508076 - 1.7320508076 = 0.0000000000 75 : 3.7320508076 - 3.7320508076 = 0.0000000000
Ada
VB.NET
Module VB0503 Public Sub Main() For i As Integer = 0 To 180 Step 15 If (i Mod 180 <> 0) Then Dim degree As Integer = i - 90 Dim radian As Double = degree * Math.PI / 180.0 Dim x2 As Double = radian * radian '自作の正接関数 Dim d1 As Double = myTan(radian, x2, 15, 0.0) '15:必要な精度が得られる十分大きな奇数 '標準の正接関数 Dim d2 As Double = Math.Tan(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 myTan(ByVal x As Double, ByVal x2 As Double, ByVal n As Integer, ByVal t As Double) As Double t = x2 / (n - t) n -= 2 If (n <= 1) Then Return x / (1 - t) Else Return myTan(x, x2, n, t) End If End Function End Module
Z:\>vbc -nologo VB0503.vb Z:\>VB0503 -75 : -3.7320508076 - -3.7320508076 = 0.0000000000 -60 : -1.7320508076 - -1.7320508076 = 0.0000000000 -45 : -1.0000000000 - -1.0000000000 = 0.0000000000 -30 : -0.5773502692 - -0.5773502692 = 0.0000000000 -15 : -0.2679491924 - -0.2679491924 = 0.0000000000 0 : 0.0000000000 - 0.0000000000 = 0.0000000000 15 : 0.2679491924 - 0.2679491924 = 0.0000000000 30 : 0.5773502692 - 0.5773502692 = 0.0000000000 45 : 1.0000000000 - 1.0000000000 = 0.0000000000 60 : 1.7320508076 - 1.7320508076 = 0.0000000000 75 : 3.7320508076 - 3.7320508076 = 0.0000000000
C#
using System; public class CS0503 { public static void Main() { for (int degree = -90; degree <= 90; degree += 15) { if ((degree + 90) % 180 != 0) { double radian = degree * Math.PI / 180.0; double x2 = radian * radian; // 自作の正接関数 double d1 = myTan(radian, x2, 15, 0.0); // 15:必要な精度が得られる十分大きな奇数 // 標準の正接関数 double d2 = Math.Tan(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 myTan(double x, double x2, int n, double t) { t = x2 / (n - t); n -= 2; if (n <= 1) return x / (1 - t); else return myTan(x, x2, n, t); } }
Z:\>csc -nologo CS0503.cs Z:\>CS0503 -75 : -3.7320508076 - -3.7320508076 = 0.0000000000 -60 : -1.7320508076 - -1.7320508076 = 0.0000000000 -45 : -1.0000000000 - -1.0000000000 = 0.0000000000 -30 : -0.5773502692 - -0.5773502692 = 0.0000000000 -15 : -0.2679491924 - -0.2679491924 = 0.0000000000 0 : 0.0000000000 - 0.0000000000 = 0.0000000000 15 : 0.2679491924 - 0.2679491924 = 0.0000000000 30 : 0.5773502692 - 0.5773502692 = 0.0000000000 45 : 1.0000000000 - 1.0000000000 = 0.0000000000 60 : 1.7320508076 - 1.7320508076 = 0.0000000000 75 : 3.7320508076 - 3.7320508076 = 0.0000000000
Java
public class Java0503 { public static void main(String []args) { for (int degree = -90; degree <= 90; degree += 15) { if ((degree + 90) % 180 != 0) { double radian = Math.toRadians(degree); double x2 = radian * radian; // 自作の正接関数 double d1 = myTan(radian, x2, 15, 0.0); // 15:必要な精度が得られる十分大きな奇数 // 標準の正接関数 double d2 = Math.tan(radian); // 標準関数との差異 System.out.println(String.format("%3d : %13.10f - %13.10f = %13.10f", degree, d1, d2, d1 - d2)); } } } // 自作の正接関数 private static double myTan(double x, double x2, int n, double t) { t = x2 / (n - t); n -= 2; if (n <= 1) return x / (1 - t); else return myTan(x, x2, n, t); } }
Z:\>javac Java0503.java Z:\Z:\>java Java0503 -75 : -3.7320508076 - -3.7320508076 = 0.0000000000 -60 : -1.7320508076 - -1.7320508076 = 0.0000000000 -45 : -1.0000000000 - -1.0000000000 = 0.0000000000 -30 : -0.5773502692 - -0.5773502692 = 0.0000000000 -15 : -0.2679491924 - -0.2679491924 = 0.0000000000 0 : 0.0000000000 - 0.0000000000 = 0.0000000000 15 : 0.2679491924 - 0.2679491924 = -0.0000000000 30 : 0.5773502692 - 0.5773502692 = 0.0000000000 45 : 1.0000000000 - 1.0000000000 = -0.0000000000 60 : 1.7320508076 - 1.7320508076 = -0.0000000000 75 : 3.7320508076 - 3.7320508076 = -0.0000000000
C++
#include <iostream> #include <iomanip> #include <math> using namespace std; double myTan(double x, double x2, int n, double t); int main() { for (int degree = -90; degree <= 90; degree += 15) { if ((degree + 90) % 180 != 0) { double radian = degree * M_PI / 180.0; double x2 = radian * radian; // 自作の正接関数 double d1 = myTan(radian, x2, 15, 0.0); // 15:必要な精度が得られる十分大きな奇数 // 標準の正接関数 double d2 = tan(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 myTan(double x, double x2, int n, double t) { t = x2 / (n - t); n -= 2; if (n <= 1) return x / (1 - t); else return myTan(x, x2, n, t); }
Z:\>bcc32 CP0503.cpp Borland C++ 5.5.1 for Win32 Copyright (c) 1993, 2000 Borland CP0503.cpp: Turbo Incremental Link 5.00 Copyright (c) 1997, 2000 Borland Z:\>CP0503 -75: -3.7320508076 - -3.7320508076 = 0.0000000000 -60: -1.7320508076 - -1.7320508076 = 0.0000000000 -45: -1.0000000000 - -1.0000000000 = 0.0000000000 -30: -0.5773502692 - -0.5773502692 = 0.0000000000 -15: -0.2679491924 - -0.2679491924 = 0.0000000000 0: 0.0000000000 - 0.0000000000 = 0.0000000000 15: 0.2679491924 - 0.2679491924 = 0.0000000000 30: 0.5773502692 - 0.5773502692 = 0.0000000000 45: 1.0000000000 - 1.0000000000 = -0.0000000000 60: 1.7320508076 - 1.7320508076 = -0.0000000000 75: 3.7320508076 - 3.7320508076 = -0.0000000000
Objective-C
#import <Foundation/Foundation.h> double myTan(double x, double x2, int n, double t); int main() { int degree; for (degree = -90; degree <= 90; degree += 15) { if ((degree + 90) % 180 != 0) { double radian = degree * M_PI / 180.0; double x2 = radian * radian; // 自作の正接関数 double d1 = myTan(radian, x2, 15, 0.0); // 15:必要な精度が得られる十分大きな奇数 // 標準の正接関数 double d2 = tan(radian); // 標準関数との差異 printf("%+03d : %+13.10f - %+13.10f = %+13.10f\n", degree, d1, d2, d1 - d2); } } return 0; } // 自作の正接関数 double myTan(double x, double x2, int n, double t) { t = x2 / (n - t); n -= 2; if (n <= 1) return x / (1 - t); else return myTan(x, x2, n, t); }
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 -75 : -3.7320508076 - -3.7320508076 = +0.0000000000 -60 : -1.7320508076 - -1.7320508076 = +0.0000000000 -45 : -1.0000000000 - -1.0000000000 = +0.0000000000 -30 : -0.5773502692 - -0.5773502692 = +0.0000000000 -15 : -0.2679491924 - -0.2679491924 = +0.0000000000 +00 : +0.0000000000 - +0.0000000000 = +0.0000000000 +15 : +0.2679491924 - +0.2679491924 = -0.0000000000 +30 : +0.5773502692 - +0.5773502692 = +0.0000000000 +45 : +1.0000000000 - +1.0000000000 = -0.0000000000 +60 : +1.7320508076 - +1.7320508076 = -0.0000000000 +75 : +3.7320508076 - +3.7320508076 = -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.
連分数展開で tan x を求める
// 自作の正接関数 def myTan(x:Double, x2:Double, n:Int, t:Double):Double = { val denom = x2 / (n - t) val num = n - 2 if (num <= 1) x / (1 - denom) else myTan(x, x2, num, denom) }
(0 to 12). map(_ * 15). filter(n => n % 180 != 0). map(_ - 90). foreach { degree => val radian = Math.toRadians(degree) val x2 = radian * radian // 自作の正接関数 val d1 = myTan(radian, x2, 15, 0.0) // 15:必要な精度が得られる十分大きな奇数 // 標準の正接関数 val d2 = Math.tan(radian) // 標準関数との差異 System.out.println("%3d : %13.10f - %13.10f = %13.10f".format(degree, d1, d2, d1 - d2)); }
-75 : -3.7320508076 - -3.7320508076 = 0.0000000000 -60 : -1.7320508076 - -1.7320508076 = 0.0000000000 -45 : -1.0000000000 - -1.0000000000 = 0.0000000000 -30 : -0.5773502692 - -0.5773502692 = 0.0000000000 -15 : -0.2679491924 - -0.2679491924 = 0.0000000000 0 : 0.0000000000 - 0.0000000000 = 0.0000000000 15 : 0.2679491924 - 0.2679491924 = -0.0000000000 30 : 0.5773502692 - 0.5773502692 = 0.0000000000 45 : 1.0000000000 - 1.0000000000 = -0.0000000000 60 : 1.7320508076 - 1.7320508076 = -0.0000000000 75 : 3.7320508076 - 3.7320508076 = -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;;
連分数展開で tan x を求める
// 自作の正接関数 let rec myTan (x:double) (x2:double) (n:int) (t:double):double = let denom = x2 / ((double n) - t) let num = n - 2 if num <= 1 then x / (1.0 - denom) else myTan x x2 num denom
[0..12] |> List.map ((*) 15) |> List.filter (fun n -> n % 180 <> 0) |> List.map ((-) 90) |> List.iter (fun degree -> let radian = (double degree) * System.Math.PI / 180.0 let x2 = radian * radian // 自作の正接関数 let d1 = (myTan radian x2 15 0.0) // 15:必要な精度が得られる十分大きな奇数 // 標準の正接関数 let d2 = System.Math.Tan(radian) // 標準関数との差異 printfn "%3d : %13.10f - %13.10f = %13.10f" degree d1 d2 (d1 - d2) )
75 : 3.7320508076 - 3.7320508076 = 0.0000000000 60 : 1.7320508076 - 1.7320508076 = 0.0000000000 45 : 1.0000000000 - 1.0000000000 = 0.0000000000 30 : 0.5773502692 - 0.5773502692 = 0.0000000000 15 : 0.2679491924 - 0.2679491924 = 0.0000000000 0 : 0.0000000000 - 0.0000000000 = 0.0000000000 -15 : -0.2679491924 - -0.2679491924 = 0.0000000000 -30 : -0.5773502692 - -0.5773502692 = 0.0000000000 -45 : -1.0000000000 - -1.0000000000 = 0.0000000000 -60 : -1.7320508076 - -1.7320508076 = 0.0000000000 -75 : -3.7320508076 - -3.7320508076 = 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
連分数展開で tan x を求める
;自作の正接関数 (defn myTan [x x2 n t] (def denom (/ x2 (- n t))) (def nume (- n 2)) (if (<= nume 1) (/ x (- 1 denom)) (myTan x x2 nume denom)))
(doseq [degree (map #(- % 90) (filter #(not (zero? (mod % 180))) (map #(* % 15) (range 0 12))))] (do (def radian (/ (* degree (. Math PI)) 180.0)) (def x2 (* radian radian)) ;自作の正接関数 (def d1 (myTan radian x2 15 0)) ;標準の正接関数 (def d2 (. Math tan radian)) ;標準関数との差異 (println (format "%3d : %13.10f - %13.10f = %13.10f" degree d1 d2 (- d1 d2)))))
-75 : -3.7320508076 - -3.7320508076 = 0.0000000000 -60 : -1.7320508076 - -1.7320508076 = 0.0000000000 -45 : -1.0000000000 - -1.0000000000 = 0.0000000000 -30 : -0.5773502692 - -0.5773502692 = 0.0000000000 -15 : -0.2679491924 - -0.2679491924 = 0.0000000000 0 : 0.0000000000 - 0.0000000000 = 0.0000000000 15 : 0.2679491924 - 0.2679491924 = -0.0000000000 30 : 0.5773502692 - 0.5773502692 = 0.0000000000 45 : 1.0000000000 - 1.0000000000 = -0.0000000000 60 : 1.7320508076 - 1.7320508076 = -0.0000000000 75 : 3.7320508076 - 3.7320508076 = -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.
連分数展開で tan x を求める
-- 自作の正接関数 myTan::Double->Double->Int->Double->Double myTan x x2 n t = let denom = x2 / ((fromIntegral n) - t) num = n - 2 in if num <= 1 then x / (1.0 - denom) else myTan x x2 num denom
import Text.Printf import Control.Monad forM_ ( map (\n -> n - 90) $ filter (\n -> mod n 180 /= 0) $ map (* 15) $ [0..12::Int] ) $ \degree -> do let radian = (fromIntegral degree) * pi / 180.0 -- 自作の正接関数 let x2 = radian * radian let d1 = (myTan radian x2 15 0.0) -- 15:必要な精度が得られる十分大きな奇数 -- 標準の正接関数 let d2 = tan(radian) -- 標準関数との差異 printf "%3d : %13.10f - %13.10f = %13.10f\n" degree d1 d2 (d1- d2)
-75 : -3.7320508076 - -3.7320508076 = 0.0000000000 -60 : -1.7320508076 - -1.7320508076 = 0.0000000000 -45 : -1.0000000000 - -1.0000000000 = 0.0000000000 -30 : -0.5773502692 - -0.5773502692 = 0.0000000000 -15 : -0.2679491924 - -0.2679491924 = 0.0000000000 0 : 0.0000000000 - 0.0000000000 = 0.0000000000 15 : 0.2679491924 - 0.2679491924 = -0.0000000000 30 : 0.5773502692 - 0.5773502692 = 0.0000000000 45 : 1.0000000000 - 1.0000000000 = -0.0000000000 60 : 1.7320508076 - 1.7320508076 = -0.0000000000 75 : 3.7320508076 - 3.7320508076 = -0.0000000000
終了
Prelude> :quit Leaving GHCi.