Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Casting UINT64 to float?

Tags:

c++

Is it safe to cast a UINT64 to a float? I realize that UINT64 does not hold decimals, so my float will be whole numbers. However, my function to return my delta-time returns a UINT64, which isn't a very useful type for the function I'm currently working with. I'm assuming a simple static_cast<float>(uint64value) will not work?

like image 704
Chris Avatar asked Sep 19 '25 00:09

Chris


2 Answers

Large values of UINT64, (an 8 byte value) may be truncated if you cast them to a float, which is only 4 bytes.

like image 184
Charles Salvia Avatar answered Sep 20 '25 15:09

Charles Salvia


Define safe - you can easily lose a lot of digits of precision if the 64-bit value is large, but apart from that (which is presumably a known issue that you don't mind about), the conversion should be safe. If your compiler doesn't handle it correctly, get a better compiler.

like image 39
Jonathan Leffler Avatar answered Sep 20 '25 16:09

Jonathan Leffler