04-20-2022, 02:20 AM
A quick example of how to change floating point precision in QB64. This swaps between quick math (which uses hardware math processors) to extended precision math (which uses software processing). Note that the default qbfpu is quite a bit faster, and for most folks this should be more than sufficient for your needs, as it tracks precision down to about the 15th decimal point. IF, however, you absolutely have to have greater levels of precision, you can now swap over to extended precision and have about 20 decimal points worth of precision, at a significant reduction of speed.
And, if you need more than 20 decimal points of precision, you're just shit out of luck. Find a math library for that, or else write a string math handling routine -- your CPU isn't equipped to handle anything more than this, natively.
FPU_Precision.h
QB64 Code:
And, if you need more than 20 decimal points of precision, you're just shit out of luck. Find a math library for that, or else write a string math handling routine -- your CPU isn't equipped to handle anything more than this, natively.
FPU_Precision.h
Code: (Select All)
void set_dpfpu() { unsigned int mode = 0x37F; asm ("fldcw %0" : : "m" (*&mode));}
void set_qbfpu() { unsigned int mode = 0x27F; asm ("fldcw %0" : : "m" (*&mode));}
QB64 Code:
Code: (Select All)
' FPU_Precision.h needs to be in QB64 folder
$CONSOLE:ONLY
_DEST _CONSOLE
DECLARE CUSTOMTYPE LIBRARY ".\FPU_Precision"
SUB set_dpfpu 'to toggle to double precision floating point math
SUB set_qbfpu 'to toggle back to what most folks will see with QB64 64-bit default math
END DECLARE
DIM x AS _FLOAT, y AS _FLOAT
'Let's print our results without screwing with anything first.
x = 5##
y = x / 9##
PRINT USING "QB64 division #.####################"; y
'Set the double precision math
set_dpfpu
x = 5##
y = x / 9##
PRINT USING "QB64 division #.####################"; y
'Set the QB64 precision math
set_qbfpu
x = 5##
y = x / 9##
PRINT USING "QB64 division #.####################"; y