0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

Swiftで三角関数を再帰関数で求める。

0
Last updated at Posted at 2022-04-16
func sin(_ x: Double) -> Double {
    if abs(x) < 1e-6 {
        return x
    }
    let a = sin(x/3)
    return 3*a - 4*a*a*a
}

func cos(_ x: Double) -> Double {
    if abs(x) < 1e-6 {
        return 1 - x*x/2
    }
    let a = cos(x/3)
    return 4*a*a*a - 3*a
}

func tan(_ x: Double) -> Double {
    if abs(x) < 1e-6 {
        return x
    }
    let a = tan(x/3)
    return (3*a-a*a*a)/(1-3*a*a)
}

print(sin(1)) // 0.8414709848079318
print(cos(1)) // 0.5402475011786687
print(tan(1)) // 1.5574077246544553

理論値:
$\sin(1) = 0.8414709848$
$\cos(1) = 0.54030230586$
$\tan(1) = 1.55740772465$

三倍角形の公式を再帰的に用いて三角関数を計算出来る。

0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?