How do I test for integers

Discussions about the BBC BASIC language, with particular reference to BB4W and BBCSDL
nvingo
Posts: 42
Joined: Sat 28 May 2022, 22:40

Re: How do I test for integers

Post by nvingo »

Hated Moron wrote: Sat 04 Jun 2022, 15:24 More specifically, I would suggest that a function which can return either a string or a numeric is never a sensible approach, unless the result is used in a context when both types can be accepted, which practically means only in a PRINT list.
Thanks - let's hope the OP stops at my first implementation of FNinput() which maybe I ought to rename FNinput_positive_integer() for clarity ;)
As presented it would still fall over if too many digits are entered (number too big error) so maybe another function parameter to limit that should be added.
I wouldn't have even contemplated the possibility that different data types be returned had you not mentioned it.
Started using BASIC circa 1981 with CP/M, Video Genie, Sinclair ZX81, Acorn Atom, and progressed with ZX Spectrum, BBC Micro and Sinclair QL, Cambridge Z88, DOS, Windows. Wrote A-level project using school's BBC Micro with dual 800K floppy drive.
Hated Moron

Re: How do I test for integers

Post by Hated Moron »

nvingo wrote: Sat 04 Jun 2022, 15:30 I wouldn't have even contemplated the possibility that different data types be returned had you not mentioned it.
The OP referred to the Python type() function, which evidently takes as a parameter an object which may be a numeric or a string (or probably several other object types too). One could easily conclude, from reading the BBC BASIC documentation, that the language has no similar concept, but in order to support user-defined functions in fact it does, internally.

In some ways it's a shame that there is no BBC BASIC variable type that can hold such a 'true variant', especially as it would be very easy to create one (given a suitable type suffix being identified), but it's not something that one expects from a BASIC dialect and the possible applications for it would be few and far between.

Amusingly, in an early incarnation of BBC BASIC for SDL 2.0 a type-mismatch test was inadvertently omitted and you could write var = "string" without it throwing an error!