天道酬勤,学无止境

fmodf

modf 返回 1 作为小数:(modf returns 1 as the fractional:)

问题 我有这个静态方法,它接收一个 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 << endl; } modf(number, &returnVal)

2021-06-24 23:53:11    分类:技术分享    c++   integer   double   fractions   fmodf

modf returns 1 as the fractional:

I have this static method, it receives a double and "cuts" its fractional tail leaving two digits after the dot. works almost all the time. I have noticed that when it receives 2.3 it turns it to 2.29. This does not happen for 0.3, 1.3, 3.3, 4.3 and 102.3. Code basically multiplies the number by 100 uses modf divides the integer value by 100 and returns it. Here the code catches this one specific number and prints out: 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 <

2021-05-13 04:57:45    分类:问答    c++   integer   double   fractions   fmodf