Changes

Technical information about Locomotive BASIC

2,639 bytes added, 13:46, 16 April 2012
/* Floating Point data definition */
To obtain the signed exponent, you must subtract 128 from the stored exponent. The minimum exponent is 0 and this describes a number of 2^-127. The maximum exponent is 255 and this describes a number of 2^128.
 
=== floating point example in RAM by MaV ===
 
The initial figure = 43.375.
An example in BASIC:
<pre>
a!=43.375
READY
PRINT a!
43.375
PRINT @a!
374
FOR I=0 TO 4:PRINT HEX$(PEEK(374+I),2);:NEXT I
0000802D86
</pre>
the result for better readability:<br>
'''00 00 80 2D 86'''<br>
The first byte contains the least significant bits of the mantissa, and every following byte up to the fourth byte contain the bits following the first in significance and in exactly that order!
 
That means that you have to turn around the first four bytes if you want to read the mantissa correctly:
2D800000
or
0010.1101.1000.0000.0000.0000.0000.0000
in binary (the stops are inserted for better readability).
 
Now the highest significant bit here is 0, so the number is positive.
What's more, you need to clear that bit and substitute the implied highest bit (see description in the link), which is always 1 (regardless of the deleted sign bit!!!).
Thus you get:
AD800000
or
1010.1101.1000.0000.0000.0000.0000.0000
in binary.
 
The mantissa has an implied decimal point before the number. Since the number is 2^32 bits (i.e. 4 bytes) long you have to divide by 2^32 to get the proper decimal representation.
 
AD800000 (hex) = 2910846976 (dec)
 
2910846976 / 2^32 = 0.677734375
 
The mantissa in a decimal representation is: 0.677734375
 
Now, the last byte contains the exponent: 86 (hex) or 134 (dec). You need to subtract 128 to get the real exponent, the highest bit tells you that the exponent is positive (1) or negative (0). Your exponent is 6.
 
0.677734375 * 2^6 = 43.375
 
'''Another example:'''<br>
PI is represented in memory as:<br>
'''A2 DA 0F 49 82'''<br>
The mantissa is<br>
490FDAA2 (highest significant bit is zero => number is positive)
The exponent is<br>
82 (=positive mantissa, value=2 thus multiply mantissa with 2^2)<br>
 
Delete the sign bit (which is 0 anyway), and then add the implied 1 bit:<br>
C90FDAA2 (hex) = 3373259426 (dec) = 1100.1001.0000.1111.1101.1010.1010.0010 (bin)<br><br>
 
Calculate the decimal representation of the mantissa:<br>
3373259426 / 2^32 = 0,7853981633670628070831298828125<br><br>
 
Then add the exponent (82h - 80h = 2 thus 2^2)<br>
0,7853981633670628070831298828125 * 2^2 = 3,14159265346825122833251953125<br><br>
 
Voilà! The internal representation of the number PI on the CPC is: 3,14159265346825122833251953125<br><br>
 
 
If you compare to a (more) correct representation of PI, you'll see that the CPC is correct up to the 9th decimal place:<br><br>
 
 
CPC:<br>
3.14159265346825122833251953125<br>
more correctly:<br>
3.1415926535897932384626433832795028841971693993751058209749445923078164062862089986...
== BASIC floating-point/real variables ==
205
edits