Acad Independent Functions
Logic and Math

Topics

We have logical functions dealing with integers and some mathematical functions.

Algebra (and its geometric representation) maybe handled by external libraries, but keeping numerical precision is hard to do.

Topics in this section:

Logical Operations on Numbers (numbers as bit-arrays)

Base Conversion

Arithmetic
Primes

Random Numbers

Statistics

Combinatorics

Trigonometry

Naming Conventions

BIT for dealing with bit-values, LOG for dealing with numbers in logical context.

We do not use bits as indices in bit-arrays, we're using bit-values to match the Autodesk docs.

Function Reference

StdLib Constants:

*INFINITY* 1.7e308

*NUM-TOL* 1e-6, numeric tolerance for comparisons

*EPSILON* 1e-12, user-defined epsilon

*MACHINE-EPSILON* 2.22e-16; lowest possible small number, machine dependent

*MAX-INT* one of the next two; longint or shortint

*MAX-LONGINT* 2147483647 ; signed long int (32 bit signed int)

*MAX-SHORTINT* 32767 ; signed short int (16 bit signed int)

*MAX-REAL* 1.7e308 ; double float, 64 bit, 15 digit prec

*PI2* (* PI 2), PI doubled, 360°

*PI/2* (/ PI 2), 90°

*PI/4* (/ PI 4), 45°

Logic, Bit Manipulations

AutoLISP native functions:

(LOGAND int int...) ; logical and

(LOGIOR int int...) ; logical inclusive or

(BOOLE func int int...) ;

(LSH int num-bits) ; left shift

StdLib functions:

(STD-LOGXOR int int) ; logical xor, two args only

(STD-LOGNOT int) ; logical not, one arg only

(STD-BITSETP val flag) ; (std-bitsetp 128 1) => nil

(STD-SETBIT val flag) ; (std-setbit 128 1) => 129

(STD-BITDEL val flag) ; (std-bitdel 128 129) => 1

(STD-BITLIST int) ; list of bitvalues

(STD-BITTOGGLE val flag) ; toggles bits in flag

Base Conversion

should num or dec be used?

(STD-NUM->HEX int) ; (std-num->hex 10) => "A"

(STD-HEX->NUM str) ; (std-num->hex "2a") => 42

(STD-NUM->BINint) ; (std-num->bin "2") => (1 0)

