How does std::cout print negative zero in a ones-complement system?

前端 未结 4 1550
遇见更好的自我
遇见更好的自我 2021-01-17 09:34

On a ones-complement platform, what would the following code print?

#include 

int main() {
    int i = 1, j = -1;

    std::cout << i+         


        
4条回答
  •  醉话见心
    2021-01-17 10:14

    Looking through the glibc source code, I found these lines in vfprintf.c:

    532       is_negative = signed_number < 0;                    \
    533       number.word = is_negative ? (- signed_number) : signed_number;      \
    534                                           \
    535       goto LABEL (number);                            \
    ...
    683       if (is_negative)                            \
    684         outchar (L_('-'));                            \
    

    So it would appear that the condition is signed_number < 0, which would return false for a -0.

    as @Ysc mentioned, nothing in the documentation gives any specification to printing -0, so a different implementation of libc (on a ones-compliment) platform may yield a different result.

提交回复
热议问题