math range errors in EVAL not raised/bogus results

Discussions relevant to the differences between LBB and LB4 which could affect code compatibility
tsh73
Posts: 43
Joined: Fri Apr 06, 2018 7:58 pm

math range errors in EVAL not raised/bogus results

Post by tsh73 »

(tripped upon while working on
chart library
viewtopic.php?f=11&p=154#p154
)
I was going to change function evalFuncX(f$,x)) so in case of math range error it returned magic number. So I could omit these ranges.
But it seems LBB has problems reporting such errors from EVAL
- no errors happens, weird results are returned.
Things I found:

sqr(negative number) - no error in EVAL, bogus result

Code: Select all

'print sqr(-2)    'Negative root
print eval("sqr(-2)")    '-2 ???
print eval("sqr(1.2-3)")    '-3 ???
fractional power (((negative number))^0.5) - errors OK

Code: Select all

'print (-1)^0.5  'Logarithm range
'print eval("(-1)^0.5")'Logarithm range
division by zero is intercepted

Code: Select all

'print 1/0   'div by zero
'print eval("1/0")  'div by zero 
x mod 0 - no error in EVAL, bogus result

Code: Select all

'print 2 mod 0   'div by zero
print eval("2 mod 0")  '2
exp overflow - no error in EVAL, 0 returned

Code: Select all

'print exp(40000)    'exponent range
print eval("exp(40000)")    '0
log(negative number) - no error in EVAL, bogus result

Code: Select all

'print log(-2)   'logarithm range
print eval("log(-2)")  '-2
argument out of range for asc, asn - no error in EVAL, 0 returned

Code: Select all

'print acs(2)   'negative root - how's that?
print eval("acs(2)")  '0

Code: Select all

'print asn(2)   'negative root - how's that?
print eval("asn(2)")  '0
guest
Site Admin
Posts: 192
Joined: Tue Apr 03, 2018 1:34 pm

Re: math range errors in EVAL not raised/bogus results

Post by guest »

tsh73 wrote: Fri Sep 14, 2018 8:20 pmBut it seems LBB has problems reporting such errors from EVAL
This issue arises from the fact that BBC BASIC's EVAL can return either a number or a string, whereas LB's EVAL has two different forms: EVAL() to return a number and EVAL$() to return a string. The way LBB deals with this difference is by means of error trapping: it initially tries to assign the result of EVAL to a numeric variable and if that results in an error it tries to assign it to a string variable. An unfortunate consequence is that if there is a 'genuine' error in the expression it may not be reported, as you have found.

One way in which you could attempt to work around this limitation is by calling BBC BASIC's EVAL function directly:

Code: Select all

!*lowercase
!n = EVAL("sqr(-2)")
Now an error ('Negative root') is reported.