Page 1 of 1

String + Numbers .... help!

Posted: Wed Apr 22, 2020 5:07 pm
by genetika
Hi.
I need to convert this BBC BASIC lines in SuperBASIC....

LET E=10
LET TIME=50
PRINT "Line:"+STR$(E)+" X "+STR$(TIME)

I would like to obtain this output="Line:10 X 50"

HELP!

:P

Re: String + Numbers .... help!

Posted: Wed Apr 22, 2020 5:24 pm
by tofro
genetika wrote:Hi.
I need to convert this BBC BASIC lines in SuperBASIC....

LET E=10
LET TIME=50
PRINT "Line:"+STR$(E)+" X "+STR$(TIME)

I would like to obtain this output="Line:10 X 50"

HELP!

:P
That's actually pretty simple in SuperBASIC:

Code: Select all

PRINT "Line:" & E & " X " & TIME
That works because S*BASIC does what's called "coercion" (that is, type conversion) between strings and numbers when needed. S*BASIC sees we want to build a string and converts the numbers into ASCII. That works, BTW, also the other way round:

Code: Select all

a$ = "10" : PRINT 1 + a$
will print "11"

Re: String + Numbers .... help!

Posted: Thu Apr 23, 2020 7:02 am
by polka
Actually, it is not so simple :

Let's try :

A = 60000000
PRINT#2,A

The QL will answer (on device #2) :

6E7

If you want him to print

60000000

you have to code this little procedure :

Code: Select all

1 DEFine PROCedure IP(device,precision,value)
2    f$=""
3    FOR i = 1 to precision
4       f$ = f$ & "#"
5    END FOR i
6    PRINT_USING#device,f$,value
7 END DEFine IP
and tell the QL :

IP(2,8,A)

(2 is the number of the device on which you want the output, and 8 is the number of characters you want to print)

Coercion etc. is "nice", but it lets the QL do what he wants, not what you want.

POLKa

Re: String + Numbers .... help!

Posted: Thu Apr 23, 2020 7:07 am
by tofro
Right - If you want to have control of the format, you can't use automagic coercion.

But I'm pretty sure the BBC Basic version above would have reverted to exponential notation as well - with such big numbers (The example doesn't seem to use numbers in that range)

Tobias

Re: String + Numbers .... help!

Posted: Thu Apr 23, 2020 7:15 am
by polka
I stumbled on this problem recently, as I was trying to code a simple model for an epidemy propagating in a population of 60000000. This number was too large for using QL integers (limited to 32767), and when using floats, the QL would not format the output to my will. :mrgreen:

Of course, I should have programmed it in Forth, that has only integers but up to 32 bits ; but I was lazy :oops:

POLKa

Re: String + Numbers .... help!

Posted: Thu Apr 23, 2020 8:04 am
by Giorgio Garabello
polka wrote:Actually, it is not so simple :

Let's try :

A = 60000000
PRINT#2,A

The QL will answer (on device #2) :

6E7

If you want him to print

60000000

you have to code this little procedure :

Code: Select all

1 DEFine PROCedure IP(device,precision,value)
2    f$=""
3    FOR i = 1 to precision
4       f$ = f$ & "#"
5    END FOR i
6    PRINT_USING#device,f$,value
7 END DEFine IP
and tell the QL :

IP(2,8,A)

(2 is the number of the device on which you want the output, and 8 is the number of characters you want to print)

Coercion etc. is "nice", but it lets the QL do what he wants, not what you want.

POLKa
Try this: https://superbasic-manual.readthedocs.i ... ht=fdec%24