Page 1 of 2 12>
Topic Options
#79379 - 2003-01-08 06:27 PM iif() stack overflow exception "problem"?
Lee Wilmott Offline
Starting to like KiXtart

Registered: 2002-09-17
Posts: 106
Loc: Bristol, UK
I am using KiX version 4.20 Beta 1.

I wrote the following Exp() function some time ago which I find very helpful for use with binary bit manipulation.

code:
Function Exp($Base, $Power)
;Returns a base value raised to the specified power
If $Power = 0
$Exp = 1
Else
$Exp = Exp($Base, $Power - 1) * $Base
EndIf
EndFunction

Anyway, since the release of 4.20 albeit Beta, I thought I could re-write it using the new iif() function.

However, the following code produces a "Stack overflow exception" error...

code:
Function Exp2($Base, $Power)
;Returns a base value raised to the specified power
$Exp2 = iif($Power = 0, 1, Exp2($Base, $Power - 1) * $Base)
EndFunction

Admittedly the function names are different (so I could test them in the same script file), but aren't these two functions programatically identical?

Should I be receiving "Stack Overflow" errors?

Lee

Top
#79380 - 2003-01-08 06:34 PM Re: iif() stack overflow exception "problem"?
Lonkero Administrator Offline
KiX Master Guru
*****

Registered: 2001-06-05
Posts: 22346
Loc: OK
will need to wait ruud's answer on this.

anyway, I never thought of using iif in this kind of nesting manner.

the stack was enchanged some time ago due to similar problem I had with exchaustive nesting script.

so, the key in here is should the if shortcut (iif) work in this manner or is it's usage limited.

anyway, good you found a usage for it, I haven't yet.
_________________________
!

download KiXnet

Top
#79381 - 2003-01-08 07:02 PM Re: iif() stack overflow exception "problem"?
Shawn Administrator Offline
Administrator
*****

Registered: 1999-08-13
Posts: 8611
Lee,

I just converted your UDF to VBS and was utterly shocked when I got exactly the same error !!! Would have bet the farm that this was a bug.

C:\t.vbs(8, 2) Microsoft VBScript runtime error: Out of stack space: 'Exp'

Heres the code:

code:
 
WScript.Echo Exp(5,5)

Function Exp(Base, Power)

' Returns a base value raised to the specified power

Exp = IIF(Power = 0, 1, Exp(Base, Power - 1) * Base)

' If Power = 0 Then
' Exp = 1
' Else
' Exp = Exp(Base, Power - 1) * Base
' End If

End Function

There must be more going on here than meets the eye !!!

-Shawn

[ 08. January 2003, 19:02: Message edited by: Shawn ]

Top
#79382 - 2003-01-08 07:04 PM Re: iif() stack overflow exception "problem"?
Lee Wilmott Offline
Starting to like KiXtart

Registered: 2002-09-17
Posts: 106
Loc: Bristol, UK
Shawn,

I didn't think of testing it with VB!

Anyway, yeah that is interesting.

Do I assume that you guys agree with me? - this shouldn't return a stack overflow error!

Lee

Top
#79383 - 2003-01-08 07:11 PM Re: iif() stack overflow exception "problem"?
Shawn Administrator Offline
Administrator
*****

Registered: 1999-08-13
Posts: 8611
The only reason I converted it to VBS is because I know thats probably what Ruud would have done. Arguably since it traps in both langauges there is more going on here that we dont understand.

My quess is (bear with me) is that IIF always evaluates both expressions - even if it is proved false in the first IIF parm (expression) - and because your recursing inside the IIF - it very quickly runs out of stack space.

Trying to think of a proof for that theory...

-Shawn

[edit]

Now that I think on it more - pretty positive that is whats going on.

[ 08. January 2003, 19:16: Message edited by: Shawn ]

Top
#79384 - 2003-01-08 07:27 PM Re: iif() stack overflow exception "problem"?
Lonkero Administrator Offline
KiX Master Guru
*****

Registered: 2001-06-05
Posts: 22346
Loc: OK
shawn, not sure is this what you mean but this got into my mind.

as iif first executes it's parameters (like any function) it does not behave in expected matter.

it seems that it tries to count the second parameter and as it kind of calls itself, it will keep calling until the stack-space roof has been reached...

