String + Numbers .... help!

Anything QL Software or Programming Related.
User avatar
genetika
Brittle Membrane
Posts: 118
Joined: Thu May 05, 2011 5:38 pm
Location: Rome, ITALY
Contact:

String + Numbers .... help!

Postby genetika » Wed Apr 22, 2020 5:07 pm

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


User avatar
tofro
QL Wafer Drive
Posts: 1958
Joined: Sun Feb 13, 2011 10:53 pm
Location: SW Germany

Re: String + Numbers .... help!

Postby tofro » Wed Apr 22, 2020 5:24 pm

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"


ʎɐqǝ ɯoɹɟ ǝq oʇ ƃuᴉoƃ ʇou sᴉ pɹɐoqʎǝʞ ʇxǝu ʎɯ 'ɹɐǝp ɥO
User avatar
polka
Over Heated PSU
Posts: 144
Joined: Mon Mar 07, 2011 11:43 am

Re: String + Numbers .... help!

Postby polka » Thu Apr 23, 2020 7:02 am

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
Last edited by polka on Thu Apr 23, 2020 7:09 am, edited 1 time in total.


May the FORTH be with you !
POLKa
User avatar
tofro
QL Wafer Drive
Posts: 1958
Joined: Sun Feb 13, 2011 10:53 pm
Location: SW Germany

Re: String + Numbers .... help!

Postby tofro » Thu Apr 23, 2020 7:07 am

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


ʎɐqǝ ɯoɹɟ ǝq oʇ ƃuᴉoƃ ʇou sᴉ pɹɐoqʎǝʞ ʇxǝu ʎɯ 'ɹɐǝp ɥO
User avatar
polka
Over Heated PSU
Posts: 144
Joined: Mon Mar 07, 2011 11:43 am

Re: String + Numbers .... help!

Postby polka » Thu Apr 23, 2020 7:15 am

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


May the FORTH be with you !
POLKa
User avatar
Giorgio Garabello
Gold Card
Posts: 257
Joined: Tue Jun 30, 2015 8:39 am
Location: Turin, Italy
Contact:

Re: String + Numbers .... help!

Postby Giorgio Garabello » Thu Apr 23, 2020 8:04 am

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



Who is online

Users browsing this forum: No registered users and 14 guests