6
6

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 数学関数まとめ

Last updated at Posted at 2020-06-07

はじめに

Swiftで数学関数とありますが、UIKitをimportすることでC言語の標準ライブラリであるmath.hが使えるようになるため、Swift独特の書き方というのは無いと思います。
Swiftからプログラミングを始めたり、Playgroundで遊びたい中高生やその指導者の参考になればと思います。
なおimport UIKitimport Foundationは割愛しております。コンパイルエラーが出たらそこを見直して下さい。
Double -> Intへの変換はDouble(変換したいInt型の数)のようにします。

乱数

print(Int.random(in: 1...100)) //1から100までの整数をランダムに出力
print(Float.random(in: 1..<10))//1以上10未満の小数をランダムに出力

絶対値

print(abs(-10)) //10
print(fabs(-9.1)) //9

切り上げ

print(ceil(2.5)) //3.0

切り捨て

print(floor(3.5))//3.0

累乗

print(pow(2,10)) //1024

平方根

print(sort(121))//11.0

立方根

print(cart(216))//6.0

三角関数

print(sin(3.14))//0.0015926529164868282
print(cos(3.14))//-0.9999987317275395
print(tan(3.14))//-0.0015926549364072232

対数関数

print(log(148.4126599508417))//4.99999663673641
print(log10(100.0))//2.0
6
6
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
6
6

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?