
- LISP Tutorial
- LISP - Home
- LISP - Overview
- LISP - Environment
- LISP - REPL
- LISP - Program Structure
- LISP - Basic Syntax
- LISP - Data Types
- Lisp Macros
- LISP - Macros
- LISP - Backquote and Comma
- LISP - Code Generation Using Macro
- LISP - Variable Capture and Hygienic macro
- LISP - Scope and Binding
- LISP - Macro Writing Style
- LISP - Macro Characters
- LISP - Read-Time Macros
- LISP - Compiler Macros
- LISP - Uses of Macros
- Lisp Functions
- LISP - Functions
- LISP - Functions vs Macros
- LISP - Calling Function using funcall
- LISP - Calling Function using apply
- LISP - Closures
- LISP - Functions as Arguments
- LISP - Functions as Return Values
- LISP - Recursion
- LISP - Built-in Functions
- Lisp Predicates
- LISP - Predicates
- LISP - Generic Data Type Predicates
- LISP - Specific Data Type Predicates
- LISP - Equality Predicates
- LISP - Numeric Predicates
- LISP - Comparison Predicates
- LISP - Logical Predicates
- LISP - List Predicates
- LISP - Custom Predicates
- LISP - Chaining Predicates
- Lisp Arrays
- LISP - Arrays
- LISP - Adjustable Arrays
- LISP - Fill Pointers in Arrays
- LISP - Specialized Arrays
- LISP - Arrays Properties
- LISP - Iterating over Arrays
- LISP - Multidimensional Arrays
- LISP - Row-Major Order
- Lisp Strings
- LISP - Strings
- LISP - String Concatenation
- LISP - String Comparison
- LISP - String Case Conversion
- LISP - String Trimmimg
- LISP - String Searching
- LISP - Getting Substring
- LISP - String Replacement
- LISP - Sorting Strings
- LISP - Merging Strings
- LISP - Accessing Characters of String
- LISP - String length
- LISP - Escape Sequences
- Lisp Sequences
- LISP - Sequences
- LISP - Accessing Element of Sequence
- LISP - Sequence length
- LISP - Getting Subsequence
- LISP - Search Element in Sequence
- LISP - Sequence Concatenation
- LISP - Reversing a Sequence
- LISP - Mapping Sequence Element
- LISP - position of Element
- LISP - Remove an Element
- LISP - Sort Sequence
- LISP - Merge Sequences
- LISP - every function
- LISP - some function
- LISP - notany function
- LISP - notevery function
- Lisp Lists
- LISP - Lists
- LISP - Accessing Elements of Lists
- LISP - Modifications to Lists
- LISP - Using mapcar on List
- LISP - Using mapc on List
- LISP - Using reduce on List
- LISP - Removing elements from List
- LISP - Reversing a List
- LISP - Sorting a List
- LISP - Searching a List
- LISP - List vs Vectors
- LISP - Matrix Multiplication
- Lisp Vectors
- LISP - Vectors
- LISP - Creating Vectors
- LISP - Accessing Elements of Vectors
- LISP - Modifications to Vectors
- LISP - Adjustable Vectors
- LISP - Specialized Vectors
- LISP - Vector Functions
- Lisp Set
- LISP - Set
- LISP - Adding elements to the Set
- LISP - Getting SubSet from a Set
- LISP - Set Difference
- LISP - Set Exclusive OR
- LISP - Set Intersection
- LISP - Set Union
- LISP - Representing Set with HashTable
- LISP - List as Set vs HashTable as Set
- Lisp Tree
- LISP - Tree
- LISP - Recursive Traversal
- LISP - Inorder Traversal
- LISP - Preorder Traversal
- LISP - Postorder Traversal
- LISP - Depth First Traversal
- LISP - Modifying Tree
- LISP - Search Tree
- LISP - Binary Tree
- Lisp Hash Table
- LISP - Hash Table
- Adding Values to Hash Table
- Removing Values from Hash Table
- Updating Values of Hash Table
- Iterating Hash Table Entries
- Searching key in HashTable
- Checking Size of HashTable
- Using Custom Equality Check
- Lisp - Input − Output
- LISP - Input − Output
- LISP - Streams
- LISP - Reading Data from Streams
- LISP - Writing Data to Streams
- LISP - File I/O
- LISP - String I/O
- LISP - Formatting with Format
- LISP - Interactive I/O
- LISP - Error Handling
- LISP - Binary I/O
- Lisp - Structures
- LISP - Structures
- LISP - Accessors and Mutators
- LISP - Structure Options
- LISP - Structure Types
- LISP - Applications and Best Practices
- Lisp - CLOS
- LISP - CLOS
- Lisp - Objects
- LISP - Class
- LISP - Slots and Accessors
- LISP - Generic Functions
- LISP - Class Precedence
- LISP - Metaobject Protocol
- LISP - Multimethods
- LISP - Multiple Inheritance
- LISP - Method Combinations
- LISP - Method Combinations
- LISP - :before Method Combination
- LISP - :primary Method Combination
- LISP - :after Method Combination
- LISP - :around Method Combination
- LISP - + Method Combination
- LISP - and Method Combination
- LISP - append Method Combination
- LISP Useful Resources
- Lisp - Quick Guide
- Lisp - Useful Resources
- Lisp - Discussion
Lisp - Numbers
Common Lisp defines several kinds of numbers. The number data type includes various kinds of numbers supported by LISP.
The number types supported by LISP are −
- Integers
- Ratios
- Floating-point numbers
- Complex numbers
The following diagram shows the number hierarchy and various numeric data types available in LISP −

Various Numeric Types in LISP
The following table describes various number type data available in 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. |
Example
Create a new source code file named main.lisp and type the following code in it.
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)))
Output
When you execute the code, it returns the following result −
1/2 5/4 #C(4 -2)
Number Functions
The following table describes some commonly used numeric functions −
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. |
Example
Update the source code file named main.lisp and type the following code in it.
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))
Output
When you execute the code, it returns the following result −
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