Given const auto foo = 13.42
I would like to get the numbers fractional part, so .42
I'm leaning toward just using fmod
like: fmod(foo, 1.0)
But I could also just do foo / static_cast<int>(foo) - 1.0
Or perhaps some other mystical method.
Would there be a motivation for me to not simply use fmod
?
Two ways I can think of, either a cast or rounding down with std::floor
int main()
{
const auto foo = 13.53;
auto firstWay = foo - static_cast<long long>(foo); // Truncating via cast and subtracting
auto otherWay = foo - std::floor(foo); // Rounding down and subtracting
return 0;
}
Quick Bench Result shows the fmod
approach as the slowest option by far, and the cast as the fastest: QuickBench