User Tools

Site Tools


ch2_3_arithm

Arithmetic functions

Simple math functions. Remeber the functional notation, the operator always appears at the first position. The artihmetic operations work on integers or floats separatelly. When using mixed integers and floats, the return will be float. When diving integers, it will be taken as be integer division.

LabLisp does not support the advanced LISP number handling, like rational numbers or bignums.

+ - * /

Accept any number of paramterers, at least one.

>(+ 1 2 3 4)     ; plus is simple (1+2+3+4)
10
 
>(- 4 3 2 1)     ; means (4-3-2-1)
-2
 
>(- 4)           ; single argument is taken as (-4)
-4
 
>(* 1 2 3 4)     ; times is simple (1*2*3*4)
24

Division can be integer or float. Division by zero produces NIL, similar to NAN in C++.

>(/ 12 3 2)      ; is (12/3)/2
2
 
>(/ 12 3.0 2)    ; poisoned by float 3.0
2.0
 
>(/ 2.0)         ; float division (1.0/2.0)
0.5
 
>(/ 1 0)         ; wrong
;Division by zero.
NIL              ; returns NIL

1+ 1-

Integer increment and decrement functions. Accept single integer argument.

>(1+ 4)     ; one plus is simple (1+4)
5
 
>(1- 4)     ; means (4-1)
3

ASH

Arithmetic shift on integer. Accepts two integer parameters, first is the number to be shifted, the second represents the shift in number of digits in the binary representation.

>(ash 7 2)     ; 7 is 111 binary
28             ; 28 is 11100 binary, shifted by two digits
 
>(ash 73 -3)   ; 73 is 1001001 
9              ; 9 is 1001, shifted back by three digits

ROUND

Rounds number to integer value. Returns multiple values: the integer result and float leftover.

>(ROUND 2.1)
2
0.1
ch2_3_arithm.txt · Last modified: 2022/02/25 00:40 by admin

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki