See screenshot
Strange Exponent Issue
Moderator: Moderators
-
- Vic 20 Newbie
- Posts: 3
- Joined: Fri May 31, 2013 5:28 am
Strange Exponent Issue
Just starting to get back into BASIC on the vic after 25+ years or so and I was playing around with the exponent function (x^2 type thing) and was checking that verses x*x for speed and for some reason using the x^2 was giving strange results with floating point outputs that didn't make sense (7^2 should be 49 not 49.0000001 shouldn't it?). Also the P*P is much faster that using the exponent..
See screenshot

See screenshot
OT: The whole thing should go into a FOR..NEXT loop (should be a bit faster than, too - and saves memory):
Code: Select all
20 PRINT "NUMBER","SQUARE"
30 FOR P=1 TO 8
40 PRINT P,P^2
50 NEXT
- Mike
- Herr VC
- Posts: 5134
- Joined: Wed Dec 01, 2004 1:57 pm
- Location: Munich, Germany
- Occupation: electrical engineer
Hi, Nibby99,
this thread should shed some more light on this.
In short, exponentiation is a much more complex operation than multiplication. For this reason, it is also prone to rounding errors, that's why you get a non-integer result with 7^2.

That being said, the arithmetic routines themselves are not too shabby speed-wise, when they are called directly from machine language. It might be an interesting exercise to come up with faster versions of the basic arithmetic (addition, subtraction, multiplication and division) without sacrifying accuracy.
this thread should shed some more light on this.

In short, exponentiation is a much more complex operation than multiplication. For this reason, it is also prone to rounding errors, that's why you get a non-integer result with 7^2.
It's still an interpreter, the routines are tuned for space and an acceptable speed in the general case. They could have implemented SQR() with Heron's Algorithm though, instead of doing X^(1/2).Kweepa wrote:VIC BASIC doesn't do any clever optimization tricks.

That being said, the arithmetic routines themselves are not too shabby speed-wise, when they are called directly from machine language. It might be an interesting exercise to come up with faster versions of the basic arithmetic (addition, subtraction, multiplication and division) without sacrifying accuracy.
Re: Strange Exponent Issue
It is a little known fact that the first Pentiums were prototyped using a modified VIC-20.Nibby99vic wrote:7^2 should be 49 not 49.0000001 shouldn't it?]
Re: Strange Exponent Issue
Indeed. Also, you may not have realised that the 'microcode' that drives all modern processors is actually 6502 assembly.RJBowman wrote:It is a little known fact that the first Pentiums were prototyped using a modified VIC-20.Nibby99vic wrote:7^2 should be 49 not 49.0000001 shouldn't it?]