Sign in to follow this  
nullsquared

"Snapping" to certain "precision"

Recommended Posts

Very basic problem here, but I just can't wrap my head around it. Maybe I'm too tired or something :( Anyways, say I have some "precision" defined:
float units = 1;
Now, how do I "snap" some arbitrary float to this precision? For example:
float a = 1.8; // in the above case, this should "snap" to 2
units = 10;
// now a show "snap" to 0
Ideas? Any help is really appreciated [smile] EDIT: Hm. Just had a second thought:
float div = a / units;
div = (int)(a + 0.5);
float snapped = div * units;
Did I get that right?

Share this post


Link to post
Share on other sites

#include <cmath>

float snapped = floor(a/units + 0.5f)*units;

Don't cast float to int, you can get overflows when the float value is too large.

Share this post


Link to post
Share on other sites
Quote:
Original post by DevFred

#include <cmath>

float snapped = floor(a/units + 0.5f)*units;

Don't cast float to int, you can get overflows when the float value is too large.


Got it [smile].

Thanks.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this