Menu

Looking at FP variables in memory

2019-04-30
2019-05-14
  • Alberto Donda

    Alberto Donda - 2019-04-30

    Hello again,
    I'm keeping to use the PCBasic with my pupils at school. Now we have studied the Floating Point standard (single precision-4 bytes) on the book, and I would be interesting to verify it on PC basic. I managed to get the location where is stored a variable but I'm not sure to understand. For sure the 4 byte i'm reading are changing if I change the variable, but this internal representation seems not clear to me.
    What standard it follows? Is the same of original GWbasic (emulated) ...or not?

     
  • Alberto Donda

    Alberto Donda - 2019-05-06

    Hello, I made a small demo capable to show a fp variable in binary...

    5 cls
    10 V=10:P=VARPTR(V)
    20 DEFSEG=&H13AD:Y=15 
    30 LOCATE 20,1:INPUT V 
    40 FOR XX=0 TO 3 
    50 X=55-(XX*18):N=PEEK(P+XX) 
    60 LOCATE Y-1,X+7: IF XX=3 THEN PRINT"e" ELSE PRINT"m" 
    65 IF XX=2 THEN LOCATE Y-1,X+1: PRINT "s" 
    70 GOSUB 500 
    80 NEXT XX 
    90 GOTO 30 
    500 LOCATE Y,X:PRINT CHR$(218);CHR$(196); 
    510 FOR I=1 TO 7: PRINT CHR$(194);CHR$(196); :NEXT I: PRINT CHR$(191) 
    520 LOCATE Y+1,X: FOR I=1 TO 9: PRINT CHR$(179);" "; : NEXT I 
    530 LOCATE Y+2,X: PRINT CHR$(192);CHR$(196); 
    540 FOR I=1 TO 7: PRINT CHR$(193);CHR$(196);:NEXT I: PRINT CHR$(217) 
    550 REM *** parte che converte "n" in binario e lo stampa *** 
    560 FOR I = 0 TO 7 
    570 LOCATE Y+1,X+(7-I)*2+1:IF (N MOD 2)=0 THEN PRINT "0" ELSE PRINT "1" 
    580 N=INT(N/2): NEXT I 
    999 RETURN 
    
     
    👍
    1
  • Rob Hagemans

    Rob Hagemans - 2019-05-07

    Hi Alberto,

    The floating-point format in PC-BASIC is the same format (MBF) as in GW-BASIC, but this is not the modern standard (IEEE754).

    MBF is documented here http://robhagemans.github.io/pcbasic/doc/1.2/#mbf; you can find a more extensive description, background and history in the Wikipedia page which Marc links to. Note that GW-BASIC and PC-BASIC only have single- and double-precision floats and not extended-precision floats also described there.

    You can also use MKS$ and MKD$ to convert from floats to character strings in MBF format and CVS and CVD for the reverse - see documentation for details.

    a$=mks$(1.5e4)
    for i = 1 to len(a$):print hex$(asc(mid$(a$,i,1))),:next
    0             60            6A            8E
    

    If you're teaching floating-point, it seems to me that would make sense to focus on IEEE 754 instead - except if this is a computer history course :) PC-BASIC or GW-BASIC won't be able to help with that as they don't support IEEE 754, but every modern language does. For example, in Python 3:

    >>> import struct
    >>> [hex(c) for c in struct.pack('f', 1.5e4)]
    ['0x0', '0x60', '0x6a', '0x46']
    

    Rob

     
    • Marc 'BlackJack' Rintsch

      If you don't need infinity and NaN as possible values, using MBF for assignments might have the advantage that the students have it much harder to find solutions on the internet and are forced to actually do the assignment themselves. ;-)

       
  • Alberto Donda

    Alberto Donda - 2019-05-08

    Exact! Thank you to both!
    Yes I'm teaching F.P. at school, this days. IEEE754 is on the book and that's what we are studing, but i dont see any problem in observing some practical (storical?) application. Used bits are exactly the same, just in different position. Both formats use 8 bit for exponent, 1 for sign and 23 for the mantissa. The IEEE754 uses a 127 bias for the exponent while "basic" uses 128 (if i'm not wrong) The "bill gates" solution seems more smart because it alligns the exponent to first byte while IEEE754 spread it on first and second byte. In my mind they also place the sign at the start of the mantissa just because, then, you just need to overwrite the implicit 1 and the register is ready to operate.

    I see on wikipedia there not support for infinite (as you can see in my demo, the variable will be set all to "1"'s, with sign, automatically becoming the biggest -or lowest- number possible). Anyway we are inside a programmig language, so overflow is an error, not a simbolic "+infinite" to carry in the next calculation!

     

    Last edit: Alberto Donda 2019-05-08
    • Marc 'BlackJack' Rintsch

      I guess when using dedicated floating point hardware the byte boundaries don't matter that much and placing the sign bit as MSB allows for this design gloal of IEEE754: “The single and double precision formats were designed to be easy to sort without using floating-point hardware. Their bits as a two's-complement integer already sort the positives correctly, and the negatives reversed. If that integer is negative, xor with its maximum positive, and the floats are sorted as integers.”