www.digitalmars.com         C & C++   DMDScript  

c++ - Bad multiplication

reply Piotr Grochowski <piotrunio-2004 wp.pl> writes:
When I compile and run this script,

```cpp
#include <iostream>
int main(){unsigned long long q=20;q=q*50;std::cout<<q;return 0;}
```

The expected output is 1000, as in Visual C++ 2005:
```
C:\Users\Acer\Documents\test>cl /EHsc debug.cpp
Microsoft (R) 32-bit C/C++ Optimizing Compiler Version 
14.00.50727.42 for 80x86
Copyright (C) Microsoft Corporation.  All rights reserved.

debug.cpp
Microsoft (R) Incremental Linker Version 8.00.50727.42
Copyright (C) Microsoft Corporation.  All rights reserved.

/out:debug.exe
debug.obj

C:\Users\Acer\Documents\test>debug
1000
```

but in Digital Mars C++ compiler, it makes it print 4294967297000 
instead.

```
C:\Users\Acer\Documents\test>dmc debug.cpp -I 
D:/dm/stlport/stlport
link debug,,,user32+kernel32/noi;


C:\Users\Acer\Documents\test>debug
4294967297000
```
Aug 21 2022
parent Piotr Grochowski <piotrunio-2004 wp.pl> writes:
Additional test cases:

```cpp
#include <iostream>
int main(){unsigned long long q=20480,w=488281250;const unsigned 
long long a=20480,s=488281250;
std::cout<<a*s<<' '<<a*w<<' '<<q*s<<' '<<q*w;return 0;}
```

```
10000000000000 5652766404163837952 5652766404163837952 
10000000000000
```

```cpp
#include <iostream>
int main(){unsigned long long q=8192,w=1220703125;const unsigned 
long long a=8192,s=1220703125;
std::cout<<a*s<<' '<<a*w<<' '<<q*s<<' '<<q*w;return 0;}
```

```
10000000000000 10000000000000 5652766404163837952 10000000000000
```

```cpp
#include <iostream>
int main(){unsigned long long q=524288,w=19073486328125;const 
unsigned long long a=524288,s=19073486328125;
std::cout<<a*s<<' '<<a*w<<' '<<q*s<<' '<<q*w;return 0;}
```

```
10000000000000000000 10000000000000000000 9939201405030498304 
10000000000000000000
```
Aug 21 2022