?FORMULA TOO COMPLEX

Basic and Machine Language

Moderator: Moderators

Post Reply
wimoos
Vic 20 Afficionado
Posts: 345
Joined: Tue Apr 14, 2009 8:15 am
Website: http://wimbasic.webs.com
Location: Netherlands
Occupation: farmer

?FORMULA TOO COMPLEX

Post by wimoos »

Hello all,

Anybody ever witnessed a "?FORMULA TOO COMPLEX" error from a CBM Basic V2.0 program (that is, without any extensions or ML additions).
To my opinion, this error can never occur. Can you think of a program (line) that proves otherwise ?

Regards,


Wim.
VICE; selfwritten 65asmgen; tasm; maintainer of WimBasic
User avatar
Mike
Herr VC
Posts: 4816
Joined: Wed Dec 01, 2004 1:57 pm
Location: Munich, Germany
Occupation: electrical engineer

Re: ?FORMULA TOO COMPLEX

Post by Mike »

wimoos wrote:Anybody ever witnessed a "?FORMULA TOO COMPLEX" error from a CBM Basic V2.0 program (that is, without any extensions or ML additions). To my opinion, this error can never occur. Can you think of a program (line) that proves otherwise ?
The stack for string expressions is only 3 strings deep. 1 PRINT "A"+("B"+("C"+"D")) throws a ?FORMULA TOO COMPLEX error, whereas 1 PRINT "A"+("B"+"C") doesn't.
wimoos
Vic 20 Afficionado
Posts: 345
Joined: Tue Apr 14, 2009 8:15 am
Website: http://wimbasic.webs.com
Location: Netherlands
Occupation: farmer

Re: ?FORMULA TOO COMPLEX

Post by wimoos »

The stack for string expressions is only 3 strings deep. 1 PRINT "A"+("B"+("C"+"D")) throws a ?FORMULA TOO COMPLEX error, whereas 1 PRINT "A"+("B"+"C") doesn't.
Hmmm....who would ever use brackets in combination with string concatenation: there is no automatic hierarchy in this that needs to be overruled. Moreover, the formula isn't really complex, is it ?

But this gave me the idea that the use of LEFT$/MID$/RIGHT$/CHR$/STR$ functions lead to similar effects. For example

Code: Select all

PRINT "A"+LEFT$("B"+MID$("C"+RIGHT$(CHR$(68)+"E",2),1),2)
will produce a ?FORMULA TOO COMPLEX error as well.

Now comes the question: why a stack of only three strings deep ? Of course limited by Zp location, but they must have thought a minimal of three required. What is the thought behind that ?
VICE; selfwritten 65asmgen; tasm; maintainer of WimBasic
User avatar
Mike
Herr VC
Posts: 4816
Joined: Wed Dec 01, 2004 1:57 pm
Location: Munich, Germany
Occupation: electrical engineer

Re: ?FORMULA TOO COMPLEX

Post by Mike »

wimoos wrote:why a stack of only three strings deep? [...] What is the thought behind that?
That setup is probably sufficient for most non-trivial string expressions. For example, even something like

A$=LEFT$(LEFT$(A$,M-1)+LEFT$(B$,N)+MID$(A$,M+LEN(LEFT$(B$,N))),LEN(A$))

works and gives the same result as the left-MID$() assignment in BASIC V3.5/V7 and WimBasic:

MID$(A$,M,N)=B$
wimoos
Vic 20 Afficionado
Posts: 345
Joined: Tue Apr 14, 2009 8:15 am
Website: http://wimbasic.webs.com
Location: Netherlands
Occupation: farmer

Re: ?FORMULA TOO COMPLEX

Post by wimoos »

Hmm...good example, thank you for that.

Now I compared the vanilla Basic formula with the Left-MID$ WimBasic implementation. I compared them on usage of strings scrap space and on the usage of the temporary descriptor stack.

The vanilla implementation uses more scrap space than the WimBasic equivalent.
Also, vanilla uses two entries on the descriptor stack, while the WimBasic implementation uses only one entry on the stack.

Chances are that vanilla runs into a garbage collection quicker and into a FORMULA TOO COMPLEX quicker when more elaborate strings are used.
Last edited by wimoos on Mon Nov 06, 2017 12:13 pm, edited 1 time in total.
VICE; selfwritten 65asmgen; tasm; maintainer of WimBasic
User avatar
Mike
Herr VC
Posts: 4816
Joined: Wed Dec 01, 2004 1:57 pm
Location: Munich, Germany
Occupation: electrical engineer

Re: ?FORMULA TOO COMPLEX

Post by Mike »

That's fair enough. Just two things I'd like to point out:

1. Ideally, an implementation of "MID$()=" shouldn't need any more temporary stack space than necessary for the RHS expression. The exchange of the string content LHS can operate in-place.

