The AutoLisp Intermediate Tutorial


Math Functions

   +         /    *      1+     1-    cos    atan    sin    sqrt    expt     Example Program


+   - Addition.

            Syntax : (+ number number)

            number - any valid number.[integer or real number]

             Returns an integer or a real number.

                 (+ 3 4) returns 7

                 (+ 3.5 4.2) returns 7.7

                 (+ 3 4 5) returns 12

                 (+ 1.2 1.2 1.2) returns 3.6

                 (+ "3" "4") returns "Error: Bad Argument Type"   ["3" and "4" are strings, not numbers]

 

-   - Subtraction.

            Syntax : (- number number)

            number - any valid number.[integer or real number]

             Returns an integer or a real number.

                 (- 4 3) returns 1

                 (- 4.5 4.2) returns 0.3

                 (- 9 5 2) returns 2

                 (- 40.5 10.0 5.2) returns 25.3

                 (- "3" "4") returns "Error: Bad Argument Type"   ["3" and "4" are strings, not numbers]

 

 

/   - Division.

            Syntax : (/ number number)

            number - any valid number.[integer or real number]

             Returns an integer or a real number.

                 (/ 9 3) returns 3

                 (/ 5 2) returns 2          [if both numbers are integers then it returns an integer]

                 (/ 5 2.0) returns 2.5  [if either number is a real number then it returns a real number]

                 (/ 5.0 2) returns 2.5  [if either number is a real number then it returns a real number]

                 (/ 12 2 3) returns 2   [12/2 = 6   then 6/3 = 2 ]

                 (/ "3" "4") returns "Error: Bad Argument Type"   ["3" and "4" are strings, not numbers]

*   - Multiplication.

            Syntax : (* number number)

            number - any valid number.[integer or real number]

             Returns an integer or a real number.

                 (* 9 3) returns 12

                 (* 5 2) returns 10           [if both numbers are integers then it returns an integer]

                 (* 5 2.0) returns 10.0  [if either number is a real number then it returns a real number]

                 (* 5.0 2) returns 10.0  [if either number is a real number then it returns a real number]

                 (* 2 3 4) returns 24   [2*3 = 6   then 6*4 = 24 ]

                 (* "3" "4") returns "Error: Bad Argument Type"   ["3" and "4" are strings, not numbers]

 

1+   - Returns value increased by one.

            Syntax : (1+ number)

            number - any valid number.[integer or real number]

             Returns an integer or a real number.

                 (1+ 3) returns 4

                 (1+ 5) returns 6           [if the number is an integer then it returns an integer]

                 (1+ 5.0) returns 6.0  [if number is a real number then it returns a real number]

                 (1+ "3") returns "Error: Bad Argument Type"   ["3" is a string, not a number]

                 (1+ 3 4) returns "Error: Too Many Arguments"   [Only accepts one number as argument]

1-   - Returns value decreased by one.

            Syntax : (1- number)

            number - any valid number.[integer or real number]

             Returns an integer or a real number.

                 (1- 3) returns 2

                 (1- 5) returns 4           [if the number is an integer then it returns an integer]

                 (1- 5.0) returns 4.0  [if number is a real number then it returns a real number]

                 (1- "3") returns "Error: Bad Argument Type"   ["3" is a string, not a number]

                 (1- 3 4) returns "Error: Too Many Arguments"   [Only accepts one number as argument]

 

cos  - Returns the cosine of an angle expressed in radians. 

           (Note: Radians are AutoCads angular units.  A circle = 2*pi       or      180 degrees = pi )

            Syntax : (cos number)

            number - any valid number.[integer or real number] that represents an angle expressed in radians.

             Returns a real number.

                 (cos pi) returns -1.0

                 (cos (+ pi pi)) returns 1.0

                 (cos 5.0) returns 0.283662

                 (cos "3") returns "Error: Bad Argument Type"   ["3" is a string, not a number]

                 (cos 3 4) returns "Error: Too Many Arguments"   [Only accepts one number as argument]

 

