Lisp HUG Maillist Archive

Problems with converting floats to integers

Hi
 
As I could understand from my 2 year Common Lisp experience, first result of TRUNCATE function was always integer, but recently I encountered a problem when result was atomagically converted back to float. I am not sure that problem lies in the TRUNCATE function. There are no arithmetic operations between place of TRUNCATE and place where argument must be integer, only LET bindings and passing to function call.
 
Simplification of my code looks like:
 
(defun caller-function (fn)
    (let ((float-var (get-float)))
        (funcall fn (truncate float-var))))
 
(let ((fn  nil))
   (defun init-fn ()
          (setf fn
               (lambda (fixnum-arg)
                        (do-something-with fixnum-arg))))
   
   (defun caller-loop ()
       (mp:process-run-function "Caller loop" '()
          (lambda ()
             (loop
               (caller-function fn)))))) 
 
In real code FN is not closure member, it is stored in slot, CALLER-LOOP and INIT-FN started inside CAPI message processing thread.
 
First question is "Can TRUNCATE result be float ?" (I couldn't find answer in Hyperspec).
Second - "Is problem in multithreading or so ? (Compiler bug ?)"
 
Thanks in advance
Lisper

Re: Problems with converting floats to integers

I'm a complete lisp newbie, but my reading of the hyperspec says that the values returned from truncate or ftruncate must be the *integers*:
http://docs.mandragor.org/files/Programming_languages/Lisp/Common_Lisp_HyperSpec_en/HyperSpec/Body/f_floorc.htm#truncate
truncate and ftruncate produce a quotient that has been truncated towards zero; that is, the quotient represents the mathematical integer of the same sign as the mathematical quotient, and that has the greatest integral magnitude not greater than that of the mathematical quotient.

From that, it appears truncate should not be the problem.

Cheers,
Chris

On 3/17/06, lisptracker < lisptracker@mail.ru> wrote:
Hi
 
As I could understand from my 2 year Common Lisp experience, first result of TRUNCATE function was always integer, but recently I encountered a problem when result was atomagically converted back to float. I am not sure that problem lies in the TRUNCATE function. There are no arithmetic operations between place of TRUNCATE and place where argument must be integer, only LET bindings and passing to function call.
 
Simplification of my code looks like:
 
(defun caller-function (fn)
    (let ((float-var (get-float)))
        (funcall fn (truncate float-var))))
 
(let ((fn  nil))
   (defun init-fn ()
          (setf fn
               (lambda (fixnum-arg)
                        (do-something-with fixnum-arg))))
   
   (defun caller-loop ()
       (mp:process-run-function "Caller loop" '()
          (lambda ()
             (loop
               (caller-function fn)))))) 
 
In real code FN is not closure member, it is stored in slot, CALLER-LOOP and INIT-FN started inside CAPI message processing thread.
 
First question is "Can TRUNCATE result be float ?" (I couldn't find answer in Hyperspec).
Second - "Is problem in multithreading or so ? (Compiler bug ?)"
 
Thanks in advance
Lisper

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