How to best arrange expressions?
Harley
Posts: 997
Each item separately evaluates OK, but when combined can even be negative values. Samples can range from 10..200, cursor positions 1 and 2 can be 0..320 and bits is 12 with a clockfreq = 80 million. This to convert on-screen cursor differences to a time value.
Seems strange what's going on inside the Prop to use all positive values yet get strange values and even negative ones (even when curs2 larger than curs1).
time := (1000 * Samples * (curs2 - curs1) * (1<<bits))/clockfreq ' in milliseconds
Seems strange what's going on inside the Prop to use all positive values yet get strange values and even negative ones (even when curs2 larger than curs1).
Comments
Instead do : That will keep everything in range.
Yes, that cleared up the problem beautifully. Greater than 2^31 and the world goes negative. And what was strange was moving one or the other cursor slight amounts would display strange results, values not varying as expected, positive or negative values for slight movements.
If you make the assumption that clkfreq will be constant, you can replace (clkfreq/1000) with constant(80_000_000/1000) in the code. That will make the SPIN compiler evaluate it, instead of doing it in your running code. The result is a faster program.
If 'bits' is constant, you can do the same for it. The end result would look like this:
The standard SPIN compiler doesn't make any attempt to optimize constant portions of an expression, but you can do it by hand.
Jason