atan  - Returns the arctangent of a number in radians.  

           (Note: Radians are AutoCads angular units.  A circle = 2*pi       or      180 degrees = pi )

            Syntax : (atan number1 ....optional number2)

            number1 - any valid number.[integer or real number].

            number2 - any valid number.[integer or real number].  This is optional and is usually omitted.

             Returns a real number representing radians.

                 (atan 1) returns 0.785398

                 (atan -1) returns -0.785398

                 (atan 5.0) returns 1.3734

                 (atan "3") returns "Error: Bad Argument Type"   ["3" is a string, not a number]

                 (atan 3 4) returns 0.643501  [Returns the arctangent of 3 divided by 4]

            (Note: The range of angles returned is -pi/2 to +pi/2 radians. )

 

sin  - Returns the sine of an angle expressed in radians. 

           (Note: Radians are AutoCads angular units.  A circle = 2*pi       or      180 degrees = pi )

            Syntax : (sin number)

            number - any valid number.[integer or real number] that represents an angle expressed in radians.

             Returns a real number.

                 (sin 1.0) returns 0.841471

                 (sin 0) returns 0.0

                 (sin 5.0) returns -0.958924

                 (sin "3") returns "Error: Bad Argument Type"   ["3" is a string, not a number]

                 (sin 3 4) returns "Error: Too Many Arguments"   [Only accepts one number as an argument]

 

sqrt  - Returns the square root of a number. 

            Syntax : (sqrt number)

            number - any valid number.[integer or real number].

             Returns a real number.

                 (sqrt 4) returns 2.0

                 (sqrt 0) returns 0.0

                 (sqrt 5.0) returns 2.23607

                 (sqrt "3") returns "Error: Bad Argument Type"   ["3" is a string, not a number]

                 (sqrt 3 4) returns "Error: Too Many Arguments"   [Only accepts one number as argument]

 

expt  - Returns a number raised to a power. 

            Syntax : (expt number power)

            number - any valid number.[integer or real number].

            power - any valid number.[integer or real number].

             Returns a real number unless both number and power are integers, then it returns an integer..

                 (expt 2 2) returns 4

                 (expt 2 2.0) returns 4.0

                 (expt 5 2) returns 25

                 (expt "3" 2) returns "Error: Bad Argument Type"   ["3" is a string, not a number]


Example Program 1:

(defun C:myProg()

  (setq intAge(getint "\n Enter your Age: "))

  (setq intDays(* intAge 365))

  (setq intWeeks(/ intDays 7))               

  (princ (strcat "\n You are " (itoa intDays) " Days old!"))

  (princ (strcat "\n You are " (itoa intWeeks) " Weeks old!"))

  (princ)

Execution:

Command: (load "myProg")<enter>

Command: myProg<enter>

Command:  Enter your Age: 39<enter>

Command: You are 14235 Days old!

Command: You are 2033 Weeks old!

 


Example Program 2:

(defun C:Triangle()

  (setq pt1(getpoint "\n Pick First Point: "))

  (setq pt2(getpoint "\n Pick Last Point: "))

  (setq x1(car pt1))   ;x-coord of point one

  (setq x2(car pt2))   ;x-coord of point two

  (setq y1(cadr pt1))  ;y-coord of point one

  (setq y2(cadr pt2))  ;y-coord of point two

  (setq xdis(- x2 x1)) ;distance in x direction between points

  (setq ydis(- y2 y1)) ;distance in y direction between points

  (setq slpdis(sqrt(+ (expt xdis 2.0)(expt ydis 2.0)))) ;A sq + B sq = C sq  

  (princ (strcat "\n Distance = " (rtos slpdis 4 4)))

  (princ)

 

Execution:

Command: (load "Triangle")<enter>

Command: Triangle<enter>

Command: Pick First Point:<pick>

Command: Pick Last Point:<pick>

Command: Distance = 3'-4 1/2"

Command:


AutoLisp Intermediate Tutorial

AutoLisp Tutorial Home