我有一个 float
类型的变量 x
,我需要它的小数部分。我知道我可以得到它
x - floorf(x)
,或fmodf(x, 1.0f)
我的问题:其中一个总是比另一个更可取吗?它们实际上是一样的吗?我可以考虑第三种选择吗?
注意事项:
- 如果答案取决于我使用的处理器,让我们将其设为 x86_64,如果您能详细说明其他处理器,那会很好。
- 请确认并引用
x
负值的行为。我不介意这种或那种行为,但我需要知道这种行为是什么。
最佳答案
Is there a third alternative I might consider?
它有专门的功能。 modff存在将数字分解为整数部分和小数部分。
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
.
关于c - 我应该如何获得浮点值的小数部分?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65576961/