0
0

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 5 years have passed since last update.

AutoLISP 算術演算関数の使い方

Last updated at Posted at 2020-01-31

算術演算関数リファレンス(公式ヘルプ)
定義済み変数(公式ヘルプ)

足し算をする

(+ 3 4)
;; -> 7
(+ 1 2 3 4 5 6 7 8 9 10)
;; -> 55
(+ 2.0 3 5)
;; -> 10
(+ 1.25 3.25)
;; -> 4.5

引き算をする

(- 9 5)
;; -> 4
(- 1 2 3 4 5 6 7 8 9 10)
;; -> -53

掛け算をする

(* 2 3)
;; -> 6
(* 1 2 3 4 5)
;; -> 120

割り算をする

(/ 9 3)
;; -> 3
(/ 10 3)
;; -> 3
;; ※ データ型に注意。整数同士の計算の戻り値は整数。
(/10 3.)
;; -> 3.33333
;; ※ 3.0 は 3. と書くことができる
(/ 10 2 2)
;; ->2
(/ 10 2. 2)
;; -> 2.5

剰余を計算

(rem 10 3)
;; -> 1
(rem 10 3.3)
;; -> 0.1
(rem 3 10)
;; -> 3 (0 余り 3)

数値を1増加(インクリメント)

(1+ 5) ;;(+ 5 1)と同じ
;; -> 6
(1+ 5.5)
;; -> 6.5
(setq n 0)
(1+ n)
;; -> 1
;; ※ 戻り値は1なるが、n が 1 になっているわけではない
;; n を 1 にするには
(setq n (1+ n))
;; とする必要がある

数値を1減少(デクリメント)

(1- 5) ;;(- 5 1)と同じ
;; -> 4
(1- 5.5)
;; -> 4.5
(setq n 0)
(1- n)
;; -> -1

絶対値を計算

(abs 7)
-> 7
(abs -7)
-> 7
(abs -7.5)
-> 7.5

三角関数

サインを計算

(sin (/ pi 6)) ;; sin30°
;; -> 0.5

コサインを計算

(cos (/ pi 3)) ;; cos60°
-> 0.5

タンジェントを計算

※ AutoLISPにはタンジェント関数が用意されていないので自作する必要がある
$$ \tan\theta = \frac{\sin\theta}{\cos\theta} $$

;; タンジェント関数の定義
(defun tan (x)
    (if (not (equal 0. (cos x) 1e-10))
        (/ (sin x) (cos x))
    )
)

(tan (/ pi 4)) ;; tan45°
;; -> 1.0

アークタンジェントを計算

※ アークタンジェントは標準で用意されている

(atan 0.5)
;; -> 0.463648
(atan (/ 1 (sqrt 3)))
;; -> 0.523599 (60°)

e のべき乗を計算

e ・・・ ネイピア数、自然対数の底

;; e = 2.71828...
(exp 10)
;; -> 22026.5
(exp 2.5)
;; -> 12.1825
(exp -1)
;; -> 0.367879

べき乗を計算

(expt 2 8) ;; 2の8乗を計算
;; -> 256
(expt 2 -1)
;; -> 0
(expt 2 -1.)
;; -> 0.5

平方根の計算

(sqrt 4)
;; -> 2.0
;; 常に実数が返される
(sqrt 2)
;; -> 1.41421
(sqrt 1)
;; -> 1.0

実数の小数点以下を切り捨てる

※四捨五入ではない

(fix 2.5)
;; -> 2
(fix pi)
;; -> 3

整数を実数に変換

(float 2)
;; -> 2.0
(/ 10 (float 3))
;; -> 3.33333
(float (/ 10 3))
;; -> 3.0

最大公約数の計算

(gcd 5 2)
;; -> 1
(gcd 54 81)
;; -> 27

自然対数の計算

自然対数 ・・・ eを底とする対数

(log 10)
;; -> 2.30259

ビット計算

ビット方式のブール演算

(boole 1 12 5)
;; -> 3
(boole 1 55 4135)
;; -> 39

ビット方式の AND

(logand 2 12)
;; -> 0
(logand 2 34)
;; -> 2

ビット方式の OR

(logior 1 4)
;; -> 5
(logior 1 2 5)
;; -> 7

ビットシフト

(lsh 1 3)
;; -> 8

数値の最大値を返す

(max 1 2 3 4 5)
;; -> 5
(max 50 2. 25)
;; -> 50.0
;; 比較数値に一つでも実数があれば、最大値が整数であっても実数が返される

数値の最小値を返す

(min 1 2 3 4 5)
;; -> 1
(min 1020 55.5 -30 -2.5)
;; -> -30.0

負の数値かどうか調べる

(minusp 10)
;; -> nil
(minusp -10)
;; -> T

数値がゼロに評価されるかどうかを調べる

(zerop 10)
;; -> nil
(zerop 0)
;; -> T
(zerop (rem 10 3))
;; -> nil
(zerop (rem 10 2))
;; -> T
0
0
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
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?