2. GC, as bad as it's implemented in BASIC V2, only becomes relevant with a 3-digit number of "active" strings, where it then can take a noticable couple of seconds, lots of minutes, even going into one to two hours. Yet this didn't keep me from using a high two-digit number of strings - in the game VICtoria Gold - to store shape definitions of the provinces for the graphic map. When a GC occurs there, it only takes ~1/2 second.

For applications that require to handle an even higher number of strings, support by machine language routines is probably the best bet: that's the route I went with the formatting process of MG Browse. If I had stored each text line into a separate BASIC string, GC would have become unbearable pretty soon.

... http://sleepingelephant.com/ipw-web/bul ... 9&start=88 ... ;)
norm8332
Vic 20 Nerd
Posts: 626
Joined: Sun Nov 13, 2016 11:04 am
Location: USA

Re: ?FORMULA TOO COMPLEX

Post by norm8332 »

On a side note, Mike that game you point out ( VICtoria Gold), isn't available for download anymore the links are broken. I for one would like to try it, it looks like a cool game.

Norm
“In religion and politics people’s beliefs and convictions are in almost every case gotten at second-hand, and without examination... whose opinions about them were not worth a brass farthing.”

-Autobiography of Mark Twain
User avatar
Mike
Herr VC
Posts: 4816
Joined: Wed Dec 01, 2004 1:57 pm
Location: Munich, Germany
Occupation: electrical engineer

Re: ?FORMULA TOO COMPLEX

Post by Mike »

Thank you for the hint - here's a working link:

https://dateipfa.de/.Public/denial/games/victoria.zip

I'll also repair the link in the OP and the announcement thread. :)
User avatar
orion70
VICtalian
Posts: 4337
Joined: Thu Feb 02, 2006 4:45 am
Location: Piacenza, Italy
Occupation: Biologist

Re: ?FORMULA TOO COMPLEX

Post by orion70 »

Thx :)
User avatar
J.E.E.K.
Vic 20 Drifter
Posts: 23
Joined: Wed Jan 25, 2017 12:31 pm
Website: http://klasek.at/8bit
Location: AT

Re: ?FORMULA TOO COMPLEX

Post by J.E.E.K. »

Not relevant in real use, but some V2 bugs may provoke a string descriptor stack overflow leading to a ?FORMULA TOO COMPLEX error.

PRINT POS("")
PRINT POS("")
PRINT POS("")

Throws the error on the 3rd one. In contrast to POS() FRE() doesn't test the dummy argument type also, but a proper argument cleanup will be done.

However, this could be used to artificially shorten the string descriptor stack (for experimenting with the interpreter) because each POS("") occupies one stack element. As far as I know this remains permanent until a CLR or RUN. Further usage of expression evaluation does not recognize the useless stack descriptor entry.
wimoos
Vic 20 Afficionado
Posts: 345
Joined: Tue Apr 14, 2009 8:15 am
Website: http://wimbasic.webs.com
Location: Netherlands
Occupation: farmer

Re: ?FORMULA TOO COMPLEX

Post by wimoos »

As far as I know this remains permanent until a CLR or RUN.
This is correct. Additionally NEW, and non-immediate LOAD, do this. Also the routine that prints "ERROR" whenever an error occurs does a call to $C67A where the string descriptor stackpointer is reset.

In WimBasic, I have done some maintenance on PRINT USING. This, and the POS("") bug, now makes that

Code: Select all

PRINT USING "##", POS("")
results in a ?FORMULA TOO COMPLEX straight away. Where

Code: Select all

PRINT USING "##", ASC("A")
works properly.

Regards,

Wim.
VICE; selfwritten 65asmgen; tasm; maintainer of WimBasic
wimoos
Vic 20 Afficionado
Posts: 345
Joined: Tue Apr 14, 2009 8:15 am
Website: http://wimbasic.webs.com
Location: Netherlands
Occupation: farmer

Re: ?FORMULA TOO COMPLEX

Post by wimoos »

A similar bug as in POS(""), is in IF.

Code: Select all

10 IF " " THEN X=1
20 IF " " THEN X=2
30 IF " " THEN X=3
40 IF " " THEN X=4
results in ?FORMULA TOO COMPLEX at line 40.

No one would ever be tempted to code this, but the handling is wrong. There should be a ?TYPE MISMATCH at line 10.

Regards,

Wim.
VICE; selfwritten 65asmgen; tasm; maintainer of WimBasic
User avatar
J.E.E.K.
Vic 20 Drifter
Posts: 23
Joined: Wed Jan 25, 2017 12:31 pm
Website: http://klasek.at/8bit
Location: AT

Re: ?FORMULA TOO COMPLEX

Post by J.E.E.K. »

wimoos wrote:A similar bug as in POS(""), is in IF.
[..]
No one would ever be tempted to code this, but the handling is wrong. There should be a ?TYPE MISMATCH at line 10.
More curiosities regarding IF can be found in the german C64-Wiki (the english version isn't up-to-date).
Post Reply