Lisp - 數字



Common Lisp 定義了幾種型別的數字。數字資料型別包含 LISP 支援的各種型別的數字。

LISP 支援的數字型別包括:

  • 整數
  • 比率
  • 浮點數
  • 複數

下圖顯示了 LISP 中的數字層次結構和各種數值資料型別:

Numeric Types

LISP 中的各種數值型別

下表描述了 LISP 中可用的各種數字型別資料:

序號 資料型別及描述
1

fixnum

此資料型別表示不太大的整數,通常在 -215 到 215-1 的範圍內(取決於機器)

2

bignum

這些是非常大的數字,其大小受為 LISP 分配的記憶體量限制,它們不是 fixnum 數字。

3

ratio

表示分子/分母形式的兩個數字的比率。當引數為整數時,/ 函式始終產生比率結果。

4

float

它表示非整數。有四種精度遞增的 float 資料型別。

5

complex

它表示複數,用 #c 表示。實部和虛部都可以是有理數或浮點數。

示例

建立一個名為 main.lisp 的新原始碼檔案,並在其中鍵入以下程式碼。

main.lisp

; write 1/2
(write (/ 1 2))
; terminate printing
(terpri)
; execute and print the expression result
(write ( + (/ 1 2) (/ 3 4)))
; terminate printing
(terpri)
; execute and print the expression result
(write ( + #c( 1 2) #c( 3 -4)))

輸出

執行程式碼時,它返回以下結果:

1/2
5/4
#C(4 -2)

數字函式

下表描述了一些常用的數字函式:

序號 函式及描述
1

+, -, *, /

相應的算術運算

2

sin, cos, tan, acos, asin, atan

相應的三角函式。

3

sinh, cosh, tanh, acosh, asinh, atanh

相應的雙曲函式。

4

exp

指數函式。計算 ex

5

expt

指數函式,同時接受底數和指數。

6

sqrt

它計算一個數字的平方根。

7

log

對數函式。如果只給出一個引數,則計算其自然對數,否則第二個引數用作底數。

8

conjugate

它計算一個數的複共軛。對於實數,它返回數字本身。

9

abs

它返回數字的絕對值(或大小)。

10

gcd

它計算給定數字的最大公約數。

11

lcm

它計算給定數字的最小公倍數。

12

isqrt

它給出小於或等於給定自然數的精確平方根的最大整數。

13

floor, ceiling, truncate, round

所有這些函式都接受一個數字作為引數並返回商;floor 返回不大於比率的最大整數,ceiling 選擇大於比率的較小整數,truncate 選擇與比率同號且絕對值小於比率絕對值的整數,而 round 選擇最接近比率的整數。

14

ffloor, fceiling, ftruncate, fround

與上述相同,但返回商為浮點數。

15

mod, rem

返回除法運算中的餘數。

16

float

將實數轉換為浮點數。

17

rational, rationalize

將實數轉換為有理數。

18

numerator, denominator

返回有理數的各個部分。

19

realpart, imagpart

返回複數的實部和虛部。

示例

更新名為 main.lisp 的原始碼檔案,並在其中鍵入以下程式碼。

main.lisp

; evaluate and print division result
(write (/ 45 78))
; terminate printing
(terpri)
; evaluate and print floor result
(write (floor 45 78))
; terminate printing
(terpri)
; evaluate and print division result
(write (/ 3456 75))
; terminate printing
(terpri)
; evaluate and print floor result
(write (floor 3456 75))
; terminate printing
(terpri)
; evaluate and print ceiling result
(write (ceiling 3456 75))
; terminate printing
(terpri)
; evaluate and print truncate result
(write (truncate 3456 75))
; terminate printing
(terpri)
; evaluate and print round result
(write (round 3456 75))
; terminate printing
(terpri)
; evaluate and print floor as floating result
(write (ffloor 3456 75))
; terminate printing
(terpri)
; evaluate and print ceiling as floating result
(write (fceiling 3456 75))
; terminate printing
(terpri)
; evaluate and print truncate as floating result
(write (ftruncate 3456 75))
; terminate printing
(terpri)
; evaluate and print round as floating result
(write (fround 3456 75))
; terminate printing
(terpri)
; evaluate and print mod as result
(write (mod 3456 75))
; terminate printing
(terpri)
; set c as complex number
(setq c (complex 6 7))
; print c
(write c)
; terminate printing
(terpri)
; write complex number
(write (complex 5 -9))
; terminate printing
(terpri)
; write real part of c
(write (realpart c))
; terminate printing
(terpri)
; write imaginary part of c
(write (imagpart c))

輸出

執行程式碼時,它返回以下結果:

15/26
0
1152/25
46
47
46
46
46.0
47.0
46.0
46.0
6
#C(6 7)
#C(5 -9)
6
7
廣告