Re: Division inconsistency between interpreted and compiled Lush
Yury Sulsky <[email protected]> Wed, 11 Jan 2006 01:26:34 -0500
| Newsgroups | gmane.lisp.lush.devel |
|---|---|
| Message-ID | <[email protected]> |
This is a bit late... On 12/24/05, Yann LeCun <[email protected]> wrote: > Well, I'm not sure how to fix this either, short of extending the > interpreter to handle different numerical types, which is a > non-trivial job (right now, everything is a double in interpreted > mode). > > Casting args of / into double would only be a partial solution. The > same other operation also have different behaviors for different > numerical types. One potential (but rather annoying) solution is to > have numerical functions cast their arguments to a single type (double > for most ops) and return a single type, and make the compiler complain > when the result is assigned to a different type without explicit > conversion. > > > What's the file that translates arithmetic functions into C operations? Is > > it dz.c? > > Most of it is in lsh/compiler/dh-macro.lsh > Look for "dhm-c /" > > -- Yann > > > > Hi, I'm not sure where to fix this, but I think the compiler should cast > > any integer arguments to the / function into floats. Right now, both unary > > and binary division work differently in interpreted code and in compiled > > code. > > > > What's the file that translates arithmetic functions into C operations? Is > > it dz.c? > > > > Yury It seems to me that the division functions "/", "2/" suffer the most from this. What other functions return different values if used with integers? The patch below fixes this. Is it okay to commit it? Yury Index: lsh/compiler/dh-macro.lsh =================================================================== RCS file: /cvsroot/lush/lush/lsh/compiler/dh-macro.lsh,v retrieving revision 1.47 diff -u -r1.47 dh-macro.lsh --- lsh/compiler/dh-macro.lsh 7 Sep 2005 18:05:30 -0000 1.47 +++ lsh/compiler/dh-macro.lsh 11 Jan 2006 06:17:41 -0000 @@ -1123,7 +1123,11 @@ (dhm-t /(source) (when (and (<> 2 (length source)) (<> 3 (length source)) ) (dhc-error "wrong number of arguments")) - (process-numerical-args-t (cdr source))) + (let ((tnode (process-numerical-args-t (cdr source)))) + ;; integer division in C isn't used + (when (==> :tnode:type is-an-int) + (setq :tnode:type (new dhc-type 'dht-real))) + tnode)) (de dhc-c-cast (type) (if (==> type is-a-real) "(real)" @@ -1133,10 +1137,10 @@ (let ((arg1 (dhc-parse-expr-c (cadr source) (cadr :treetype:tn-list) ()))) (if (= (length source) 2) - (sprintf "(1/%s%s)" (dhc-c-cast :(cadr :treetype:tn-list):type) + (sprintf "(1/%s%s)" (dhc-c-cast :treetype:type) arg1) (sprintf "(%s/%s%s)" - arg1 (dhc-c-cast :(cadr :treetype:tn-list):type) + arg1 (dhc-c-cast :treetype:type) (dhc-parse-expr-c (caddr source) (caddr :treetype:tn-list) ()))))) @@ -1281,7 +1285,7 @@ (generate-monadic '1+ "((%s)+1)" "((%s)+1)" "((%s)+1)") (generate-monadic '1- "((%s)-1)" "((%s)-1)" "((%s)-1)") (generate-monadic '2* "((%s)*2)" "((%s)*2)" "((%s)*2)") -(generate-monadic '2/ "((%s)/2)" "((%s)/2)" "((%s)/2)") +(generate-monadic '2/ "((%s)/2.0)" "((%s)/2.0f)" ()) (dhm-t seed(source) (if (<> (length source) 2) ------------------------------------------------------- This SF.net email is sponsored by: Splunk Inc. Do you grep through log files for problems? Stop! Download the new AJAX search engine that makes searching your log files as easy as surfing the web. DOWNLOAD SPLUNK! http://ads.osdn.com/?ad_idv37&alloc_id865&op=click