yes, made a test script to prove this.

code:
$var=1
$loopcount
test()

function test()
$loopcount=$loopcount+1
$loopcount ?
$var=iif($var=1,$var,test())
endfunction

_________________________
!

download KiXnet

Top
#79385 - 2003-01-08 07:47 PM Re: iif() stack overflow exception "problem"?
Shawn Administrator Offline
Administrator
*****

Registered: 1999-08-13
Posts: 8611
agreed. I (personally) would classify this as unexpected behavior because (imho) would have expected IIF only to evaluate the expression that was true or false depending. But since this occurs in VBS as well I guess that Microsofts expectations are different from yours, mine and Lee's.

Put another way - its as if IIF first evaluates both expressions, than returns the required result (depending on the first expression) as opposed to evaluting the first expression first, than returning the result of only the required expression - my head hurts.

[ 08. January 2003, 19:52: Message edited by: Shawn ]

Top
#79386 - 2003-01-08 07:51 PM Re: iif() stack overflow exception "problem"?
Lonkero Administrator Offline
KiX Master Guru
*****

Registered: 2001-06-05
Posts: 22346
Loc: OK
so, as thought this way, iif has to be always slower than just if...

now, if the else part would be optional...
_________________________
!

download KiXnet

Top
#79387 - 2003-01-08 07:56 PM Re: iif() stack overflow exception "problem"?
Shawn Administrator Offline
Administrator
*****

Registered: 1999-08-13
Posts: 8611
Would be curious to see if this behavior is exhibited in a compiled language where short-circuiting is possible ...
Top
#79388 - 2003-01-08 07:57 PM Re: iif() stack overflow exception "problem"?
Lonkero Administrator Offline
KiX Master Guru
*****

Registered: 2001-06-05
Posts: 22346
Loc: OK
btw, as I said it's unexpected, have to correct...

one would indeed think it to work otherwise, but as the general execution rules apply, it is expected behavior.

just the same way as any other operation.
good reference is those AND, OR and NOT thingies some of has hard time fitting into mind.
_________________________
!

download KiXnet

Top
#79389 - 2003-01-08 08:02 PM Re: iif() stack overflow exception "problem"?
Shawn Administrator Offline
Administrator
*****

Registered: 1999-08-13
Posts: 8611
lol - just tried this in Visual Studio VB.Net and got the following error:

An unhandled exception of type 'System.StackOverflowException' occurred in mscorlib.dll

Top
#79390 - 2003-01-08 08:04 PM Re: iif() stack overflow exception "problem"?
Lonkero Administrator Offline
KiX Master Guru
*****

Registered: 2001-06-05
Posts: 22346
Loc: OK
to better explain the behavior as expected, one might try:
code:
$var=1,0
$loopcount
test()

function test()
$test=0
$loopcount=$loopcount+1
? $loopcount

$var[test()]
endfunction

it does the same thing, but this is more easily understood as will not work.

well, adding there a switch that stops looping after 1000 loops, makes it work, but anyway.
_________________________
!

download KiXnet

Top
#79391 - 2003-01-08 08:08 PM Re: iif() stack overflow exception "problem"?
Shawn Administrator Offline
Administrator
*****

Registered: 1999-08-13
Posts: 8611
Heres another proof I guess:

code:
break on

$x = iif(1=1,FuncOne(),FuncTwo())

exit 1

function FuncOne()
?"FuncOne..."
endfunction

function FuncTwo()
?"FuncTwo..."
endfunction

Output is:

code:
C:\>kix32 t

FuncOne...
FuncTwo...



[ 08. January 2003, 20:11: Message edited by: Shawn ]

Top
#79392 - 2003-01-08 08:14 PM Re: iif() stack overflow exception "problem"?
Shawn Administrator Offline
Administrator
*****

Registered: 1999-08-13
Posts: 8611
Actually calling VB.Net a compiled language is probably being too nice - be curious to try VB6.
Top
#79393 - 2003-01-08 08:46 PM Re: iif() stack overflow exception "problem"?
Shawn Administrator Offline
Administrator
*****

Registered: 1999-08-13
Posts: 8611
I think we've exhausted this discussion and reached the conclusion that despite how we think IIF should function - this is just how IIF works and we should accept it as such !?!?!?

