Drafters Forum | JOBS | Drafting Standards | Funnies | Technical Dictionary | AutoCAD Tutorials | Free Lisp | Free Blocks

ARTICLE: Top ways drafters can make money online with Freelancer.com

AutoLISP lesson 6 - NUMBERS

From Draftsperson.net

Mathematical expression format

AutoLISP uses a prefix notation for evaluating math expressions. The normal way you write equations is using an infix notation. If you wanted to add two numbers you would write it:

1 + 2 = X

In AutoLISP it would be written as:

(+ 1 2) ;Returns 3

As you should know by now, all lists in AutoLISP start with a function and the remainder of the list is the arguments to the function. Math in AutoLISP is no different. If you want to add two numbers you first give the function name which is "+" then you give the numbers you want the addition performed on. The rest of the math function works the same way.

(/ 4 2) ;Returns 2
(* 4 2) ;Returns 8
(- 4 2) ;Returns 2

If you get confused at first just try to visualize the operator placed between the two numbers that follow the operator.

AutoLISP has many functions built right in for using numbers and strings. Learning to use numbers & strings is the single most important thing you can do to increase your productivity by using LISP. Most of the programs you write will be calculating numbers & telling the user what you calculated.

The AutoLISP functions for dealing with numbers are listed below.

Predefined mathematic functions

+ The addition operator.

(+ 2 4) ;Returns 6

- The subtraction operator.

(- 4 2) ;Returns 2
* The multiplication operator.
(* 4 2) ;Returns 8

/ The division operator. (/ 4 2) ;Returns 2

1+ Adds one to a number.

(1+ 2.0) ;Returns 3

1- Subtracts one from a number.

(1- 2.0) ;Returns 1

min Gets the minimum number from a list of numbers.

(min 3 2 6 8) ;Returns 2

max Gets the maximum number from a list of numbers.

(max 3 2 6 8) ;Returns 8

abs Returns the absolute value of a number.

(abs 1) ;Returns 1
(abs -1) ;Returns 1

sqrt Returns the square root of a number.

(sqrt 9) ;Returns 3

exp Returns e raised to the power of a number. e is the constant 2.71828.

(exp 1.0) ;Returns 2.71828
(exp 2.0) ;Returns 7.38905

expt Returns a number raised to the power of another number.

(expt 2 3) ;Returns 8
(expt 2 4) ;Returns 16

log Returns the natural log of a number.

(log 10) ;Returns 2.30259
(log 20) ;Returns 2.99573

gcd Returns the greatest common denominator of two integer numbers.

(gcd 12 24) ;Returns 6

rem Returns the remainder of the division of two numbers.

(rem 10 3) ;Returns 1

sin Returns the sine of a number in radians.

(sin 90) ;Returns 0.893997

cos Returns the cosine of a number in radians.

(cos 45) ;Returns 0.525322

atan Returns the arctangent of a number in radians.

(atan 45) ;Returns 1.548578


Magic numbers

The use of magic numbers should be avoided if at all possible. There's nothing "magic" about magic numbers. The magic part comes in when you go back later to modify a program that works, and you find literal numbers everywhere but have no idea what they are. It makes it much easier to read and modify a program if constants with GOOD names are used instead of numbers. Remember when naming your constants to give them names that describe what they are. PI is a good example of a constant name, it tells exactly what it is.

The value PI has been made a constant in AutoLISP, because it is used so frequently when calculating angles. All you need to do to get the value of PI is type PI into your formulas.

Type conversions

AutoLISP has two types of numbers, integers and reals. An integer is just what it sounds like. A real number is a floating-point number.

An integer can be any number between -2,147,483,648 and +2,147,483,647. The + sign is optional as any number without a - sign is taken to be positive.

Real numbers, or floating point, are double precision floating point values with at least 14 significant digits of precision. A floating-point number; must be written with leading 0's if the number is less than 1. The AutoLISP interpreter would, not accept the number .4. The number 0.4 would be recognized as a floating-point value.

AutoLISP will automatically convert types for you when you mix them in your equations. The predefined function FIX converts a floating-point number to an integer number. Its complementary function is FLOAT, which converts integer numbers to floating-point numbers.

Most of the time you will be dealing with floating point numbers. Point data is returned as floating-point numbers, and that is usually what we are dealing with. The real problem with using numbers when you’re not sure of their type is when you end up with two integer numbers.

