Integer treated as double precision in division

New to FreeBASIC? Post your questions here.
Aetzbarr
Posts: 4
Joined: Oct 14, 2023 4:48

Integer treated as double precision in division

Consider the following little code:

Code: Select all

``````? 1.1+7, 1.1+7!, 1.1+7#
? 1.1*7, 1.1*7!, 1.1*7#
? 1.1/7, 1.1/7!, 1.1/7#
``````
When compiled (-lang qb) and run, the output is:
8.1 8.1 8.100000023841858
7.7 7.7 7.700000166893005
0.1571428605488368 0.1571429 0.1571428605488368

So in addition and multiplication, "7" is treated as "7!". But in division, "7" is treated as "7#". Why is that? It seems to me that "7" should be treated as "7!" in division also. That's how it works in QuickBASIC/QBasic. Don't assume the denominator is known to double precision...
fxm
Moderator
Posts: 12197
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: Integer treated as double precision in division

In '-lang fb', I get:
8.1 8.1 8.1
7.700000000000001 7.700000000000001 7.700000000000001
0.1571428571428572 0.1571428571428572 0.1571428571428572
Aetzbarr
Posts: 4
Joined: Oct 14, 2023 4:48

Re: Integer treated as double precision in division

I think the behaviour of -lang qb is incorrect. Since the "7" in "1.1/7" fits in a 16-bit integer, the calculation should be done in single precision. 1.1/7# or 1.1/77777 are double precision, but not 1.1/7. Anyway that's how it works in QB. And if the 7 is considered as having infinite precision (since it is an integer - EXACTLY 7), Then why are 1.1+7 and 1.1*7 not calculated in double precision, like 1.1/7?
exagonx
Posts: 316
Joined: Mar 20, 2009 17:03
Location: Italy
Contact:

Re: Integer treated as double precision in division

Aetzbarr wrote: Mar 30, 2024 19:24 Consider the following little code:

Code: Select all

``````? 1.1+7, 1.1+7!, 1.1+7#
? 1.1*7, 1.1*7!, 1.1*7#
? 1.1/7, 1.1/7!, 1.1/7#
``````
Athlon 32 and Pentium 2
8.1 8.1 8.100000023841858
7.7 7.7 7.700000166893005
0.1571428605488368 0.1571429 0.1571428605488368

Rayzen CPU and Intel i9
8.1 8.1 8.1
7.700000000000001 7.700000000000001 7.700000000000001
0.1571428571428572 0.1571428571428572 0.1571428571428572

I ran the same code on 4 PCs with different CPUs:
Intel Core i9
Rayzen 7
Pentium 2
AMD Athlon XP 3200

And it seems to give different results based on the CPU used, I believe -QB mode directly uses the processor registers to perform calculations and based on the size of the register it gives different results as recent processors have more complex registers.

As soon as I have more time I will try to confirm or deny this theory.