📜  LISP-数字

📅  最后修改于: 2020-11-03 07:11:07             🧑  作者: Mango


Common Lisp定义了几种数字。数字数据类型包括LISP支持的各种数字。

LISP支持的数字类型是-

  • 整数
  • 比率
  • 浮点数字
  • 复数

下图显示了LISP中可用的数字层次结构和各种数字数据类型-

数值类型

LISP中的各种数字类型

下表描述了LISP中可用的各种数字类型数据-

Sr.No. Data type & Description
1

fixnum

This data type represents integers which are not too large and mostly in the range -215 to 215-1 (it is machine-dependent)

2

bignum

These are very large numbers with size limited by the amount of memory allocated for LISP, they are not fixnum numbers.

3

ratio

Represents the ratio of two numbers in the numerator/denominator form. The / function always produce the result in ratios, when its arguments are integers.

4

float

It represents non-integer numbers. There are four float data types with increasing precision.

5

complex

It represents complex numbers, which are denoted by #c. The real and imaginary parts could be both either rational or floating point numbers.

创建一个名为main.lisp的新源代码文件,然后在其中键入以下代码。

(write (/ 1 2))
(terpri)
(write ( + (/ 1 2) (/ 3 4)))
(terpri)
(write ( + #c( 1 2) #c( 3 -4)))

当您执行代码时,它返回以下结果-

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

数字功能

下表描述了一些常用的数值函数-

Sr.No. Function & Description
1

+, -, *, /

Respective arithmetic operations

2

sin, cos, tan, acos, asin, atan

Respective trigonometric functions.

3

sinh, cosh, tanh, acosh, asinh, atanh

Respective hyperbolic functions.

4

exp

Exponentiation function. Calculates ex

5

expt

Exponentiation function, takes base and power both.

6

sqrt

It calculates the square root of a number.

7

log

Logarithmic function. It one parameter is given, then it calculates its natural logarithm, otherwise the second parameter is used as base.

8

conjugate

It calculates the complex conjugate of a number. In case of a real number, it returns the number itself.

9

abs

It returns the absolute value (or magnitude) of a number.

10

gcd

It calculates the greatest common divisor of the given numbers.

11

lcm

It calculates the least common multiple of the given numbers.

12

isqrt

It gives the greatest integer less than or equal to the exact square root of a given natural number.

13

floor, ceiling, truncate, round

All these functions take two arguments as a number and returns the quotient; floor returns the largest integer that is not greater than ratio, ceiling chooses the smaller integer that is larger than ratio, truncate chooses the integer of the same sign as ratio with the largest absolute value that is less than absolute value of ratio, and round chooses an integer that is closest to ratio.

14

ffloor, fceiling, ftruncate, fround

Does the same as above, but returns the quotient as a floating point number.

15

mod, rem

Returns the remainder in a division operation.

16

float

Converts a real number to a floating point number.

17

rational, rationalize

Converts a real number to rational number.

18

numerator, denominator

Returns the respective parts of a rational number.

19

realpart, imagpart

Returns the real and imaginary part of a complex number.

创建一个名为main.lisp的新源代码文件,然后在其中键入以下代码。

(write (/ 45 78))
(terpri)
(write (floor 45 78))
(terpri)
(write (/ 3456 75))
(terpri)
(write (floor 3456 75))
(terpri)
(write (ceiling 3456 75))
(terpri)
(write (truncate 3456 75))
(terpri)
(write (round 3456 75))
(terpri)
(write (ffloor 3456 75))
(terpri)
(write (fceiling 3456 75))
(terpri)
(write (ftruncate 3456 75))
(terpri)
(write (fround 3456 75))
(terpri)
(write (mod 3456 75))
(terpri)
(setq c (complex 6 7))
(write c)
(terpri)
(write (complex 5 -9))
(terpri)
(write (realpart c))
(terpri)
(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