c++ - Why "int" variable doesn't print negative zero? -


in following program, float variable print negative zero.

//g++  5.4.0  #include <iostream>  int main() {     float val = 0;     val = -val;      std::cout<<val<<std::endl; } 

output:

-0 

but, in following code

//g++  5.4.0  #include <iostream>  int main() {     int val = 0;     val = -val;      std::cout<<val<<std::endl; } 

output:

0 

print positive zero.

why int variable doesn't print negative zero?

there's no such thing negative 0 in set of mathematical integers.

some used machine representations of integers have more 1 representation of zero, , in case 1 of them may customarily called "negative zero". c++ language doesn't allow exposing such representation programmer through normal integer apis. if computer has such representation, doesn't, not printed -0.

on other hand, common representation of floating point numbers has signed zeros, , c++ standard allows implementation expose sign programmer.


Comments

Popular posts from this blog

ubuntu - PHP script to find files of certain extensions in a directory, returns populated array when run in browser, but empty array when run from terminal -

php - How can i create a user dashboard -

javascript - How to detect toggling of the fullscreen-toolbar in jQuery Mobile? -