Integer*integer == integer^2 is not always true

Dear scilab,

while running scilab-2026.0.1 on my elderly MX Linux box
and experimenting with some large numbers using both double
and int64 (available since 6.0) types,
I had an experience that shook my confidence in arithmetic.
I squared 'y=int64("708158977")' and got “501489136705686528”.
This (an even square of an odd prime) aroused some suspicion,
so I tried 'y*y' and got “501489136705686529”.
Casting 'y' to uint64 did not change the results.

To check I applied (via javasci) methods ‘pow’ and 'multiply' of the java.math.BigInteger package to ‘y’.
Both gave identical answers, namely the `y*y’ value.

There must be an AI inside scilab, a set of Anomalous Int64s.
I conjectured that the members of AI are large prime numbers.
But this is not true. Only “large” is a necessary property.
The phenomenon seems to set in at about 94999001, which is 793073917.
You can try yourself some numbers until “3037000001”, which is not far
from the largest int64 that can be squared without overflow. Here
the difference `y*y-y^2’ is -383.

Cordialement Xenomanes

1 Like

Hello,

I translated your examples into executable Scilab code:

--> n = "708158977"; [int64(n)**2 ; int64(n)*int64(n)]

 ans = [2x1 int64]

  501489136705686528
  501489136705686529

There is an implicit cast to double for the power operation, which leads to this behavior. Could you report a bug on Work items · scilab / scilab · GitLab ?

Thanks

1 Like

The result is OK for powers of 2, but is getting worse as we move away from powers of 2:

--> n = 2^31; [int64(n)^2 ; int64(n)*int64(n)]

 ans = [2x1 int64]

  4611686018427387904
  4611686018427387904

--> n = 2^31-1; [int64(n)^2 ; int64(n)*int64(n)]

 ans = [2x1 int64]

  4611686014132420608
  4611686014132420609

--> n = 2^31-20; [int64(n)^2 ; int64(n)*int64(n)]

 ans = [2x1 int64]

  4611685932528042496
  4611685932528042384

--> n = 2^31-200; [int64(n)^2 ; int64(n)*int64(n)]

 ans = [2x1 int64]

  4611685159433968640
  4611685159433968704