Quantcast
Channel: Active questions tagged gcc - Stack Overflow
Viewing all articles
Browse latest Browse all 22122

Casting from double always returns zero

$
0
0

Why does the following code returns zero, when compiled with clang?

#include <stdint.h>
uint64_t square() {
    return ((uint32_t)((double)4294967296.0)) - 10;
}

The assembly it produces is:

    push    rbp
    mov     rbp, rsp
    xor     eax, eax
    pop     rbp
    ret

I would have expected that double to become zero (integer) and that minus would wrap it around. In other words, why doesn't in matter what number there is to subtract, as it always produces zero? Note that gcc does produce different numbers, as expected:

    push    rbp
    mov     rbp, rsp
    mov     eax, 4294967285
    pop     rbp
    ret

I assume casting 4294967296.0 to uint32_t is undefined behaviour but even then, I would expect to produce different results for different subtrahends.


Viewing all articles
Browse latest Browse all 22122

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>