c - 我应该如何获得浮点值的小数部分?

标签 c floating-point c99 floor libm

我有一个 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 as arg. The integral part (in floating-point format) is stored in the object pointed to by iptr.

关于c - 我应该如何获得浮点值的小数部分?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65576961/

相关文章:

java - 使用小数据类型是否会减少内存使用(来自内存分配而不是效率)?

c++ - 异步 http 客户端库和 glib 事件循环

将简单的 C 代码转换为 CUDA 代码

c - 交叉格式说明符的 printf 输出

c - 内存分配/重新分配问题

C中的复合文字存储持续时间

c++ - C++ 中 float、double 或 long double 的精度是什么意思?

c 代码不适用于带有 float 的 Turbo C++

c++ - 将 float 转换为字符串的最快 C++ 方法

c - 长双数学库实现?