Hello.
I am kinda rusty in language so il just make example code;
I need this
float number = 1.332;
float newNumber = RemoveBigNumber(number);
//newNumber = 0.332;
what is the std function for such a method? i cant find it in
Hello.
I am kinda rusty in language so il just make example code;
I need this
float number = 1.332;
float newNumber = RemoveBigNumber(number);
//newNumber = 0.332;
what is the std function for such a method? i cant find it in
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.
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.
With c++11:
float number = 1.332;
float truncatedNumber = trunc(number);
float wantedNumber = number - truncatedNumber;
float mantissa(float x) {
return x - std::floor(x);
}
I can't think of any circumstances where you want the answer to be outside of [0,1), so I wouldn't use truncation. Casting to int has even worse problems, since you also have to worry about values larger than what an int can represent.
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.