Tema 1. Introducción:
Funciones matemáticas
Autor: Robierzo
Pasaré a describir las distintas
funciones de lisp por temas.
Empecemos por la de Matemáticas.
+ suma
- resta
* multiplica
/ divide
rem resto de una división de
neteros
1+ suma 1
1- resta 1
abs valor absoluto
fix la parte entera de un numero
real
float convierte un entero en un
real
gcd máximo común denominador
min el más pequeño de un grupo
max el mayor de un grupo
sqrt la raíz cuadrada
expt exponente
exp potencia de e , base de los
logaritmos naturales
log logaritmo natuar
cvunit conversor de unidades
Sigamos por cómo se usan estas
funciones.
Lisp tiene un modo particular de
expresar las operaciones matemáticas
, según tengo entendido se deriva
de la Notación polaca inversa, no es igual pero se le asemeja.
las operaciones se escriben
poniendo como prefijo el operador
por ejemplo para sumar 2 + 3
pondremos
+ suma
( + 2 3 ) devuelve 5 , por
supuesto
( + 4 5.6 -2 ) da 7.6, en este
caso es la suma de dos numeros positivos mas un negativo
Les comento algo , no hace falta
escribir un programa para verificar esto,
tan solo pongan en la linea de
comandos de acad la operación tal
como la he puesto arriba , abren parentesis , el signo mas, el 2, el 3
, cierran parentesis y dan intro enter y podrán ver el resultado en la
linea de comando.
y así para todas las operaciones
- resta
( - 9 7 )
( - 8 4 0.5 )
Multiplica
( * 6 7)
( * 2 -3 1.5 )
Division
( / 12 6 )
( / 11 6) , fíjense que pasa con
esta division , y aquí vale lea pena hablar sobre enteros INT y reales
.
11 y 6 son dos enteros y por lo
tanto darán como resultado la parte entera de la división que será 1
ahora si tan solo ponen el
divisor como real , el resultado será un real
( / 11 6.0)
basta decir que para que un
número sea real debe tener expresada su parte decimal , aunque sean 0
6 es un entero
6.0 es un real
PRESTEN ATENCIÓN A ESTE TEMA , YA
QUE ES MOTIVO DE ERRORES QUE SON AVECES CASI IMPOSIBLES DE DETECTAR.
bueno por ahora los dejo , pero
antes les dejo una lección completa ,
lamentablemente está en inglés , pero es lo único que tengo a mano
Cita:
Function Definitions
--------------------------------------------------------------------------------
1 - Mathematics
--------------------------------------------------------------------------------
+ ..... Adds
(+ 2 3) returns 5
(+ 4 5.6 -2) returns 7.6
After (setq a 20 b 9)
(+ a b) returns 29
--------------------------------------------------------------------------------
- ..... Subtracts
(- 9 7) returns 2
(- 8 4 0.5) returns 3.5
After (setq a 20 b 9)
(- a b) returns 11
(- b a) returns -11
(- a) returns -20. When only one
argument is supplied, its value is subtracted from 0, switching the
sign of any number.
--------------------------------------------------------------------------------
* ..... Multiplies
(* 6 7) returns 42
(* 2 -3 1.5) returns -9.0
After (setq a 3 b 4.5)
(* a b) returns 13.5
(* a a) returns 9
--------------------------------------------------------------------------------
/ ..... Divides
(/ 12 6) returns 2
(/ 11 6) returns 1 (Division
using only integers ignores any remainder. Beware of "integer
division.")
(/ 11 6.0) returns 1.833333
(/ 12 3 2) returns 2
(/ 12 -2.0) returns -6.0
--------------------------------------------------------------------------------
rem ..... Returns a remainder
resulting from division
(rem 10 3) returns 1
(rem 10 5) returns 0
(rem 6.2 1.2) returns 0.2
--------------------------------------------------------------------------------
1+ ..... Increments by 1
(1+ 16) returns 17
After (setq a 21)
(1+ a) returns 22. Used with
setq, this serves as a loop counter for iteration. For example, (setq c
(1+ c)).
(1+ -4) returns -3
--------------------------------------------------------------------------------
1- ..... Decrements by 1
(1- 16) returns 15
After (setq a -8)
(1- a) returns -9
--------------------------------------------------------------------------------
abs ..... Returns the absolute of
a number
(abs 7.0) returns 7.0
(abs -6) returns 6
--------------------------------------------------------------------------------
fix ..... Discards fractional
portion of a number and returns an integer
(fix 7. 1) returns 7
(fix 7.9) returns 7
(fix -4.6) returns -4
--------------------------------------------------------------------------------
float ..... Converts a number to
a real number (floating point decimal number)
(float 5) returns 5.0
(float 5.82) returns 5.82
--------------------------------------------------------------------------------
gcd ..... Returns the greatest
factor (divisor) common to two or more integers. Arguments must be
positive integers.
(gcd 21 39) returns 3
(gcd 102 138) returns 6
(gcd 30 45 60) returns 15
Autodesk says this function
returns the "greatest common denominator"
(a meaningless phrase). It is better to think of it as "greatest common
divisor."
--------------------------------------------------------------------------------
min ..... Returns the smallest
(lowest) of two or more numbers
(min 8 3 11.88 14 6) returns 3.0
(min 13 -28 2 66) returns -28
--------------------------------------------------------------------------------
max ..... Returns the greatest of
two or more numbers
(max 8 3 11.88 14 6) returns 14.0
(max -22 -2 16) returns 16
--------------------------------------------------------------------------------
sqrt ..... Returns the square
root of a number as a real
(sqrt 16) returns 4.0
(sqrt 2.0) returns 1.414214
--------------------------------------------------------------------------------
expt ..... Raises a given number
to a given power
(expt 3 4) returns 81
(expt 2.0 5.0) returns 32
(expt 3 -2) returns 0 (negative
exponents applied to integers can appear to give incorrect results
because of integer division)
(expt 3.0 -2.0) returns 0.111111
(expt 256 0.25) returns 4.0
After (setq a 2.25 b 0.5)
(expt a b) returns 1.5
--------------------------------------------------------------------------------
exp ..... Returns e raised to a
given power
(exp 1.0) returns 2.71828
(exp 2.14) returns 8.49944
(exp 0.4) returns 1.49182
(exp -2.0) returns 0.135335
Euler's number, e, is the base of
natural logarithms, and equals
approximately 2.71828. It is used to simplify certain formulas in
calculus. Compare log below.
--------------------------------------------------------------------------------
log ..... Returns the natural log
(base e) of a number. Compare exp above.
(log 3) returns 1.09861
(log 1.82) returns 0.598837
(log 0.25) returns -1.38629
--------------------------------------------------------------------------------
cvunit ..... Converts a value, or
a list of values, from one unit to
another. The first argument is the number to be converted, the second
argument is the unit from which you are converting, the third argument
is the unit to which you are converting.
(cvunit 10 "inch" "mm") returns
254.0
(cvunit 100 "mm" "inch") returns
3.93701
(cvunit 25 "lb" "kg") returns
11.3398
(cvunit (list 6 5 24) "inch"
"feet") returns (0.5 0.416667 2.0)
After (setq a 1 b 2)
(cvunit a "inch" "mm") returns
25.4
(cvunit (list (eval a) (eval b))
"inch" "mm") returns (25.4 50.8)
The unit names for the second and
third arguments must be spelled as
they appear in the ACAD.UNT file. The cvunit function depends on the
values in ACAD.UNT. Therefore, for most conversions, simple
multiplication or division by the appropriate factor within the
AutoLISP expression is preferred over dependence on an external file.
|