如何在C中为双数据类型取mod [英] How to take mod for double data type in C

查看:68
本文介绍了如何在C中为双数据类型取mod的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想采用双数据类型的mod来检查它是否均匀。



我尝试了什么:



sry for that ...

i want to take mod of double data type to check if its even or not.

What I have tried:

sry for that...

#include <stdio.h>

 
int main()
{
     long n;
    scanf("%d",&n);
    while(n--){
        double g;
        scanf("%d",&g);
        if(((int)g)%2==0)
        printf("YES\n");
        else
        printf("NO\n");
        
    }
}



限制条件

1< = n< = 100000

1< = g< = 10 ^ 100



这里n是否定的。测试用例

和g是否定的。检查它是否均匀。

sirr它无效。


Constraints
1<=n<=100000
1<=g<=10^100

here n is the no. of test case
and g is the no. to check whether its even or not.
sirr its not working.

推荐答案

通过强制转换将其转换为int,并使用整数模数运算符%:

Convert it to an int by casting, and use the integer modulus operator "%":
if (((int) myDouble) % 2 == 0)
   {
   // It's even.
   ...





和BTW:不要再次发誓这个网站了:这是不必要的,不专业的,不需要的。再做一次,你将被视为一个巨魔,并被禁止从该网站。



And BTW: do not swear on this site again: it was unnecessary, unprofessional, and unwanted. Do it again and you will be treated as a troll, and banned from the site.


双打是偶数或奇数(他们的概率几乎为零)。你必须将它们转换为整数。你这样做的方式可能会偏向结果:你可以选择,例如截断double值(如 Griff 建议的那样)或将其舍入。
Doubles aren't even or odd (they are with near zero probability). You have to convert them to integers. The way you do it may bias the result: you may choose, for instance to truncate the double value (as Griff suggested) or to round it.


这篇关于如何在C中为双数据类型取mod的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