Re: Yagl update(again) + coding problem.
- Posted by Pete Lomax <petelomax at blueyonder.co.uk> Aug 08, 2006
- 523 views
On Mon, 07 Aug 2006 12:09:01 -0700, Jeremy Peterson <guest at RapidEuphoria.com> wrote: >My problem has to do with this piece of code: > >}}} <eucode> >dist = sqrt((209+p[ix_part][a]-p[x_part][a])*(209[p[ix_part][a]-p[x_part][a])+ >(138+p[iy_part][a]-p[y_part][a])*(138+p[iy_part][a]-p[y_part[a])) ></eucode> {{{ As someone else has probably already said, error is in "*(209[p[" part, which should be "*(209+p[". At the end of the expression you will also find "p[y_part[a]))" which is missing a closing "]". However, there is a more grave error here in style. I rewrote that code firstly as follows (factoring out the common subscripts):
object pixa, pxa, piya, pya pixa = p[ix_part][a] pxa = p[x_part][a] piya = p[iy_part][a] pya = p[y_part][a] dist = sqrt((209+pixa-pxa)*(209+pixa-pxa)+ (138+piya-pya)*(138+piya-pya)) </eucode And immediately repeated with the new common expressions: }}} <eucode> object pixa, pxa, piya, pya object x,y pixa = p[ix_part][a] pxa = p[x_part][a] piya = p[iy_part][a] pya = p[y_part][a] x = 209+pixa-pxa y = 138+piya-pya dist = sqrt(x*x+ y*y) </eucode Now, I know what you are thinking - it'll be slower. No it will not! In fact I predict it will be nearly twice as fast because there are six expressions there now calculated once, not twice.
Internally the compiler converts complex expressions into simpler code like the above which uses "hidden" temporary work variables that behave exactly the same as any you explicitly create.
As well as the code being easier to write, easier to read, and faster, it will also be easier to debug because trace()/ex.err can show you a bit more directly what is happening.
Note that you should use atoms or integers rather than objects where possible, but maybe you can't here [?]. Obviously, I've used names such as pixa because I don't really know what the code is doing, (of course I have a much clearer idea after the rewrites), but you should be able to pick better & more meaningful names as well.
Regards, Pete }}}