Jump to content
  • Advertisement
Sign in to follow this  
JohnnyCode

Floor functon definition

This topic is 1829 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi.

 

Is it possible to return a closest integer to a floating number by defining a funtion that uses only +,-,*,/ operations?

Share this post


Link to post
Share on other sites
Advertisement

I must admit I do not see how this could work though. Could you elaborate on the definition a bit? About the two constants and such. Thanks a lot

Share this post


Link to post
Share on other sites
Let's start with a number that smallish (say, less than 4 million). The constant is designed so the sum of x plus the constant will have its exponent so large that the 24 bits of precision in a float will precisely allow it to represent integers (meaning, the distance between consecutive floats in this range is 1). That sum is where the rounding happens, because the bits that don't fit are discarded, hopefully with some reasonable rounding rules. The subtraction of the magic constant brings the number back to its original range, but we have lost the bits beyond the integer part.

EDIT: Removed some nonsense about adding 0.5. Edited by Álvaro

Share this post


Link to post
Share on other sites

What would the constant be for 64 bit floats? I gess the same since exponent is the same range as in 64 bit, but it seems not to work.

 

I could truncate the float by shifting significant bits to right by amount of (bias)-(exponent unbiased) times and then set exponent to bias. But I cannot afford bitwise operations, only algebraic ones, how could I do this with algebraic operation? Thanks a lot

Share this post


Link to post
Share on other sites

Is this some kind of arbitrary challenge? What's the actual problem, and why can you only use +,-,*,/?

Share this post


Link to post
Share on other sites

Is this some kind of arbitrary challenge? What's the actual problem, and why can you only use +,-,*,/?

I need to do the operation on gpu, a float resulting to a float. So I cannot use things like modulo, or bitwise ops. Using standard operations would also make this definable as a proper math function, but that is not my concern.

Share this post


Link to post
Share on other sites

GPU's can do floor natively, so using the built-in floor function will most likely be much faster than emulating it yourself using a bunch of arithmetic.

Is there a reason you're avoiding the built-in implementation?

Share this post


Link to post
Share on other sites
The constant for a double would be 3*2^51 = 6755399441055744.0 Edited by Álvaro

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!