C语言 如何在 c 中对浮点值使用 % 运算符

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7500128/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 09:41:28  来源:igfitidea点击:

How to use % operator for float values in c

cmath

提问by Shiv Shakti

When I use % operator on float values I get error stating that "invalid operands to binary % (have ‘float' and ‘double')".I want to enter the integers value only but the numbers are very large(not in the range of int type)so to avoid the inconvenience I use float.Is there any way to use % operator on such large integer values????

当我在浮点值上使用 % 运算符时,我收到错误消息,指出“二进制 % 的操作数无效(有 'float' 和 'double')”。我只想输入整数值,但数字非常大(不在范围内) int 类型)所以为了避免我使用 float 的不便。有没有办法在如此大的整数值上使用 % 运算符????

回答by cyco130

You can use the fmodfunction from the standard math library. Its prototype is in the standard header <math.h>.

您可以使用fmod标准数学库中的函数。它的原型在标准头文件中<math.h>

回答by Marcelo Cantos

You're probably better off using long long, which has greater precision than doublein most systems.

您可能最好使用long long,它比double大多数系统具有更高的精度。

Note: If your numbers are bigger than a long longcan hold, then fmodprobably won't behave the way you want it to. In that case, your best bet is a bigint library, such as this one.

注意:如果您的数字大于 along long可以容纳的数量,那么fmod可能不会按照您希望的方式运行。在这种情况下,最好的选择是 bigint 库,例如this one

回答by John Bode

The %operator is only defined for integer type operands; you'll need to use the fmod*library functions for floating-point types:

%操作者只对整数型操作数所定义; 您需要使用fmod*浮点类型的库函数:

#include <math.h>
double fmod(double x, double y);
float fmodf(float x, float y);
long double fmodl(long double x, long double y);  

回答by Irony

If you want to use an int use long long, don't use a format that is non-ideal for your problem if a better format exists.

如果您想使用 int use long long,如果存在更好的格式,请不要使用不适合您的问题的格式。

回答by Tom Auger

When I haven't had easy access to fmodor other libraries (for example, doing a quick Arduino sketch), I find that the following works well enough:

当我无法轻松访问fmod或其他库时(例如,做一个快速的 Arduino 草图),我发现以下工作足够好:

float someValue = 0.0;

// later...

// Since someValue = (someValue + 1) % 256 won't work for floats...
someValue += 1.0; // (or whatever increment you want to use)
while (someValue >= 256.0){
    someValue -= 256.0;
}

回答by Aman Agarwal

consider : int 32 bit and long long int of 64 bits

考虑:int 32 位和 long long int 64 位

Yes, %(modulo) operator isn't work with floats and double.. if you want to do the modulo operation on large number you can check long long int(64bits)might this help you.

是的, %(modulo) 运算符不适用于浮点数和双精度数long long int(64bits)

still the range grater than 64 bits then in that case you need to store the data in .. string and do the modulo operation algorithmically.

仍然是大于 64 位的范围,那么在这种情况下,您需要将数据存储在 .. 字符串中并通过算法进行模运算。

or either you can go to any scripting language like python

或者你可以使用任何脚本语言,比如 python