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

0 Members and 1 Guest are viewing this topic.

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Possible error in INTEGER64 calculations
« Reply #15 on: September 29, 2021, 02:37:12 pm »
Need error flag for this:
Code: QB64: [Select]
  1. Print ipower&&(7, 32)
  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
« Last Edit: September 29, 2021, 04:15:17 pm by bplus »

Offline jack

  • Seasoned Forum Regular
  • Posts: 408
    • View Profile
Re: Possible error in INTEGER64 calculations
« Reply #16 on: September 29, 2021, 03:46:16 pm »
        While (n Mod 2) = 0 ' why have this?
while n is even
    divide n by 2 and square the result
wend

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Possible error in INTEGER64 calculations
« Reply #17 on: September 29, 2021, 04:18:22 pm »
Thanks @jack

Steve got here first to explain, clever idea!

We need error checking for when numbers get too big.

Offline jack

  • Seasoned Forum Regular
  • Posts: 408
    • View Profile
Re: Possible error in INTEGER64 calculations
« Reply #18 on: September 30, 2021, 08:27:01 am »
bplus, can you think of an easy and efficient way to detect or prevent overflow?
there might be some ideas from this post https://stackoverflow.com/questions/3944505/detecting-signed-overflow-in-c-c

Offline SMcNeill

  • QB64 Developer
  • Forum Resident
  • Posts: 3972
    • View Profile
    • Steve’s QB64 Archive Forum
Re: Possible error in INTEGER64 calculations
« Reply #19 on: September 30, 2021, 09:18:11 am »
bplus, can you think of an easy and efficient way to detect or prevent overflow?
there might be some ideas from this post https://stackoverflow.com/questions/3944505/detecting-signed-overflow-in-c-c

Precalculate values with _FLOATs.  If the result is greater than what an INT64 can hold, flag an error and reject them.
https://github.com/SteveMcNeill/Steve64 — A github collection of all things Steve!

Offline jack

  • Seasoned Forum Regular
  • Posts: 408
    • View Profile
Re: Possible error in INTEGER64 calculations
« Reply #20 on: September 30, 2021, 10:07:56 am »
never mind, it didn't work
« Last Edit: September 30, 2021, 10:14:44 am by jack »

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Possible error in INTEGER64 calculations
« Reply #21 on: September 30, 2021, 10:09:47 am »
Precalculate values with _FLOATs.  If the result is greater than what an INT64 can hold, flag an error and reject them.

I had similar thought getting out of bed this morning. The b ^ e = Exp(e * Log(b)) may fail in precision before the limit of _Integer64 though. Will be interesting challenge to test.

(Jack just posted as I was editing this. So I have yet to study your reply jack.)

wait... what am I thinking?  the precision for the calc using * will be fine as long as we don't go over.

Offline jack

  • Seasoned Forum Regular
  • Posts: 408
    • View Profile
Re: Possible error in INTEGER64 calculations
« Reply #22 on: September 30, 2021, 10:17:55 am »
bplus, when I tested with x=2 it seemed to work but it doesn't work for other numbers

Offline jack

  • Seasoned Forum Regular
  • Posts: 408
    • View Profile
Re: Possible error in INTEGER64 calculations
« Reply #23 on: September 30, 2021, 11:41:45 am »
bplus, this should work, it's up to the user to decide what to do when overflow occured
reference https://gcc.gnu.org/onlinedocs/gcc/Integer-Overflow-Builtins.html
Code: QB64: [Select]
  1.     Function mul_overflow& Alias "__builtin_smulll_overflow" (ByVal a As _Integer64, Byval b As _Integer64, res As _Integer64)
  2.  
  3. 'Print ipower(2&&, 3)
  4. 'Print ipower(2&&, 4)
  5. 'Print ipower(2&&, 5)
  6. Print ipower(3&&, 40)
  7.  
  8.     'take x to an integer power
  9.     Dim As _Integer64 z, y
  10.     Dim As Long f
  11.     y = x
  12.     n = e
  13.     z = 1&&
  14.     While n > 0
  15.         While (n And 1) = 0
  16.             n = n \ 2
  17.             f = mul_overflow(y, y, y) 'y = y * y
  18.             If f Then Print "an overflow occured"
  19.         Wend
  20.         n = n - 1
  21.         f = mul_overflow(y, z, z) ' z = y * z
  22.         If f Then Print "an overflow occured"
  23.     Wend
  24.     ipower = z
  25.  
« Last Edit: September 30, 2021, 11:47:38 am by jack »

Offline jack

  • Seasoned Forum Regular
  • Posts: 408
    • View Profile
Re: Possible error in INTEGER64 calculations
« Reply #24 on: September 30, 2021, 12:18:31 pm »
a little better
Code: QB64: [Select]
  1. ' reference https://gcc.gnu.org/onlinedocs/gcc/Integer-Overflow-Builtins.html
  2.     Function mul_overflow& Alias "__builtin_smulll_overflow" (ByVal a As _Integer64, Byval b As _Integer64, res As _Integer64)
  3.  
  4. Print ipower(3&&, 39)
  5. Print ipower(3&&, 40)
  6.  
  7.     'take x to an integer power
  8.     Dim As _Integer64 z, y
  9.  
  10.     y = x
  11.     n = e
  12.     z = 1&&
  13.     While n > 0
  14.         While (n And 1) = 0
  15.             n = n \ 2
  16.             If mul_overflow(y, y, y) Then Print "an overflow occured"
  17.         Wend
  18.         n = n - 1
  19.         If mul_overflow(y, z, z) Then Print "an overflow occured"
  20.     Wend
  21.     ipower = z
  22.  

Offline jack

  • Seasoned Forum Regular
  • Posts: 408
    • View Profile
Re: Possible error in INTEGER64 calculations
« Reply #25 on: September 30, 2021, 12:33:22 pm »
hello everyone, why didn't you think of the obvious simple solution?
a positive integer that overflows will be negative
never mind, that doesn't always work
« Last Edit: September 30, 2021, 12:40:47 pm by jack »

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Possible error in INTEGER64 calculations
« Reply #26 on: September 30, 2021, 12:52:24 pm »
Quote
hello everyone, why didn't you think of the obvious simple solution?
a positive integer that overflows will be negative

But only for a little while then it builds up to positive number again, just wrong and way too small!

This https://www.qb64.org/forum/index.php?topic=4210.msg135991#msg135991
shows positive result only it's wrong.

Is it possible numbers could slip through negative detection while calculating result?

@jack your built-in detector of overflow (in C++ ?) may be the better.

Offline Pete

  • Forum Resident
  • Posts: 2361
  • Cuz I sez so, varmint!
    • View Profile
Re: Possible error in INTEGER64 calculations
« Reply #27 on: September 30, 2021, 11:39:39 pm »
String math
How'd you get so funky
String Math
Can you use a hot key
String Math
Made by an Egyptian
String Math
Even does division
Born in Windows 7
Works in Win-11
String Math
Want to learn how to write code on cave walls? https://www.tapatalk.com/groups/qbasic/qbasic-f1/

Offline Richard

  • Seasoned Forum Regular
  • Posts: 364
    • View Profile
Re: Possible error in INTEGER64 calculations
« Reply #28 on: October 29, 2021, 12:03:02 pm »
@david_uwi


https://www.qb64.org/forum/index.php?topic=2276.msg137521#msg137521  Reply #183


You may be interested in reading the above reply as it addresses the problem you are having. 

Note that there is a difference between "Overflow/Overrange" and "Numbers not representable".