我可以将 NULL 传递给 modf/modff 吗?

标签 c math floating-point language-lawyer fmodf

modf() family of functions都带一个指针参数:

float       modff( float arg, float* iptr );
double      modf( double arg, double* iptr );
long double modfl( long double arg, long double* iptr );

用于返回其输入arg的整数部分(小数部分为实际返回值)。

如果我只需要小数部分,我可以通过传递NULL来“跳过”整数部分的计算吗?

无论是我喜欢的 cppreference 页面,还是 man modff,都没有说明当您为 iptr 传递 NULL 时会发生什么,而一些其他标准库函数将 NULL 作为“忽略此参数”的指示。

最佳答案

standard没有定义这个

Description

2 The modf functions break the argument value into integral and fractional parts, each of which has the same type and sign as the argument. They store the integral part (in floating-point format) in the object pointed to by iptr.

Returns

3 The modf functions return the signed fractional part of value.

因此传递 NULL 时的行为将是未定义的。您可能会发现一些允许它的编译器,但任何依赖它的代码都不可移植(即使在同一编译器的不同版本之间)。

关于我可以将 NULL 传递给 modf/modff 吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65578459/

相关文章:

c++ - 0.1 float 大于 0.1 double。我以为是假的

c - 结构成员变量未定义 - 没有命名的成员

c - C编程中如何判断给定的数组是否按降序排列

c - 为什么要使用 _mm_malloc? (相对于 _aligned_malloc、alligned_alloc 或 posix_memalign)

python - 了解因式分解函数

java - 如何使用 Point2D.Double 作为点绘制多边形?

c - 如何使一个数组等于C中的另一个数组

java - Android - 从 0,0 开始获取触摸位置

java - 实现自定义算术系统

java - Java中的整数除法