• Advertisement
Sign in to follow this  

How to get float value that is less then 1 without the higher part

This topic is 1359 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

Advertisement

Look at it from a different angle instead: how do you obtain the integer part so that you can subtract it from the original number? You can cast the float to an integer, or using the floor function, for example.

Share this post


Link to post
Share on other sites

With c++11:

 

float number = 1.332;

float truncatedNumber = trunc(number);

float wantedNumber = number - truncatedNumber;

Share this post


Link to post
Share on other sites


I can't think of any circumstances where you want the answer to be outside of [0,1), so I wouldn't use truncation.

True -- if negative numbers don't matter, floor would be preferred instead of trunc.

Share this post


Link to post
Share on other sites

modf returns the fractional and integer part of a floating point number.

If you do it with subtraction then remember to check that it gives you the answer you want for negative numbers.

This is the solution i ended up using.

Thank you on help and i kinda forgot i made this topic mellow.png .

Share this post


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

  • Advertisement