Code: Select all
dim as integer x = 5
print cshort(3.1 * x - 79)
print cshort(3.1 * 5 - 79)
-63
-64
Code: Select all
dim as integer x = 5
print cshort(3.1 * x - 79)
print cshort(3.1 * 5 - 79)
-63
-64
Code: Select all
'' run-time - 80 bits of precision on FPU stack
dim as integer x = 5
print cshort(3.1 * x - 79) '' -63
'' compile-time constant folding - 64 bits of precision using DOUBLE type
print cshort(3.1 * 5 - 79) '' -64
'' run-time, but emulate what compiler does with constant folding
dim as integer y = 5
dim as double t = 3.1 * y
print cshort(t - 79) ''-64
In case you need a workaround you maybe could use something like this, dear Schooner. It first multiplies with 10 and then divides with 10:schooner wrote:Why do these produce different numbers for the 32-bit gas? Shouldn't the rounding be the same for all variations?Code: Select all
dim as integer x = 5 print cshort(3.1 * x - 79) print cshort(3.1 * 5 - 79) -63 -64
Code: Select all
dim as integer x = 5
print cshort(3.1 * x - 79)
print cshort(3.1 * 5 - 79)
sleep
print
print cshort((31 * x)/10 - 79)
print cshort((31 * 5)/10 - 79)
sleep
-63
-64
-64
-64
Thank you coderJeff.coderJeff wrote:Different things are happening at run time and compile time:
The first part of the calculation 3.1 * 5 is VERY close to 15.5 but slightly higher in 80 bit precision compared to 64 bit.Code: Select all
'' run-time - 80 bits of precision on FPU stack dim as integer x = 5 print cshort(3.1 * x - 79) '' -63 '' compile-time constant folding - 64 bits of precision using DOUBLE type print cshort(3.1 * 5 - 79) '' -64 '' run-time, but emulate what compiler does with constant folding dim as integer y = 5 dim as double t = 3.1 * y print cshort(t - 79) ''-64
Thank you lizard. This also works:lizard wrote:In case you need a workaround you maybe could use something like this, dear Schooner. It first multiplies with 10 and then divides with 10:
Although i am not sure if this is what you need.Code: Select all
dim as integer x = 5 print cshort(3.1 * x - 79) print cshort(3.1 * 5 - 79) sleep print print cshort((31 * x)/10 - 79) print cshort((31 * 5)/10 - 79) sleep -63 -64 -64 -64
Code: Select all
dim as integer x = 5
print cshort(( (3.1 * 10) * x)/10 - 79)
print cshort(( (3.1 * 10) * 5)/10 - 79)
Code: Select all
dim as integer x = 5
? cast( short, csng( 3.1 * x - 79 ) )
? cast( short, csng( 3.1 * 5 - 79 ) )
sleep()
Output:
-64
-64
This needs not be. You can use GCC to spit a 32-bit executable:schooner wrote:The 64-bit gcc always produces -64 so I suppose that is the correct answer. This becomes a nightmare to debug when trying to compare cross-compiles between 32 and 64 bit assemblies.
Does not work on my configuration.paul doe wrote:Simply convert the expression to single, and then cast it to short:Code: Select all
dim as integer x = 5 ? cast( short, csng( 3.1 * x - 79 ) ) ? cast( short, csng( 3.1 * 5 - 79 ) ) sleep() Output: -64 -64
Which one is that? I get those results in 32-bit GAS, 32-bit GCC and 64-bit GCC. Tested on FreeBasic compiler version 1.05.0.schooner wrote:Does not work on my configuration.
Hello paul doe.paul doe wrote:Which one is that? I get those results in 32-bit GAS, 32-bit GCC and 64-bit GCC. Tested on FreeBasic compiler version 1.05.0.schooner wrote:Does not work on my configuration.
I thought that the topic was about you getting different rounding results in 32-bit GAS vs 64-bit GCC?schooner wrote: Hello paul doe.
It does not matter for this topic. There is a work-around from lizard. There is no need to change the FBC configuration unless something else goes wrong. However, it is surprising your results do not duplicate the others.
schooner wrote:Why do these produce different numbers for the 32-bit gas? Shouldn't the rounding be the same for all variations?
But whatever, suit yourself.schooner wrote:The 64-bit gcc always produces -64 so I suppose that is the correct answer. This becomes a nightmare to debug when trying to compare cross-compiles between 32 and 64 bit assemblies.
Another story i once heard: There was a programmer of a bank who had similar problems. Sometimes there was a cent to much in transactions. He found a simple solution. Whenever this happened he transfered this cent to his own account. It became a pretty high sum after some time. Until they found out and then he had opportunity in prison to think about what to program next ...lizard wrote:Never trust a computer when it comes to money and floatingpoint calculations. :-)