1

I am looking to do floating point number file input/output in Gforth. I found this question here which converts them to strings GForth: Convert floating point number to String , but I can't see how to do it. Any suggestions on how I can save floats to a file and then load them back on to the floating point stack would be much appreciated. Thank you.

EDIT:

I can now do it by taking the address where the float is saved and then saving the number as 8 characters (1 cell is 8 characters) to a file. The issue is this is completely non-human readable and is also not easy to then load into other languages. The idea is I want to be able to load in the floats in say a python script for graph plotting! Thanks!

3
  • 1
    Ok I have worked out that you can save the r number to a variable and then load it using @ (not F@) to get a number on the n stack. I've checked and you don't seem to loose anything doing this, now I just need to work out how to save numbers, which for some reason doesn't appear to be trivial. Commented Apr 17 at 8:07
  • "How I can save floats to a file and then load them back on to the floating point stack would be much appreciated.", maybe like in write anything to files from vars with gforth or File Tutorial and then enter numbers in Forth. Commented Apr 17 at 8:52
  • 1
    Hey, thank you for the suggestion. So I can save the floats to a file in a character array and can even load them back in now, but only in forth as the output is completely non human readable and say if I have a whole load of them I want to for example throw through a python script, it becomes very difficult to do so. Commented Apr 18 at 1:32

1 Answer 1

1

Ok here is how I solved this, I can now easily save my matrices in gforth and then load them back in gforth or even in python in case I wanted to do some stuff like easy graph plotting. I will show both

to begin my matrices are a variable that stores an address on heap so @ will get the address and a second @ get the value. They have a header that is #dimensions,dimsize1,dimsize2,...,dimsizen.

: matrixsave ( var, str -- )
w/o create-file throw dup >r swap dup matrixcellnumber swap dup @ @ rot + 1+ chars/cell * swap @ swap rot write-file r> close-file drop drop
; 

This will create a file with the name given by string. Matrixcellnumber is a word I wrote that returns the number of cells excluding the header (which is why we then need the number of dimensions+1 to get the total number of cells). chars/cell is a constant that gives the number of characters per cell. we then write the number of characters that correponds to the size of the matrix to a file. Note this will not be human readable at all!

Opening this file in forth is fairly similar just read in all the chars and save to an allocated memory space.

Using it in python

To use it in python I wrote the following script

char_array = []
with open("matrix.txt","rb") as file:
    while 1:
        my_char = file.read(8)

        if not my_char:
            break
        char_array.append(my_char)
int_array = np.zeros(len(char_array))
int_array[0] = int.from_bytes(char_array[0],byteorder="little")
print(int_array.shape)
start = 0
dt = np.dtype(float)
dt = dt.newbyteorder('<')
for i in range(0,int(int_array[0]+1)):
    int_array[i] = int.from_bytes(char_array[i],byteorder="little")
    start += 1
for i in range(start,len(char_array)):
    int_array[i] = np.frombuffer(char_array[i],dtype=dt)[0]
sizes = tuple(int_array[1:(int(int_array[0])+1)].astype(int))
float_array = np.reshape(int_array[int(int_array[0]+1):],sizes)
print(float_array)

ok so the name of int array is misleading only the header is ints, the rest of the data is assumed to be floats. So we start by loading in the bytes to "char array", then we read the first integer +1 to which gives us the size of the header. Next we read in the rest of the header. Moving after the start of the header we then read everything else in as floats. Next we want to reshape it to be the dimensions according to the header, work out those sizes, throw away the header and reshape it.

This was all quite a pain to work out so I hope this will be useful for anyone else trying something like this! :)

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.