Arithmetic/Integer: Difference between revisions

Content added Content deleted
(→‎{{header|Common Lisp}}: Rewritten to fit task description.)
Line 124: Line 124:
=={{header|Common Lisp}}==
=={{header|Common Lisp}}==


<lang lisp>(defun arithmetic ()
<lang lisp>(defun arithmetic (&aux (a (read *query-io*)) (b (read *query-io*)))
(mapc
(let ((a (read *query-io*)) (b (read *query-io*)))
(format t "a + b = ~a~%" (+ a b))
(lambda (op &aux (form (list op a b)))
(format t "a - b = ~a~%" (- a b))
(format t "~a => ~a~%" form (eval form)))
(format t "a * b = ~a~%" (* a b))
'(+ - * mod rem floor ceiling truncate round expt)))</lang>

(format t "a / b = ~a~%" (/ a b))
Common Lisp's integer division functions are <code>floor</code>, <code>ceiling</code>, <code>truncate</code>, and <code>round</code>. They differ in how they round their quotient.
(format t "a % b = ~a~%" (mod a b))))</lang>

{| class="wikitable"
! The function !! rounds its quotient towards
|-
! <code>floor</code>
| negative infinity
|-
! <code>ceiling</code>
| positive infinity
|-
! <code>truncate</code>
| zero
|-
! <code>round</code>
| the nearest integer (preferring the even integer if the mathematical quotient is equidistant from two integers)
|}

Each function also returns a remainder as its secondary value, such that
quotient * divisor + remainder = dividend .
<code>(mod a b)</code> and <code>(rem a b)</code> return numbers equal to the secondary values of <code>(floor a b)</code> and <code>(truncate a b)</code>, respectively.


=={{header|D}}==
=={{header|D}}==