Hi all,
I was sort of surprised by the behavior of the str$(x#,w) function when x# was a negative value. -3 can be displayed as -2!
This experiment was only run in the simulator in i-TRiLOGI V 7.03 build 6. I did not run this code on actual hardware, sorry.
This is my test code:
' Display Float
'
i = -3
while (i < 4)
i# = i
print #2 "Value: ";i; ' print integer value
print #2 " "; str$(i#); ' float with default formatting
print #2 " "; str$(i#,-3); ' float with 3 decimal points
print #2 " "; str$(i#,0) ' float rounded to nearest integer
i = i + 1
endwhile
[/color]
This is what I saw:
Value: -3 -3.0000000 -2.999 -2
Value: -2 -2.0000000 -1.999 -1
Value: -1 -1.0000000 -0.999 0
Value: 0 +0.0 +0.000 0
Value: 1 +1.0000000 +1.000 1
Value: 2 +2.0000000 +2.000 2
Value: 3 +3.0000000 +3.000 3
[/color]
I checked the bit pattern of the floating point values and saw that they represented exactly -3, -2 and -1, but if I used the str$(x#, w) statement, the strings did not represent whole integer values.
The str$(x#,0) format, which is documented as "Rounded to the nearest integer" thinks that the closest integer to -3 is -2??? I sort of think that the nearest integer to -3 is actually -3.
Am I expecting too much?
Also another fun fact about TBASIC. The following code will not work as expected:
for i = -3 to 3
i# = i
print #2 "Value: ";i ' print integer value
next
[/color]
Oh, I expected that the code between the for and next keywords would be executed for values of "I" from -3 to 3...
Interestingly, "I" is initialized to &H0000FFFD which is not -3 (for a 32-bit signed integer). This results in the initial value of "I" being greater than the limit value of 3, so the code between for and next keywords is not evaluated. If "I" had been a 16-bit signed value then %HFFFD is -3. I am guessing that for/next can only work with unsigned values between 0 and 32767. I'm guessing that this is real old code "feature".
Best regards,
Gary D