Created
October 9, 2018 01:24
-
-
Save tilkinsc/e76057faa2280da80f20327eebf0097c to your computer and use it in GitHub Desktop.
Flooring a float in C
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
/** | |
* floating points: | |
* 32 bit number | |
* | |
* 0-22 mantissa | |
* 23-30 exp | |
* 31 sign | |
* | |
* Theory: You can use the exp potion to determine the offset and null out the bits to produce a floored number. | |
* However, its just a theory I had. I couldn't 100% determine how many bits I needed to cut off, but I did, however, | |
* determine that you can get up to 120 percision using an offset of 18. | |
* ex: floorf32(120.1239, 18) -> 120 | |
* | |
* Another thing is, the compiler may get in your way with errors. | |
*/ | |
float floorf32(float in, uint32_t offset) { | |
return (float)(*((uint32_t*)&in) >> offset) << offset; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment