Author Topic: Possible error in INTEGER64 calculations  (Read 8874 times)

0 Members and 1 Guest are viewing this topic.

Offline david_uwi

  • Newbie
  • Posts: 71
    • View Profile
Possible error in INTEGER64 calculations
« on: September 25, 2021, 04:19:42 am »
Sorry I've not got the latest version, so if this has been sorted you can delete this.

DIM z AS _INTEGER64
z = 7 ^ 19
PRINT z

The answer should end in ..373143, but the above gives ...373144


Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Possible error in INTEGER64 calculations
« Reply #1 on: September 25, 2021, 12:54:33 pm »
^ power is imprecise:
Code: QB64: [Select]
  1. z = 7 ^ 19
  2. z = 1
  3. For i = 1 To 19
  4.     z = z * 7
  5.  
  6.  

Offline SMcNeill

  • QB64 Developer
  • Forum Resident
  • Posts: 3972
    • View Profile
    • Steve’s QB64 Archive Forum
Re: Possible error in INTEGER64 calculations
« Reply #2 on: September 25, 2021, 01:07:03 pm »
This may be a case of intermediary math issues, where the program is storing theresults as the wrong type as it calculates the answer.

To explain what I’m speaking of, think of how that end result is PROCESSED.

z = 7 ^ 19

Somewhere in our math processors, the computer will take those two numbers (7 and 19) and then process them according to the operator between them…

z is an INTEGER64, as you’ve defined it, but what are 7 and 19??  I’m thinking they’re probably SINGLE values, since that’s what QB64 types are by default.  If so, what do you think the internal registers use to store the result of a SINGLE raised to a SINGLE? 

I’d imagine that intermediate step would store the math result in a SINGLE, and your end result is so large you lose perfect precision.

I’m not at a PC right now (and my ipad can’t compile QB64 programs), but see what the results look like if you specify 7 and 19 as INT64 values:

z = 7&& ^ 19&&

It may end up making a world of difference!
https://github.com/SteveMcNeill/Steve64 — A github collection of all things Steve!

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Possible error in INTEGER64 calculations
« Reply #3 on: September 25, 2021, 01:17:54 pm »
Nope it's ^ it uses formulas LOG or EXP with lots of digits to do ^ calcs, multiplying with * is more precise.
 

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Possible error in INTEGER64 calculations
« Reply #4 on: September 25, 2021, 01:26:56 pm »
Nope it's ^ it uses formulas LOG or EXP with lots of digits to do ^ calcs, multiplying with * is more precise.

The beauty of ^ is that you are not limited to integers.

Offline david_uwi

  • Newbie
  • Posts: 71
    • View Profile
Re: Possible error in INTEGER64 calculations
« Reply #5 on: September 25, 2021, 01:58:56 pm »
Interesting. I will bear that in mind when using ^.
I note that 7^21 is even further out
..284007 is correct putting in 7^21 the number ends in ends in ..284032 (25 too big).
"casting" (as I believe it is called) 7&&^21&& gives the same value as 7^21.

Offline random1

  • Newbie
  • Posts: 86
    • View Profile
Re: Possible error in INTEGER64 calculations
« Reply #6 on: September 28, 2021, 11:31:26 pm »
I've ran into similar problems in dealing with very large numbers
but was never able to find a work-around.  Hope someone here
can help with a solution.
R1     

Offline Pete

  • Forum Resident
  • Posts: 2361
  • Cuz I sez so, varmint!
    • View Profile
Re: Possible error in INTEGER64 calculations
« Reply #7 on: September 28, 2021, 11:50:22 pm »
Another argument for String Math! I haven't done powers with it, but it would be as simple as a multiplication loop. I'm not sure if poers has any tricks, like finding square roots. Anyway, anything _INTEGER64 can do String Math can do better.

Pete
Want to learn how to write code on cave walls? https://www.tapatalk.com/groups/qbasic/qbasic-f1/

Offline david_uwi

  • Newbie
  • Posts: 71
    • View Profile
Re: Possible error in INTEGER64 calculations
« Reply #8 on: September 29, 2021, 03:10:23 am »
It certainly could catch out the unaware.
That said it is integer arithmetic - both arguments 7 and 21 are integer so the answer is integer.
As long as there are no overflows it is reasonable to assume that the integer answer should be the correct answer, but it is not.

The compiler should not default to floats when doing integer calculations (but what do I know I've never tried to write a compiler so I have no clue as to the difficulties)

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Possible error in INTEGER64 calculations
« Reply #9 on: September 29, 2021, 01:44:57 pm »
The way I think of ^ is like /, you use it then you have lost pure integer returns from the operator.

^ still very good up to 14 digits.

@david_uwi  you could setup your own Power&& function to be assured integers (or error flag).

Offline jack

  • Seasoned Forum Regular
  • Posts: 408
    • View Profile
Re: Possible error in INTEGER64 calculations
« Reply #10 on: September 29, 2021, 02:11:36 pm »
something like this
Code: QB64: [Select]
  1.     'take x to an integer power
  2.     Dim As _Integer64 z, y
  3.     y = x
  4.     n = e
  5.     z = 1&&
  6.     While n > 0
  7.         While (n Mod 2) = 0
  8.             n = n \ 2
  9.             y = y * y
  10.         Wend
  11.         n = n - 1
  12.         z = y * z
  13.     Wend
  14.     ipower = z
  15.  

Offline jack

  • Seasoned Forum Regular
  • Posts: 408
    • View Profile
Re: Possible error in INTEGER64 calculations
« Reply #11 on: September 29, 2021, 02:16:17 pm »
you could also substitute WHILE (n MOD 2) = 0 with While (n And 1) = 0 which might be faster

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Possible error in INTEGER64 calculations
« Reply #12 on: September 29, 2021, 02:19:37 pm »
I wonder why the middle stuff?
Code: QB64: [Select]
  1. Print ipower&&(7, 21)
  2.  
  3.     'take x to an integer power
  4.     Dim As _Integer64 z, y
  5.     y = x
  6.     n = e
  7.     z = 1&&
  8.     While n > 0
  9.         'While (n Mod 2) = 0  ' why have this?
  10.         '    n = n \ 2
  11.         '    y = y * y
  12.         'Wend
  13.         n = n - 1
  14.         z = y * z
  15.     Wend
  16.     ipower = z
  17.  
  18.  
  19.  

Seems to work for the one sample I tested. ;-))

Good to make copies of arguments and use a While loop as opposed to For.

Offline SMcNeill

  • QB64 Developer
  • Forum Resident
  • Posts: 3972
    • View Profile
    • Steve’s QB64 Archive Forum
Re: Possible error in INTEGER64 calculations
« Reply #13 on: September 29, 2021, 02:25:27 pm »
*Bplus — half the loops.

3 ^ 12 = 3*3*3*3*3*3*3*3*3*3*3
9 ^ 6 = (3*3)*(3*3)*(3*3)*(3*3)*(3*3)*(3*3)
81 ^ 3 = you get the point.  :P
https://github.com/SteveMcNeill/Steve64 — A github collection of all things Steve!

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Possible error in INTEGER64 calculations
« Reply #14 on: September 29, 2021, 02:30:56 pm »
Oh so if e where a power of 2, then only one outer loop, clever!

Code: QB64: [Select]
  1. Print ipower&&(7, 16)
  2.  
  3.     'take x to an integer power
  4.     Dim As _Integer64 z, y
  5.     y = x
  6.     n = e
  7.     z = 1&&
  8.     While n > 0
  9.         outer = outer + 1
  10.         While (n Mod 2) = 0 ' why have this?
  11.             inner = inner + 1
  12.             n = n \ 2
  13.             y = y * y
  14.         Wend
  15.         n = n - 1
  16.         z = y * z
  17.     Wend
  18.     ipower = z
  19.     Print outer, inner
  20.