Why the expression (int) +1e10 doesn't yield -2147483648 as CSAPP describes?

Viewed 310

I am reading CS:APP (an x86-64 assembly / low-level textbook) and it mentions:

From float or double to int, the value will be rounded toward zero. For example, 1.999 will be converted to 1, while −1.999 will be converted to −1. Furthermore, the value may overflow. The C standards do not specify a fixed result for this case. Intel-compatible microprocessors designate the bit pattern [10 ... 00] (TMinw for word size w) as an integer indefinite value. Any conversion from floating point to integer that cannot assign a reasonable integer approximation yields this value. Thus, the expression (int) +1e10 yields -2147483648, generating a negative value from a positive one.

What is Intel-compatible microprocessors mentioned here? x86 architecture including AMD series?

Anyway, I have an Intel i5 with Win10 64bit machine and I tried under Visual Studio:

    #include <iostream>
    using namespace std;
    
    int main() {
        int b = (int)+1e10;
        cout << b << endl;
    }

and gets 1410065408 as output.

Also I tried int32_t and gets 1410065408 too.

So why don't I have the result -2147483648 which is [10 ... 00] as the book describes?

1 Answers

Even if the processor used has "Any conversion from floating point to integer that cannot assign a reasonable integer approximation yields this value.", the compiler is not required to follow that as it can use other code to achieve the goal.

In particular, values that can be determine at compiler time like some_32_bit_int = (int)+1e10; may get an value like some_32_bit_int = 10000000000 & 0xFFFFFFFF; or 1410065408 that is completely independent of a processor.

If the value of the integral part cannot be represented by the integer type, the behavior is undefined. C17dr § 6.3.1.4 1

The book describes the processor, not the compiler.

Related