(STD-BIN->NUM intlst) ; (std-bin->bin '(0 1 1)) => 3

(STD-NUM->OCTint) ; (std-num->oct 151) => "227"

(STD-OCT->NUM str) ; (std-oct->num "227") => 151

Arithmetic

AutoLISP native functions:

(EXP num) ; enum

(EXPT num x) ; REAL: num x

(LOG num) ; natural logarithm, inverse to ex

(SQRT num) ; REAL: square root

StdLib functions:

(STD-SQR num) ; num2, square

(STD-SQRT num) ; type preserving square root

(STD-EXPT num x) ; type preserving: numx

(STD-ROUND num) ; rounded to the nearest integer

(STD-FRACTION num) ; only fractional part

(STD-FLOOR num) ; fix to lower integer

(STD-CEILING num) ; fix to upper integer

(STD-LOG2 int) ; log2, log base 2

(STD-LOG10 num) ; log10, log base 10

(STD-ISQRT num) ; integer square root

(STD-GCD intlst) ; greatest common divisor

(STD-LCM intlst) ; least common multiple

Statistics:

(STD-MEAN numlst) ; average of nums

(STD-MEDIAN numlst) ; center of distribution

(STD-STANDARD-DEVIATION numlst) ;

Primes, only in module PRIMES:

(STD-PRIMEP n) ; is n prime?

(STD-PRIME-FACTORIZATION n) ; all prime multiplicants for n

(STD-PRIMES n) ; list of prime numbers up to n

Random Numbers:

(STD-RANDOM num) ; < num of type num

only in module RANDOM:

(STD-MAKE-RANDOM-STATE num) ; to set and restore a unique sequence

(STD-RANDOM-STATE-P num) ;

Trigonometry

AutoLISP native functions and constants

PI ; PI with maximal precision

(SIN radians) ; sine

(COS radians) ; cosine

(ATAN radians) ; arc tangent

StdLib functions:

(STD-DTR degree) ; degree to radian

(STD-RTD radians) ; radian to degree

(STD-TAN radians) ; tan x = sin x/cos x

(STD-ACOS radians) ; arc cosine

(STD-ASIN radians) ; arc sine

(STD-SEC radians) ; secant

(STD-CSC radians) ; cosecant

(STD-ASEC real) ; arc secant

(STD-ACSC real) ; arc cosecant

(STD-ACOT real) ; arc cotangent

(STD-SINH radians) ; hyperbolic sine

(STD-COSH radians) ; hyperbolic cosine

(STD-TANH radians) ; hyperbolic tangent

(STD-SECH radians) ; hyperbolic secant

(STD-CSCH radians) ; hyperbolic cosecant

(STD-ARSINH real) ; hyperbolic area sine

(STD-ARCOSH real) ; hyperbolic area cosine

(STD-ARTANH real) ; hyperbolic area tangent

(STD-SIGNUM num) ; -1, 0 or 1

Combinatorial functions, external module COMBINATIONS:

(PERMUTATE lst) ; list of n! permutations

(COMBINATIONS lst m) ; list of n!/m!(n-m)! combinations

(PERMUTATIONS lst m) ; list of n!/(n-m)! permutations

(FACTORIAL n) ; n!

(COMBINATIONS-COUNT n m) ; n!/m!(n-m)!

(PERMUTATIONS-COUNT n m) ; n!/(n-m)!

(NTH-PERMUTATION i lst) ; i-th permutation

(NTH-PERMUTATIONS i lst m) ; i-th value of permutations

(NTH-COMBINATIONS i lst m) ; i-th value of combinations

Overview

Most of the logic functions deal with integer numbers and their representation as bit-arrays, which make them common in c-style for storing flags, multiple options stored in one single number.

The logic functions deal with those flags, setting, getting, manipulating bits in the flags. The bit arguments are the "bit-values" and not the "bit-positions", the 2's complement of the position. A bit-value of 8 accesses the 3rd bit in the flag, 23 = 8

The math functions are trivial and quite slow. Built-in trigonometric functions are sin, cos and atan, the other built-in mathematical functions dealing with real's are exp, expt, sqr and sqrt. All the other needed functions may be derived from these.

For problems with precision and complex solutions see the discussion below.

Some exotic functions rarely used were moved to external modules, such as support for changing random generators, primes and combinatorics.

Numbers

One of the strongest points in Common Lisp is one of the weakest point in AutoLISP: numbers

CL supports accurate calculations with and coercion from and to rational and complex numbers, several integer and floating-point types with different precision and is known of its superiority over C in numbers, exception handling and arbitrary precision arithemetic.

AutoLISP supports just integer -
internally signed longs: 32 bit, +/- 2,147,483,646,

externally - talking with Acad - converted to signed shortnums: 16 bit, +/- 32767

and real numbers (double-float, +/- 1.7e308, 64 bit, ~15 digits precision).

Due to the nature of floating point maths there often occur rounding errors, esp. when values are often passed between AutoCAD and AutoLISP.

So we have to deal with numeric overflow and underflow problems and not correct type coercions in older AutoLISP versions.

Especially the COMBINATIONS and PRIMES modules are optimized to work with possible large numbers or large lists.

Random Number Generators

Supplied as external module is a fast pseudo random number generator which should be suitable for most purposes. However pseudo random number never provide series of real random values. This can only be supplied by hardware. We didn't implement the known hardware based library function yet, because Win32 provides no easy access to these chips.

We provide methods, fully compatible to Common Lisp, to save and restore a state for series, and to change seeds and methods for different generators at run-time.

We also supplied some more generators but there's no ultimative method or even no good enough method (the "quality generator") yet, which might hurt when you see the grapical result of randomness esp, in 3D.

See std-make-random-state and the test suite for the generators (test/RNDTEST.LSP) and the supplied documentation there (test/rndtest.html), and maybe you want to have a look at the stdlib mail archive (part 2). There was a lot of ­not only academic­ discussion about random generators.

The basic function STD-RANDOM is supplied with STDMATH and the dynamic redefinition and dealing with states with STD-MAKE-RANDOM.-STATE is in the seperate module RANDOM.LSP. You'll have to use (std-require 'RANDOM) to be able to use the advanced methods.

Prime Numbers

This is a quite esoteric field. It's probably needed for some security schemes and recreation.

It is located in an external module PRIMES.LSP. You'll have to use (std-require 'PRIMES) to be able to use STD-PRIMES, STD-PRIMEP or STD-PRIME-FACTORIZATION .

There are no fast probabilistic methods implemented yet.

timings: check 100000000007, see test/PRIMETST.LSP

(STD-PRIMEP 100000000007) 	: ~70 ms
(STD-PRIME-FACTORIZATION 100000000007) 	: 60157 ms
(STD-PRIMES 100000000007) 	: more than 24 hours.