Lisp HUG Maillist Archive

Found bug in truncating floats to integers ???

Hi
 
I've found how to simply reproduce that case when result is float despite of TRUNCATE call.
 
(defun test-bug (value fn)
  (let ((pseudo-fixnum 5))
    (declare (fixnum pseudo-fixnum))
    (setf pseudo-fixnum (+ value pseudo-fixnum))
    (funcall fn (truncate pseudo-fixnum))))
   
 
(defun bug-raise (fixnum-value)
  (declare (fixnum fixnum-value))
  (format t "Value = ~D~%" fixnum-value))
 
(test-bug 5.0 #'bug-raise)
 
You should see in output window that "Value = 10.0" instead of expected "Value = 10" (because it should be converted to integer by TRUNCATE function).
 
I know that it is wrong using of declarations, but this makes possible to pass
float instead of integer even if argument variable is declared fixnum and truncated before passing to another function, and root of this error is comparatively hard to detect.
 
Regards
Lisper

Re: Found bug in truncating floats to integers ???

Hello lisptracker,

| I've found how to simply reproduce that case when result is float
| despite of TRUNCATE call.
|
| (defun test-bug (value fn)
|   (let ((pseudo-fixnum 5))
|     (declare (fixnum pseudo-fixnum))
|     (setf pseudo-fixnum (+ value pseudo-fixnum))
|     (funcall fn (truncate pseudo-fixnum))))
|
|
| (defun bug-raise (fixnum-value)
|   (declare (fixnum fixnum-value))
|   (format t "Value = ~D~%" fixnum-value))
|
| (test-bug 5.0 #'bug-raise)
|
| You should see in output window that "Value = 10.0" instead of expected
| "Value = 10" (because it should be converted to integer by TRUNCATE
| function).
|
| I know that it is wrong using of declarations, but this makes possible
| to pass float instead of integer even if argument variable is declared
| fixnum and truncated before passing to another function, and root of
| this error is comparatively hard to detect.

This is not a bug - this is you to blame. Below is the excerpt from CLHS.

A type declaration is valid in all declarations. The interpretation of a
type declaration is as follows:

  1. During the execution of any reference to the declared variable within
the scope of the declaration, the consequences are undefined if the value of
the declared variable is not of the declared type.

  2. During the execution of any setq of the declared variable within the
scope of the declaration, the consequences are undefined if the newly
assigned value of the declared variable is not of the declared type.

  3. At the moment the scope of the declaration is entered, the consequences
are undefined if the value of the declared variable is not of the declared
type.
--
Sincerely,
Dmitriy Ivanov
lisp.ystok.ru


Re: Found bug in truncating floats to integers ???

Unable to parse email body. Email id is 5436

Updated at: 2020-12-10 08:48 UTC