<table border="1" cellspacing="0" cellpadding="8">
<tr>
<th>Issue</th>
<td>
<a href=https://github.com/llvm/llvm-project/issues/55344>55344</a>
</td>
</tr>
<tr>
<th>Summary</th>
<td>
Unexpected overflow of a long variable
</td>
</tr>
<tr>
<th>Labels</th>
<td>
new issue
</td>
</tr>
<tr>
<th>Assignees</th>
<td>
</td>
</tr>
<tr>
<th>Reporter</th>
<td>
danolivo
</td>
</tr>
</table>
<pre>
I wanted to avoid overflow during conversion double -> long, but found an unexpected behaviour. Minimal example:
```
#include "math.h"
#include "stdio.h"
#define Min(x, y) ((x) < (y) ? (x) : (y))
int
main()
{
long lg;
double dg = 1.E+23;
lg = (long) Min(dg, (double) __LONG_MAX__);
printf("Result: lg=%ld\n", lg);
return 0;
}
```
Compiling with gcc I see:
Result: lg=9223372036854775807
but with clang:
Result: lg=-9223372036854775808
Why casting of LONG_MAX increases the value by 1?
P.S.
```
clang -v
Homebrew clang version 13.0.1
Target: x86_64-apple-darwin21.4.0
Thread model: posix
InstalledDir: /usr/local/opt/llvm/bin
```
P.P.S.
It looks like https://github.com/openvinotoolkit/openvino/issues/7469
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJx9VNtu2zgQ_Rr6ZWBBpu4PekjipBtgs1tsu9i-GZQ4lthQpCBSvvz9kpRTu21agBCHM-SZMzc1mp_rZzgyZZGD1cAOWnDQB5z2Uh-Bz5NQHbRaOY0RWgHXcyMR1iR5BKlVR-gDNLOFvZ4VB6ZgVngasfV4DfbsIPQ8RfAilBiYBDyxYZRIkjsSb0l8R_L4spYjTYRq5cwRCKUDs33UO-E9o7Fc6O-tHPdCofdFaHnyzM6EViQOi5aLsgKSPDiA8rzIT3DV373pwzOPKpRdhIEF1G8GUtxfhLjyeQDZkeSquqSJdw51C5vokdB7mtzekIvJYS5prC68eUipFwKEN-x2f_7914fdy92X3c4TuEEZXX3sPhCj_6CZpfVReC5bQjPJSfagfIocpOx-eDuhnScF8VVXbN-vSvg-6GEU0rfDUdgeuraFZzB4reUP_itKk6SgcZKXWVoUWRkXt3C-aQJQK5mL_xcg659RyluU__oztMxYT0vv4S1P4BplQmbQgO0RDkzOCM0ZNq7et88_Rp-id0MOpGB9WE5_6AGbCY8LV3ibhU0SxdFmufKZTR0G3qcy3-Xpmo2uz9ecTUeh6CZKowvy594R4zBojtJfH7URp8X0rIxlUiLfimlpxqfZTO4rdcuk2_Vo_UkeBrc1rl1-Xa2P0TW2Z-tGVb8akOIVobd2ND7f9MmtzpVgbqJWDwEf1UEobbWWr8LeaJwojJnROKFI82rF64RXScVWVliJ9b_Xqf_283D1YOEf4fI_CeaaeTVPsv6N_0tkfluPk_7qAG8dZ1mSpqu-pqyoGsb2eYEFT5MkbaqmxaasqrKsYixXkjUoTU2ye9f7ytUtQPg5yLYrUdOY0jiLqzhLcppFecz3zNWExhuKecNIGqMbdxl5HpGeutVUB0rN3BlnlMJYczUyY0SnEIM7h89m2-up5kxpKQ56FXzXgfv_yR2WRQ">