Just estimate...

Everything else that doesn't fall into one of the other PB categories.
User avatar
Michael Vogel
Addict
Addict
Posts: 2820
Joined: Thu Feb 09, 2006 11:27 pm
Contact:

Just estimate...

Post 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
Trond
Always Here
Always Here
Posts: 7446
Joined: Mon Sep 22, 2003 6:45 pm
Location: Norway

Post by Trond »

Strange.
dell_jockey
Enthusiast
Enthusiast
Posts: 767
Joined: Sat Jan 24, 2004 6:56 pm

Post 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...
cheers,
dell_jockey
________
http://blog.forex-trading-ideas.com
Dr. Dri
Enthusiast
Enthusiast
Posts: 243
Joined: Sat Aug 23, 2003 6:45 pm

Post 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
Trond
Always Here
Always Here
Posts: 7446
Joined: Mon Sep 22, 2003 6:45 pm
Location: Norway

Post by Trond »

The problem is not in the quad/int/float comparision, it's inside Pow():

Code: Select all

cube.f = Pow(257., 3.)
Debug cube
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.
User avatar
Kaeru Gaman
Addict
Addict
Posts: 4826
Joined: Sun Mar 19, 2006 1:57 pm
Location: Germany

Post by Kaeru Gaman »

I think it's the coparison.

change the one line into:

Code: Select all

Until cube <> Int(Pow(a,3))
and the code will run until the limit of a LONG.
oh... and have a nice day.
Trond
Always Here
Always Here
Posts: 7446
Joined: Mon Sep 22, 2003 6:45 pm
Location: Norway

Post by Trond »

It's not the comparision. When you use Int() the double precision version of Pow() is used.
User avatar
Kaeru Gaman
Addict
Addict
Posts: 4826
Joined: Sun Mar 19, 2006 1:57 pm
Location: Germany

Post 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.... ;)
oh... and have a nice day.
Trond
Always Here
Always Here
Posts: 7446
Joined: Mon Sep 22, 2003 6:45 pm
Location: Norway

Post 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.
Post Reply