SBASIC: Maximum and minimum value for a float

Anything QL Software or Programming Related.
Post Reply
User avatar
ql_freak
Gold Card
Posts: 354
Joined: Sun Jan 18, 2015 1:29 am

SBASIC: Maximum and minimum value for a float

Post by ql_freak »

When using functions which return a float, you often want to signal an error to the calling procedure. in C/C++ there exist NAN (or similar) which is no number, but can be returned. This can be interpreted as an error.

In SBASIC such a value doesn't exist to my knowledge. So a good candidate would e. g. be the smallest possible number, to signal an error. So I tinkered to find the smallest possible number:

-1.615850267e616

Even appending a zero to the mantissa will result in an error (try PRINT -1.6158502670e616)

You can e. g. use MIN_FLOAT$="-1.615850267e616": MIN_FLOAT=MIN_FLOAT$. To return an error from a function use:

oops=MIN_FLOAT$: REMark an error has occurred
return oops

The calling code then can test:

rslt=foo(expr)
if rslt=MAX_FLOAT THEN : PRINT"Error occurred in foo()!": STOP

I have tested, that this works (at least on QPC2, can anyone test on other machines, ROMs) with the attached SBASIC program.

The same magic number (without the minus sign) can be used for maximum possible float.
Attachments
minFloat_bas.jpg


http://peter-sulzer.bplaced.net
GERMAN! QL-Download page also available in English: GETLINE$() function, UNIX-like "ls" command, improved DIY-Toolkit function EDLINE$ - All with source. AND a good Python 3 Tutorial (German) for Win/UNIX :-)
Post Reply