My personal feeling at this point is that Microsoft (not Ruud) made an extremely poor naming choice in calling IIF "IIF" ... its NOT an "Immediate IF" at all - its an ass-backward conditional expression evaluator (ABCEE ?) that in no way behaves like an normal (yet compressed) IF statement.

And I think that this understanding is crucial when using IIF because you certainly wouldn't want to be calling functions that made some sort of permanent change from within an IIF because its going to get done regardless of the outcome - so maybe functions are not valid food for the IIF !!!

[ 08. January 2003, 21:02: Message edited by: Shawn ]

Top
#79394 - 2003-01-08 10:10 PM Re: iif() stack overflow exception "problem"?
Lee Wilmott Offline
Starting to like KiXtart

Registered: 2002-09-17
Posts: 106
Loc: Bristol, UK
Interesting topic - I think!

Shawn,

Your earlier example...

quote:
code:
$x = iif(1=1,FuncOne(),FuncTwo())


Which display's the two strings defined in BOTH functions really surprised me!

I 100% agree with you, iif() is clearly NOT the same as if ... else ... .

Although I see no reason why this shouldn't work - I also believe that KiX should perform in the same way that VB does - for interoperability reasons if nothing else.

Having said all that, I would be very interested in Ruud's views on this matter.

Lee

Top
#79395 - 2003-01-08 10:31 PM Re: iif() stack overflow exception "problem"?
maciep Offline
Korg Regular
*****

Registered: 2002-06-14
Posts: 947
Loc: Pittsburgh
If it's any consolation, this seems to work:

code:
  

Function Exp2($Base, $Power)
;Returns a base value raised to the specified power
$ = iif($Power = 0, '$$Exp2 = 1', '$$Exp2 = Exp2($$Base, $$Power - 1) * $$Base')
$ = execute($)
EndFunction

_________________________
Eric

Top
#79396 - 2003-01-08 10:43 PM Re: iif() stack overflow exception "problem"?
Lonkero Administrator Offline
KiX Master Guru
*****

Registered: 2001-06-05
Posts: 22346
Loc: OK
maciep, yes. it proves the concept.

but is it anymore worth it?
simple if-else-endif works properly...
and likely is faster.

I think the kix.doc should state something about this...
_________________________
!

download KiXnet

Top
#79397 - 2003-01-08 11:13 PM Re: iif() stack overflow exception "problem"?
Shawn Administrator Offline
Administrator
*****

Registered: 1999-08-13
Posts: 8611
Guys (especially Jooel) my apologies - i'm such a windbag peabrain - in all my ranting i totally lost sight of the fact that IIF is a FUNCTION - not a CONSTRUCT ... of course it would behave this way - just like any other function (builtin or custom) all parameters would be evaluated before the function was entered. It was hard for me to think of IIF as a function (for some reason) ...

Only came to this (obvious?) realization on the train ride home and re-reading jooels earlier comments - Jooel, you should have like threw a book at my head or something to wake me up.

My apologies to Microsoft and Ruud as well.

-Shawn

[ 09. January 2003, 02:26: Message edited by: Shawn ]

Top
#79398 - 2003-01-08 11:19 PM Re: iif() stack overflow exception "problem"?
Shawn Administrator Offline
Administrator
*****

Registered: 1999-08-13
Posts: 8611
So really all IIF really is is this:

code:
Break On

$x = IIF2(1=1,1+1,2+2)

Exit 1

Function IIF2($x,$y,$z)

If $x
$IIF2=$y
Else
$IIF2=$z
EndIf

EndFunction

And to my mind, IIF is only really usefull for quick one-shot conditional assignments - using throw-away expressions. Comments ?

[ 09. January 2003, 01:25: Message edited by: Shawn ]

Top
Page 1 of 2 12>


Moderator:  ShaneEP, Arend_, Jochen, Radimus, Glenn Barnas, Allen, Ruud van Velsen, Mart 
Hop to:
Shout Box

Who's Online
0 registered and 718 anonymous users online.
Newest Members
Timothy, Jojo67, MaikSimon, kvn317, kixtarts2025
17874 Registered Users

Generated in 0.075 seconds in which 0.024 seconds were spent on a total of 13 queries. Zlib compression enabled.