CCurl / c4-old

c4: A minimal, stack-based VM/OS for PCs and development boards

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

floating point calculations?

wboeke opened this issue · comments

Hello,
Maybe I'm stupid, but I cannot get floating point calculations working.
This works: 2.2 f. -> 2.20000
This not: 2.2 3.3 f+ f. -> 3.30000
I tested this with a vanilla c4, only in the makefile I omitted the -m32 option (not needed for linux)
---- Edit:
I tried the program on an old 32bit computer, and there everything works. So yes, I was stupid. Stil I am not going to cripple down my 64bit machine but will try to find another solution.

Linux I assume? f+ doesn't work when it is a 64 bit build? Weird, I'll look into that

I bet it is because floats are 32 bits and longs on Linux are 64 bits, and c4 uses the same stack for both. So it works when both are the same size,

Actually, it's not the same stack. In c4.h:
typedef union { float f[STK_SZ + 1]; CELL i[STK_SZ + 1]; } STK_T;
Here, float is 32bit and CELL is 64bit, so you have to always use the right union component. I didn't trust that, so I modified the code using one stack, with a union as stack component. However: this didn't change anything, the errors stayed the same!
Then, totally by accident, I found out that if I hit return between typing the float numbers, everything worked as expected! The cause if this I coudn't find yet however.

Adendum:
On a 32bit machine everything works as expected: strings, floats, "(here)" etc. On a 64bit laptop sometimes this works but mostly not. So in my opinion there is an alignment problem. Maybe you are on Windows only, where things may be different from Linux? In your program you go to great length in order to use as little memory as possible and then alignment can go wrong?

There are 2 problems, there was a place where it was hard-coded to step by 4 even on 64-bit systems, where is should step by 8. The other problem is that floats are 32-bit, and the ST_T union of the 2 different sizes are messing up the slots for the entries, because of the different sizes. The fix is to use a double for 64-bit systems and a float for 32-bit systems.

I have a fix and will upload it shortly.

Please have a look at this branch.

https://github.com/CCurl/c4/tree/float-fix

Thanks

FWIW, the LSP my neovim uses reformatted the files, so it added like 500 lines to the source. I doubt I will complete this PR, because there were really only less than 5 changes total.

I will do another PR with the formatting turned off.

Don't bother about formatting: floats are working. Next problem: strings don't. An example:

variable v1
v1 80 allot
" Harry " v1 str-cpy
v1 ztype cr
" meets Sally" v1 str-cat
v1 ztype cr

On 32bit: no problem, 64bit: wrong or no output.

I think it's the same problem, where it is doing "pc+=4" instead of "pc+=CELL_SZ". That is also fixed by the changes in the "float-fix" branch, which should hopefully fix the str-cat problem.

This is/was wrong on 64-bit systems:
image

Float's and strings are okay now, only 2 lines had to be modified.
As said before, I use one kind of stack, so floats didn't have to be changed to doubles.

Float's and strings are okay now, only 2 lines had to be modified. As said before, I use one kind of stack, so floats didn't have to be changed to doubles.

Which 2 lines? The 2 that were incorrectly hard-coded to "pc += 4"?

Yes.
That was simple, I had been searching for a solution myself, but in vain.

Excellent! Thanks for trying out my stuff. :)