Hi all,
I recently came upon a nice Sbasic rounding/ conversion problem, when a
program that worked OK on other machines didn't work correctly on
SMSQmulator.
After much headscratching, I was able to pinpoint the problem, which was
a general SBasic problem, not related to SMSQmulaor after all (phew)...
Basically, what a user did was get a number (say, a), a normal Sbasic
floating point number (but the number was an integer).
Convert that to a string and later back to a number (say, b). Both
numbers (a and b) no longer were equal.
Something like;
100 CLS
110 a=$5E84320
120 PRINT "a = ";a,HEX$(a,32)
130 a$=a
140 PRINT "a$= ";a$
150 b=a$
160 PRINT "b = ";b,HEX$(b,32)
In line 110, a is given a big integer value. In line 130, a is converted
to a string and in line 150 b is converted into a float from that string.
From the hex values printed, it is easy to see that both numbers, a and
b, are different.
The problem stems from the conversion float -> string
Marcel Kilgus proposed the following solution, to convert an SBasic
floating point INTEGER to a string.
DEFine FuNction FP2DEC$(val)
RETurn FDEC$(val, INT(LN(val) / LN(10)) + 1, 0)
END DEFine
You can check this, as follows :
100 CLS
110 a=$5E84320
120 PRINT "a = ";a,HEX$(a,32)
130 a$=a
140 PRINT "a$= ";a$
150 b=a$
160 PRINT "b = ";b,HEX$(b,32)
170 c$=FP2DEC$(a)
175 PRINT "c$ =";c$
180 d=c$
190 PRINT "d = ";d,HEX$(d,32)
200 :
250 DEFine FuNction FP2DEC$(val)
260 RETurn FDEC$(val, INT(LN(val) / LN(10)) + 1, 0)
270 END DEFine
a and d are equal...
Wolfgang
_______________________________________________
QL-Users Mailing List
http://www.q-v-d.demon.co.uk/smsqe.htm