Integer division is not like floating point division. If you divide two integers you will end up with an integer.

(/ 1 2) ;Returns 0
(/ 1 2.0) ;Returns 0.5

As you can see from the examples, an integer returns the result of the division as the integer portion of the result and leaves off the decimal portion. This is handy at times but can be a real "gotcha" if that's not what you had in mind. The best way to make sure this doesn't happen to you is to do the type conversions yourself, that way you know what you are dealing with.


A string is any group of ASCII characters within double quotes (" "). Because memory for strings is dynamically allocated a string may be any length up to the memory of the computer. The only restriction on a string is, a string constant can only be 132 characters in length. Using the STRCAT function can combine longer strings, as you will see later. There are many predefined functions for working with strings; they are listed below.

Predefined String Functions

Note: Arguments enclosed in brackets [] are optional.

angtof (angtof string [mode])
(angtof "45.00" 0) ;Returns 0.785398

Converts a string, which represents an angle in the display format, specified by mode, into a floating-point value. The return value will be expressed in radians if ANGTOF succeeds, else nil will be returned. The mode argument specifies the units in which the string is formatted. The value should match the values used by the AUNITS system variable. If mode is left out, ANGTOF uses the current setting of the AUNITS system variable. The possible values allowed for use with ANGTOF are shown below.

Mode value String format

  • 0 Degrees
  • 1 Degrees/Minutes/Seconds
  • 2 Grads
  • 3 Radians
  • 4 Surveyor's units

Input to ANGTOF must be in a format acceptable as input to an AutoCAD command.

angtos (angtos angle [mode [precision]])
(angtos 0.785398 0 4) ;Returns "45.0000"
(angtos -0.785398 0 4) ;Returns "315.0000"

Converts an angle in radians (a real number) to a string according to the values of "mode" and "precision". Mode and precision follow the same rules as applied to ANGTOF. The mode table shown above may be used.

Once again if the "mode" argument or the "precision" argument are not given, AutoLISP will use the current values of AUNITS and AUPREC respectively, to format the string.

As you can see in the examples, negative values can be entered and will be converted to positive angles. Remember that the angle conversion are relative to the ANGBASE, so if you are trying to convert arbitrary angles you must add the angle between ANGBASE and your angle to get the angle to convert correctly.

