Opened 3 years ago

Closed 2 years ago

# 2 ^ 49 and further return wrong results

Reported by: Owned by: timsky AutoIt 3.3.14.5 None

### Description

Bug starts at 2 49 and further﻿. Sample code:

```\$iInt64 = Int(2 ^ 49, 1)
ConsoleWrite(\$iInt64 & @lf)
```
```562949953421313
```

### comment:1 in reply to: ↑ description Changed 3 years ago by t

Bug starts at 2 49 and further﻿. Sample code:

```\$iInt64 = Int(2 ^ 49, 1)
ConsoleWrite(\$iInt64 & @lf)
```
```562949953421313
```

Sorry there is a typo. Code should be:

```\$iInt64 = Int(2 ^ 49, 2)
ConsoleWrite(\$iInt64 & @lf)
```

### comment:2 Changed 2 years ago by water

Telling us that something is wrong isn't enough.
As the wiki states: A ticket "should contain all the information necessary to reproduce the issue".
So please provide the expected (correct) result as well.

### comment:3 Changed 2 years ago by Jpm

• Resolution set to Wont Fix
• Status changed from new to closed

Hi,
the result must Be 562949953421312
In fact the problem comes from the fact that 2 49 in a floating point which reach the max precision 15/16 digits so the conversion to integer is "rounded".
I Don't fully Understand why Int( 2
48 * 2, 2) is OK but the rounding as Something to do in the internal calculation.

You cannot expect that integer exact conversion in the range

### comment:4 Changed 8 months ago by jchd18

Sorry but the "max precision of 15/16 digits" has nothing to do here. The max number of exact digits output by printf() and friends, as the example below clearly shows, is determined by the upper integral value stored exactly in a FP double.

Direct cast from double to long long gives the correct result up to 253 (9007199254740992). You can check that the conversion from FP to string in plain C also is correct up to 253.

So there is no valid reason why AutoIt would yield a wrong result below the limiting value, which is actually hardware-dependant (FP used in PCs has a 53-bit mantissa).

Here's a very simple C program illustrating the facts:

```#include <math.h>
#include <stdio.h>

int main(int argc, char* argv[]) {
long long n;
double f;
int i;
printf("\n%s\t%20s\t%20s\t%20s\n", "i", "(1 << i) - 1", "f = pow(2, i) - 1", "(long long) f");
for (i = 47; i < 55; i++) {
n = (1LL << i) - 1;
f = pow(2, i) - 1;
printf("%d\t%20lli\t%20.0lf\t%20lli\n", i, n, f, (long long) f);
};
printf("\n%s\t%20s\t%20s\t%20s\n", "i", "1 << i", "f = pow(2, i)", "(long long) f");
for (i = 47; i < 55; i++) {
n = 1LL << i;
f = pow(2, i);
printf("%d\t%20lli\t%20.0lf\t%20lli\n", i, n, f, (long long) f);
};
printf("\n%s\t%20s\t%20s\t%20s\n", "i", "(1 << i) + 1", "f = pow(2, i) + 1", "(long long) f");
for (i = 47; i < 55; i++) {
n = (1LL << i) + 1;
f = pow(2, i) + 1;
printf("%d\t%20lli\t%20.0lf\t%20lli\n", i, n, f, (long long) f);
};
printf("\n\n%20s\t%20s\n", "f", "(long long) f");
f = 9007199254740980.0;
for (i = 0; i < 15; i++) {
printf("%20.0lf\t%20lli\n", f + i, (long long) f + i);
};
}
```

and here's the result:

```PS C:\tcc64\tcc> .\fpconv

i               (1 << i) - 1       f = pow(2, i) - 1           (long long) f
47           140737488355327         140737488355327         140737488355327
48           281474976710655         281474976710655         281474976710655
49           562949953421311         562949953421311         562949953421311
50          1125899906842623        1125899906842623        1125899906842623
51          2251799813685247        2251799813685247        2251799813685247
52          4503599627370495        4503599627370495        4503599627370495
53          9007199254740991        9007199254740991        9007199254740991
54         18014398509481983       18014398509481984       18014398509481984

i                     1 << i           f = pow(2, i)           (long long) f
47           140737488355328         140737488355328         140737488355328
48           281474976710656         281474976710656         281474976710656
49           562949953421312         562949953421312         562949953421312
50          1125899906842624        1125899906842624        1125899906842624
51          2251799813685248        2251799813685248        2251799813685248
52          4503599627370496        4503599627370496        4503599627370496
53          9007199254740992        9007199254740992        9007199254740992
54         18014398509481984       18014398509481984       18014398509481984

i               (1 << i) + 1       f = pow(2, i) + 1           (long long) f
47           140737488355329         140737488355329         140737488355329
48           281474976710657         281474976710657         281474976710657
49           562949953421313         562949953421313         562949953421313
50          1125899906842625        1125899906842625        1125899906842625
51          2251799813685249        2251799813685249        2251799813685249
52          4503599627370497        4503599627370497        4503599627370497
53          9007199254740993        9007199254740992        9007199254740992
54         18014398509481985       18014398509481984       18014398509481984

f           (long long) f
9007199254740980        9007199254740980
9007199254740981        9007199254740981
9007199254740982        9007199254740982
9007199254740983        9007199254740983
9007199254740984        9007199254740984
9007199254740985        9007199254740985
9007199254740986        9007199254740986
9007199254740987        9007199254740987
9007199254740988        9007199254740988
9007199254740989        9007199254740989
9007199254740990        9007199254740990
9007199254740991        9007199254740991
9007199254740992        9007199254740992
9007199254740992        9007199254740993       <<--
9007199254740994        9007199254740994
PS C:\tcc64\tcc>
```

• In-depth discussions should take place on the forum.

For more information see the full version of the ticket guidelines here.