在C++ C中:输出:"1610612736"
#include <math.h>
#include <stdio.h>
int main(int argc, char** argv)
{
printf("%d\n", fmodf(5.6f, 6.4f));
getchar();
}
Run Code Online (Sandbox Code Playgroud)
在C#中:输出:"5.6"
using System;
static class Program
{
public static void Main(string[] args)
{
Console.WriteLine(5.6f % 6.4f);
Console.Read();
}
}
Run Code Online (Sandbox Code Playgroud)
显然输出不一样.建议?
你好,我希望有人可以帮助我.
我正在使用math.h中的float fmodf(float_x,float_y)函数.
我能够正确编码,但我只是想知道有没有人知道函数的确切代码是什么,所以我可以更好地理解它
我有这个静态方法,它接收一个double并"切割"它的小数尾部,在点之后留下两位数.工程几乎所有的时间.我注意到当它收到2.3时它会变成2.29.0.3,1.3,3.3,4.3和102.3不会发生这种情况.代码基本上将数字乘以100使用modf将整数值除以100并返回它.这里代码捕获了这个特定的数字并打印出来:
static double dRound(double number) {
bool c = false;
if (number == 2.3)
c = true;
int factor = pow(10, 2);
number *= factor;
if (c) {
cout << " number *= factor : " << number << endl;
//number = 230;// When this is not marked as comment the code works well.
}
double returnVal;
if (c){
cout << " fractional : " << modf(number, &returnVal) << endl;
cout << " integer : " <<returnVal << …Run Code Online (Sandbox Code Playgroud) 鉴于const auto foo = 13.42我想得到数字小数部分,所以.42
我倾向于使用fmod像:fmod(foo, 1.0)
但我也可以做foo / static_cast<int>(foo) - 1.0或者或许其他一些神秘的方法.
我不会简单地使用动机fmod吗?
如果小数部分的前3位数字包含"9",则此代码应显示,但不起作用.令人惊讶的是,"mod"变量对于任何数字都是0.
int main( void )
{
float number, dmod;
int mod;
double digit_1, digit_2, digit_3;
double search=9;
cout<<"Enter the number:";
cin>>number;
mod = modf(number, &dmod);
digit_1 = mod /100 % 10;
digit_2 = mod /10 % 10;
digit_3 = mod /1 % 10;
if( (digit_1 == search) || (digit_2 == search) || (digit_3 ==search) )
{
cout<<"mod contains 9"<<endl;
}
else
{
cout<<"mod does not contains 9"<<endl;
}
}
Run Code Online (Sandbox Code Playgroud)