If you select a mode of 4 (surveyor's units), the UNITMODE variable will affect the conversion to a string. If UNITMODE=0, spaces are included in the string ("N 45d E"); if UNITMODE=1, no spaces are used in the string ("N45dE").

read (read "string") ;Returns the string string
(read "(a b c)") ;Returns the list (a b c)
(read "(a b c) (d)") ;Returns the list (a b c)
(read "25.4") ;Returns the real number 25.4
(read "4") ;Returns the integer 4
(read "4 12.5") ;Returns the integer 4

Returns the first list or atom obtained from the string. The string cannot contain blanks except within a list or string. Read attempts to convert the string into the closest data type. If it cannot figure out what data type to convert to it will return nil.

strcase (strcase "string" [which]) ;If [which] is nil Returns
;STRING else if [which] is not
;nil returns string.
(strcase "String") ;Returns "STRING"
(strcase "String") ;Returns "string"

Returns a copy of a string with all the characters converted to either upper or lower case. If the "which" argument is omitted all characters are converted to uppercase. If "which" is supplied and non-nil STRCASE converts the characters to lowercase.

strcat (strcase "string1" "string2");Returns "string1string2"

Returns the concatenation of one or more strings. For those of you who don't know, "concatenation" simply means it combines the two strings into one string.

strlen (strlen "string") ;Returns 6
(strlen "string1" "string2");Returns 14

Returns the length, in characters, of a string. If more than one string is given it returns the sum of all the string lengths.

substr (substr "string" start [length])
(substr "string" 3 3) ;Returns ring
(substr "string" 3) ;Returns ring

Returns a sub-string of a string. It starts at "start" and goes on for [length]. If length is unspecified it returns the remainder of the string.

ascii (ascii "abc") ;Returns 97
(ascii "ABC") ;Returns 65
(ascii "a") ;Returns 97
(ascii "A") ;Returns 65
(ascii "Bite") ;Returns 66

Returns the conversion of the first character of a string into its ASCII character code.

atof (atof "25.4") ;Returns the real number 25.4
(atof "4") ;Returns the real number 4.0

Returns the conversion of a string into a real value.

atoi (atoi "25.4") ;Returns the integer 25
(atoi "12.8") ;Returns the integer 12

Returns the conversion of a string into an integer.

chr (chr 66) ;Returns "B"
(chr 65) ;Returns "A"

Returns the conversion of an integer representing an ASCII character code into a single-character string.

itoa (itoa 65) ;Returns "65"
(itoa -23) ;Returns "-23"

Returns the conversion of an integer into a string.

rtos (rtos number [mode [precision]])
(rtos 24.375621 2 3) ;Returns "24.375"

Returns the conversion of a real number into a string. The mode argument corresponds to the LUNITS system variable in AutoCAD. The precision corresponds to the LUPREC system variable. The unit mode values are set as follows.

Mode Value String format

  • 1 Scientific
  • 2 Decimal
  • 3 Engineering (feet and decimal inches)
  • 4 Architectural (feet and fractional inches)
  • 5 Fractional

If you omit the units RTOS uses the current settings of LUNITS and LUPREC to format the string.

String considerations

There are a few things you need to know to make your work with strings a little easier. There are special characters that are defined within AutoLISP, they are there for better formatting of your strings. Some people call them "control" characters or "escape" codes. They can be used in a string just like any other character but are a combination of two characters. They are listed below.

  • \\ Backslash character
  • \" Double quote character
  • \e Escape character
  • \n Newline character
  • \r Return character
  • \t Tab character
  • \nnn Character whose octal code is nnn

As you notice all the escape code use a backslash as the first character. This means that in AutoLISP you cannot use a backslash by its self. If you use a backslash such as in a path string the backslash will be ignored and all the characters will be run together. The following example illustrates this point.

(setq path "c:\acad\lisp") ;Returns "c:acadlisp"

Be careful not to do this in your lisp programs. You must use the forward slash when entering paths.


When using strings for prompts there are a few things you can do to make your code more readable. The formatting of prompts can be done several ways, all giving the same output. So what you want is to make your code read as close to the actual output as possible.

If you were to give a person the choice between two different courses of action in one prompt and output them on two different command lines it would look something like the following.

(prompt "\nPlease pick insertion point...\nor press enter to exit.")

This would be perfectly acceptable to the AutoLISP interpreter and it would put the correct prompts to the screen. They would look like the following.

command:Please pick insertion point...
command:or press enter to exit.

The better way to write the line would be as follows.


(prompt "\nPlease pick insertion point...")
(prompt "\nor press enter to exit.")


(prompt (strcat "\nPlease pick insertion point ..."
"\nor press enter to exit."))

You would get the same output to the screen, but the program code is much more readable. When writing informative prompts to the user of your program, many times you will need to use STRCAT to get string and information all tied together for output to the screen. This can also get messy if you don't pay attention to how you code it.

(setq Val1 "Dave")
(prompt (strcat "\nI think " Val1 " is a very nice person!"))

The example above shows a way to code the prompt, but once again I think can do better.

(prompt (strcat "\nI think "
Val1 "is a very nice person."))

The second way takes a little more typing but if you do many of these type prompts, you will find they are much easier to see what strings are being brought together. It becomes increasingly important as you bring more variables into the strings.

More Lessons

Need drafting work?

We are having great success with Freelancer.com. With this website we can win all types of drafting work. People from all over the world are looking for freelancers skilled in AutoCAD, Revit, Civil 3D and Microstation, are you one of those people? If so you can live the freelancer lifestyle! and work from any location you want too. Freelancer.com, it works really well - we like it - and we believe you will too.

Looking for CAD drafters?

With modern construction, software and increased legal requirements, we often require specialized assistance with our projects. Putting skilled staff on full time is not always economically feasible for small businesses. Freelancer.com solves this problem. On this website you can engage skilled drafters. Either on fixed prices for specific tasks, or reasonably priced hourly rates for longer term contracts.

E-book: AutoCAD Block Best Practices

We draw repetitively when using AutoCAD. If we manage the drawing elements as reusable contents, it will increase our productivity significantly. Not only can we draw faster, we can add information to our blocks. Which allows us to generate useful reports. This is what Edwins e-book entitled AutoCAD Block Best Practices is all about. Yes, increase your productivity with the block!

Printable version



Main Page | Free AutoCAD Blocks | Free Revit Families | Funnies | Drafting Standards | Drafting & CAD Forums | Structural Drafting | Technical Dictionary

Personal tools