Skip to content

Instantly share code, notes, and snippets.

@m-chaturvedi
Created November 26, 2022 21:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save m-chaturvedi/673e9bd378b8b5ada02a377bbb3a5bc0 to your computer and use it in GitHub Desktop.
Save m-chaturvedi/673e9bd378b8b5ada02a377bbb3a5bc0 to your computer and use it in GitHub Desktop.
negative float to unsigned int
$ cat test_burkhard.cpp
int main()
{
float f = -5.33;
unsigned int n = static_cast<unsigned int>(f);
return 0;
}
$ clang++ -fsanitize=undefined test_burkhard.cpp
$ ./a.out
test_burkhard.cpp:4:48: runtime error: -5.33 is outside the range of representable values of type 'unsigned int'
SUMMARY: UndefinedBehaviorSanitizer: undefined-behavior test_burkhard.cpp:4:48 in
$ clang++ --version
clang version 11.0.0 (https://github.com/llvm/llvm-project.git 0160ad802e899c2922bc9b29564080c22eb0908c)
Target: x86_64-unknown-linux-gnu
Thread model: posix
InstalledDir: /home/chaturvedi/.linuxbrew/bin
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment