Created
March 25, 2015 16:46
-
-
Save tomsmeding/494da58726843984bcea to your computer and use it in GitHub Desktop.
Minus zero ≠ zero in C++ (and C and other C-based languages)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <iomanip> | |
#include <cstdint> | |
#include <cstring> | |
using namespace std; | |
typedef float fltp; | |
int main(void){ | |
fltp zero=0,mzero=-zero; | |
uint8_t zero_bytes[sizeof(fltp)],mzero_bytes[sizeof(fltp)]; | |
memcpy(zero_bytes,&zero,sizeof(fltp)); | |
memcpy(mzero_bytes,&mzero,sizeof(fltp)); | |
cout<<" 0: "; for(int i=0;i<sizeof(fltp);i++)cout<<hex<<(int) zero_bytes[i]<<' '; cout<<endl; | |
// -> 0: 0 0 0 0 | |
cout<<"-0: "; for(int i=0;i<sizeof(fltp);i++)cout<<hex<<(int)mzero_bytes[i]<<' '; cout<<endl; | |
// -> -0: 0 0 0 80 | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment