TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

Q about Exercise 13.3(b) in "ANSI Common Lisp"

2 pointsby paul_reinersabout 17 years ago
Has anyone else worked on exercise 13.3(b) in "ANSI Common Lisp"? I approached it by declaring all the variables in Figure 9.2 as long-floats:<p><pre><code> ; Use declarations in math utilities: (defun sq (x) (declare (long-float x)) (* x x)) (defun mag (x y z) (declare (long-float x y z)) (sqrt (+ (sq x) (sq y) (sq z)))) (defun unit-vector (x y z) (declare (long-float x y z)) (let ((d (mag x y z))) (declare (long-float d)) (values (/ x d) (/ y d) (/ z d)))) (defstruct (point (:conc-name nil)) x y z) (defun distance (p1 p2) (mag (- (x p1) (x p2)) (- (y p1) (y p2)) (- (z p1) (z p2)))) (defun minroot (a b c) (declare (long-float a b c)) (if (zerop a) (/ (- c) b) (let ((disc (- (sq b) (* 4 a c)))) (declare (long-float disc)) (unless (minusp disc) (let ((discrt (sqrt disc))) (declare (long-float discrt)) (min (/ (+ (- b) discrt) (* 2 a)) (/ (- (- b) discrt) (* 2 a)))))))) </code></pre> However, the running time slowed down as a result of this change, rather than sped up.<p>Note that I used long-floats, rather than double-floats, because, when I used double-floats, the output was slightly different (only off by 1 on some values) than the original version in which variables were undeclared. However, the output was slightly different with long-floats, too, so I guess that didn't help.<p>Anyway, should declaring these variables make the program faster, or am I doing the problem incorrectly? And why is the output slightly different if I'm using the largest size floating-points? Below is the diff:<p><pre><code> diff "spheres.pgm copy" spheres.pgm 125c125 &#60; 62 --- &#62; 61 179c179 &#60; 62 --- &#62; 61 6837,6838c6837,6838 &#60; 41 &#60; 117 --- &#62; 42 &#62; 118 6866,6867c6866,6867 &#60; 117 &#60; 41 --- &#62; 118 &#62; 42 7028c7028 &#60; 22 --- &#62; 23 7076c7076 &#60; 22 --- &#62; 23 7138c7138 &#60; 114 --- &#62; 113 7166c7166 &#60; 114 --- &#62; 113 7226c7226 &#60; 118 --- &#62; 117 7278c7278 &#60; 118 --- &#62; 117 7416,7417c7416,7417 &#60; 85 &#60; 54 --- &#62; 84 &#62; 53 7434c7434 &#60; 85 --- &#62; 86 7470c7470 &#60; 85 --- &#62; 86 7487,7488c7487,7488 &#60; 54 &#60; 85 --- &#62; 53 &#62; 84 7531c7531 &#60; 19 --- &#62; 18 7539c7539 &#60; 18 --- &#62; 19 7565c7565 &#60; 18 --- &#62; 19 7573c7573 &#60; 19 --- &#62; 18 7831c7831 &#60; 117 --- &#62; 116 7873c7873 &#60; 117 --- &#62; 116 7937c7937 &#60; 19 --- &#62; 18 7967c7967 &#60; 19 --- &#62; 18 8006c8006 &#60; 107 --- &#62; 108 8008c8008 &#60; 128 --- &#62; 127 8018c8018 &#60; 24 --- &#62; 23 8086c8086 &#60; 24 --- &#62; 23 8096c8096 &#60; 128 --- &#62; 127 8098c8098 &#60; 107 --- &#62; 108 8152c8152 &#60; 164 --- &#62; 163 8707c8707 &#60; 110 --- &#62; 111 8797c8797 &#60; 110 --- &#62; 111</code></pre>

1 comment

paul_reinersabout 17 years ago
I can't get any speed-up by using declarations. If anyone does, please let me know.