I have a variable x
of type float
, and I need its fractional part. I know I can get it with
x - floorf(x)
, orfmodf(x, 1.0f)
My questions: Is one of these always preferable to the other? Are they effectively the same? Is there a third alternative I might consider?
Notes:
x
. I don't mind this behavior or that, but I need to know what the behavior is.Is there a third alternative I might consider?
There's the dedicated function for it. modff exists to decompose a number into its integral and fractional parts.
float modff( float arg, float* iptr );
Decomposes given floating point value
arg
into integral and fractional parts, each having the same type and sign asarg
. The integral part (in floating-point format) is stored in the object pointed to byiptr
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With