AutoLISP: The Complete Guide — Chapter 4 / 16

Mathematical Operations

Mathematical operations are among the first things you use in any programming language. In AutoLISP, they follow the same prefix notation as all other functions — and they come with a few interesting quirks.

The Four Basic Operations

Addition: +

(+ 2 3)       ; → 5
(+ 10 20 30)  ; → 60 (you can add more than 2 numbers!)
(+ 1.5 2.3)   ; → 3.8

One advantage of prefix notation: the + function accepts as many arguments as you want. No need to write (+ (+ 10 20) 30) — a simple (+ 10 20 30) is enough.

Subtraction: -

(- 10 3)      ; → 7
(- 100 20 30) ; → 50 (100 - 20 - 30)
(- 5)         ; → -5 (with a single argument, returns the opposite)

Multiplication: *

(* 3 4)       ; → 12
(* 2 3 4)     ; → 24
(* 1.5 2)     ; → 3.0

Division: /

(/ 20 4)      ; → 5
(/ 7 2)       ; → 3 (integer division!)
(/ 7.0 2)     ; → 3.5 (real division)
(/ 7 2.0)     ; → 3.5 (real division too)

Division calculations in the AutoCAD command line

Beware: Integer Division

This is a classic pitfall in AutoLISP (and many other languages). When both operands are integers, the division returns an integer — the decimal part is truncated:

(/ 7 2)     ; → 3 (not 3.5!)
(/ 1 3)     ; → 0 (not 0.333...!)

To get a decimal result, at least one of the operands must be a real number (with a decimal point):

(/ 7.0 2)   ; → 3.5
(/ 7 2.0)   ; → 3.5

You can also use the float function to convert an integer to a real number:

(/ (float 7) 2)  ; → 3.5

Division Remainder: rem

The rem (remainder) function returns the remainder of integer division:

(rem 7 2)    ; → 1 (7 = 2×3 + 1)
(rem 10 3)   ; → 1 (10 = 3×3 + 1)
(rem 8 4)    ; → 0 (exact division)

This is useful for checking whether a number is even:

(rem 6 2)    ; → 0 (even)
(rem 7 2)    ; → 1 (odd)

Advanced Mathematical Functions

AutoLISP provides many additional mathematical functions:

Absolute Value: abs

(abs -5)     ; → 5
(abs 3)      ; → 3
(abs -3.14)  ; → 3.14

Power: expt

(expt 2 3)   ; → 8 (2³)
(expt 5 2)   ; → 25 (5²)
(expt 9 0.5) ; → 3.0 (square root of 9)

Square Root: sqrt

(sqrt 16)    ; → 4.0
(sqrt 2)     ; → 1.41421

Minimum and Maximum: min, max

(min 3 7 1 9)  ; → 1
(max 3 7 1 9)  ; → 9

Trigonometric Functions

AutoLISP works in radians, not degrees:

(sin 0)            ; → 0.0 (sine)
(cos 0)            ; → 1.0 (cosine)
(atan 1)           ; → 0.785398 (arc tangent, i.e. π/4)

To convert degrees to radians, use this formula:

;; Convert 90° to radians
(* (/ 90.0 180) pi)  ; → 1.5708 (π/2)

The constant pi is predefined in AutoLISP and equals approximately 3.14159.

Logarithms and Exponential

(log 1)       ; → 0.0 (natural logarithm)
(log 2.718)   ; → 0.999896
(exp 1)       ; → 2.71828 (e¹)
(exp 0)       ; → 1.0 (e⁰)

Type Conversions

When mixing integers and real numbers, it is sometimes useful to convert explicitly:

(float 5)    ; → 5.0 (integer → real)
(fix 3.7)    ; → 3 (real → integer, truncates the decimal part)
(fix 3.2)    ; → 3
(fix -3.7)   ; → -3

The fix function truncates (it does not round). To round to the nearest integer, you can combine fix and +:

;; Round to nearest integer
(fix (+ 3.7 0.5))   ; → 4
(fix (+ 3.2 0.5))   ; → 3

A Practical Example: Area of a Circle

Let's calculate the area of a circle with radius 5:

;; Area = π × r²
(* pi (expt 5.0 2))  ; → 78.5398

Let's display the result in a dialog box:

(alert
  (strcat "The area of the circle is: "
    (rtos (* pi (expt 5.0 2)) 2 2)
  )
)

Message box displayed by the command

Let's break down this expression:

  1. (expt 5.0 2)25.0 (5²)
  2. (* pi 25.0)78.5398 (π × 25)
  3. (rtos 78.5398 2 2)"78.54" (converts the number to a string with 2 decimal places)
  4. (strcat "The area of the circle is: " "78.54") → concatenates the two strings
  5. (alert ...) → displays the result

The rtos (real to string) and strcat (string concatenate) functions will be covered in detail later. For now, just remember that you can combine calculations and display by nesting expressions.

Summary

Function Description Example
+ Addition (+ 2 3)5
- Subtraction (- 10 3)7
* Multiplication (* 3 4)12
/ Division (/ 7.0 2)3.5
rem Remainder (modulo) (rem 7 2)1
abs Absolute value (abs -5)5
expt Power (expt 2 3)8
sqrt Square root (sqrt 16)4.0
min / max Minimum / Maximum (max 3 7)7
float Integer → Real (float 5)5.0
fix Real → Integer (fix 3.7)3

In the next chapter, we will see how to store values in variables and work with AutoLISP's different data types.


Helping hand Need an AutoCAD (AutoLISP, ObjectARX, .NET, VBA) development? Contact me for a free quote.