Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Integer division in Common Lisp?

When I do (/ 7 2), what should I do to get the result 3? If I do (/ 7 2.0), I get 3.5, which is as expected.

like image 460
appusajeev Avatar asked Jan 16 '10 09:01

appusajeev


2 Answers

(floor 7 2)

Ref: http://rosettacode.org/wiki/Basic_integer_arithmetic#Common_Lisp

like image 200
kennytm Avatar answered Oct 21 '22 13:10

kennytm


See FLOOR, CEILING and TRUNCATE in ANSI Common Lisp.

Examples (see the positive and negative numbers):

CL-USER 218 > (floor -5 2)
-3
1

CL-USER 219 > (ceiling -5 2)
-2
-1

CL-USER 220 > (truncate -5 2)
-2
-1

CL-USER 221 > (floor 5 2)
2
1

CL-USER 222 > (ceiling 5 2)
3
-1

CL-USER 223 > (truncate 5 2)
2
1

Usually for division to integer TRUNCATE is used.

like image 41
Rainer Joswig Avatar answered Oct 21 '22 14:10

Rainer Joswig