Floating-point answer needed

This function is supposed to round a number to some given number of decimal places, returning a floating-point answer:

(DEFUN DECIMAL-PLACES (NUM PLACES)
  (LET ((SCALE (EXPT 10 PLACES)))
    (/ (ROUND (* NUM SCALE))
       SCALE)))

However, these results demonstrate that the present implementation isn’t returning floats:

(LIST (DECIMAL-PLACES 0.49 1)
      (DECIMAL-PLACES 1 2)
      (DECIMAL-PLACES 1/3 3))

=> (1/2 1 333/1000)
Filed under  //   numbers  

About

A growing compendium of erroneous Common Lisp code snippets by Ismail Ikram.

Lisp novices might try to correct these as a learning exercise. Need some help or just feel like nitpicking? Do post a comment and let me know.

Twitter