Page 1 of 1
Just estimate...
Posted: Sun May 27, 2007 12:27 pm
by Michael Vogel
...how long this program will take and what value will be displayed?
Code: Select all
a.q
cube.q
a=1
Repeat
a+1
cube=a*a*a
Until cube<>Pow(a,3)
Debug a
Posted: Sun May 27, 2007 12:55 pm
by Trond
Strange.
Posted: Sun May 27, 2007 1:04 pm
by dell_jockey
hmmm, on 4.01 (XP) I got '257', whose third power is only 16.974.593 - way, way below the maximum limit of a quad
Although you seem to have stumbled over something, it's perfectly sensible: POW expects float arguments (so 'a' will be typecast to a float by POW) and returns a float. 16.974.593 is still way below the maximum limits of floats too, but floating point rounding errors WILL creep in, so it's actually astonishing the loop gets executed that often, really.
It goes to show that one shouldn't compare floats with ints/quads...
Posted: Sun May 27, 2007 1:43 pm
by Dr. Dri
it's weird indeed
Code: Select all
a.q
cube.q
a=1
Repeat
a+1
cube=a*a*a
Until cube <> Pow(a,3)
Debug a
Debug cube
Debug Pow(a, 3)
[EDIT]
i got it !
pow is interpreted as a float in the test and as a double in the debug
Code: Select all
a.q = 257
cube_q.q = a * a * a
cube_f.f = a * a * a
cube_d.d = a * a * a
Debug cube_q
Debug cube_f
Debug cube_d
Debug Pow(a, 3)
Dri
Posted: Sun May 27, 2007 1:48 pm
by Trond
The problem is not in the quad/int/float comparision, it's inside Pow():
PB actually has two Pow()s. The double precision one returns the correct answer, the single precision one does not. In my opinion it should, since it's within the range of a float.
Posted: Sun May 27, 2007 1:50 pm
by Kaeru Gaman
I think it's the coparison.
change the one line into:
and the code will run until the limit of a LONG.
Posted: Sun May 27, 2007 2:23 pm
by Trond
It's not the comparision. When you use Int() the double precision version of Pow() is used.
Posted: Sun May 27, 2007 2:38 pm
by Kaeru Gaman
> When you use Int() the double precision version of Pow() is used.
where can I get more information about the two versions of Pow()?
how can I force the use of one of them?
> the single precision one does not. In my opinion it should, since it's within the range of a float.
not only the range, also the precision counts.
a 4byte float uses 23bit for the mantissa.
that means, if the value exceeds 2^24 = 16.777.216, the precision gets worse than 1.
(meaning, a variance grater 1 can occur)
as you see, the code runs out when Pow(a,3) = 16.974.593,
wich is the first cube that is bigger than 2^24
the last attempt that works is 256^3, wich is 2^24....

Posted: Sun May 27, 2007 2:51 pm
by Trond
Kaeru Gaman wrote:> When you use Int() the double precision version of Pow() is used.
where can I get more information about the two versions of Pow()?
how can I force the use of one of them?
Look at the generated asm code.