整数部分为双 [英] Integer part of double
问题描述
何来获取double _as int value_的整数部分的有效值?
这是一个演示问题的程序。
------ foo.c ------
#include< stdio.h>
#include< math.h>
int main()
{
double d1 = 0.0;
double d1i = 0.0;
double d1f = 0.0;
int i1 = 0;
int i2 = 0;
size_t k = 0;
for(k = 0; k <1000; k ++)
{
d1 = d1 + 1.1;
i1 =(int)d1;
d1f = modf(d1,& d1i);
i2 =(int)d1i ;
if(((d1 - i1)0.95)||((d1i - i2)0.95))
{
printf(i1 =%3d,i2 =%3d,d1 =%10f,d1 =%16.12f,d1i =
%10f%\ n,i1,i2,d1, d1,d1i);
}
}
返回0;
}
-------------------
------运行------
i1 = 10,i2 = 10,d1 = 11。 000000,d1 = 11.000000000000,d1i =
10.000000
i1 = 76,i2 = 76,d1 = 77.000000,d1 = 77.000000000000,d1i =
76.000000
i1 = 87,i2 = 87,d1 = 88.000000,d1 = 88.000000000000,d1i =
87.000000
i1 = 98,i2 = 98,d1 = 99.000000,d1 = 99.000000000000,d1i =
98.000000
i1 = 109,i2 = 109,d1 = 110.000000,d1 = 110.000000000000,d1i =
109.000000
i1 = 120,i2 = 120,d1 = 121.000000,d1 = 121.000000000000,d1i =
120.000000
i1 = 131,i2 = 131,d1 = 132.000000,d1 = 132.000000000000,d1i =
131.000000
i1 = 142,i2 = 142,d1 = 143.000000,d1 = 143.000000000000 ,d1i =
142.000000
i1 = 153,i2 = 153,d1 = 154.000000,d1 = 154.000000000000,d1i =
153.000000 >
i1 = 164,i2 = 164,d1 = 165.000000,d1 = 165.000000000000,d1i =
164.000000
i1 = 175,i2 = 175,d1 = 176.000000,d1 = 1 75.999999999999,d1i =
175.000000
i1 = 186,i2 = 186,d1 = 187.000000,d1 = 186.999999999999,d1i =
186.000000
i1 = 197,i2 = 197,d1 = 198.000000,d1 = 197.999999999999,d1i =
197.000000
i1 = 208,i2 = 208,d1 = 209.000000,d1 = 208.999999999999,d1i =
208.000000
i1 = 219,i2 = 219,d1 = 220.000000,d1 = 219.999999999999,d1i =
219.000000
i1 = 230,i2 = 230,d1 = 231.000000,d1 = 230.999999999999,d1i =
230.000000
i1 = 241, i2 = 241,d1 = 242.000000,d1 = 241.999999999999,d1i =
241.000000
i1 = 252,i2 = 252,d1 = 253.000000,d1 = 252.999999999999,d1i =
252.000000
i1 = 263,i2 = 263,d1 = 264.000000,d1 = 263.999999999999,d1i =
263.000000
i1 = 274,i2 = 274,d1 = 275.000000,d1 = 274.999999999999,d1i =
274.000000
i1 = 285,i2 = 285,d1 = 286.000000,d1 = 286.0000000 00000,d1i =
285.000000
i1 = 296,i2 = 296,d1 = 297.000000,d1 = 297.000000000000,d1i =
296.000000
-----------------
Alex Vinokur
电子邮件:alex DOT vinokur AT gmail DOT com
http://mathforum.org/library/view/10978。 html
http://sourceforge.net/users/alexvn
Ho to get valid value of integer part of double _as int value_?
Here is a program that demonstrate the problem.
------ foo.c ------
#include <stdio.h>
#include <math.h>
int main ()
{
double d1 = 0.0;
double d1i = 0.0;
double d1f = 0.0;
int i1 = 0;
int i2 = 0;
size_t k = 0;
for (k = 0; k < 1000; k++)
{
d1 = d1 + 1.1;
i1 = (int) d1;
d1f = modf (d1, & d1i);
i2 = (int) d1i;
if ( ((d1 - i1) 0.95) || ((d1i - i2) 0.95) )
{
printf ("i1 = %3d, i2 = %3d, d1 = %10f, d1 = %16.12f, d1i =
%10f%\n", i1, i2, d1, d1, d1i);
}
}
return 0;
}
-------------------
------ Run ------
i1 = 10, i2 = 10, d1 = 11.000000, d1 = 11.000000000000, d1i =
10.000000
i1 = 76, i2 = 76, d1 = 77.000000, d1 = 77.000000000000, d1i =
76.000000
i1 = 87, i2 = 87, d1 = 88.000000, d1 = 88.000000000000, d1i =
87.000000
i1 = 98, i2 = 98, d1 = 99.000000, d1 = 99.000000000000, d1i =
98.000000
i1 = 109, i2 = 109, d1 = 110.000000, d1 = 110.000000000000, d1i =
109.000000
i1 = 120, i2 = 120, d1 = 121.000000, d1 = 121.000000000000, d1i =
120.000000
i1 = 131, i2 = 131, d1 = 132.000000, d1 = 132.000000000000, d1i =
131.000000
i1 = 142, i2 = 142, d1 = 143.000000, d1 = 143.000000000000, d1i =
142.000000
i1 = 153, i2 = 153, d1 = 154.000000, d1 = 154.000000000000, d1i =
153.000000
i1 = 164, i2 = 164, d1 = 165.000000, d1 = 165.000000000000, d1i =
164.000000
i1 = 175, i2 = 175, d1 = 176.000000, d1 = 175.999999999999, d1i =
175.000000
i1 = 186, i2 = 186, d1 = 187.000000, d1 = 186.999999999999, d1i =
186.000000
i1 = 197, i2 = 197, d1 = 198.000000, d1 = 197.999999999999, d1i =
197.000000
i1 = 208, i2 = 208, d1 = 209.000000, d1 = 208.999999999999, d1i =
208.000000
i1 = 219, i2 = 219, d1 = 220.000000, d1 = 219.999999999999, d1i =
219.000000
i1 = 230, i2 = 230, d1 = 231.000000, d1 = 230.999999999999, d1i =
230.000000
i1 = 241, i2 = 241, d1 = 242.000000, d1 = 241.999999999999, d1i =
241.000000
i1 = 252, i2 = 252, d1 = 253.000000, d1 = 252.999999999999, d1i =
252.000000
i1 = 263, i2 = 263, d1 = 264.000000, d1 = 263.999999999999, d1i =
263.000000
i1 = 274, i2 = 274, d1 = 275.000000, d1 = 274.999999999999, d1i =
274.000000
i1 = 285, i2 = 285, d1 = 286.000000, d1 = 286.000000000000, d1i =
285.000000
i1 = 296, i2 = 296, d1 = 297.000000, d1 = 297.000000000000, d1i =
296.000000
-----------------
Alex Vinokur
email: alex DOT vinokur AT gmail DOT com
http://mathforum.org/library/view/10978.html
http://sourceforge.net/users/alexvn
推荐答案
Alex Vinokur写道:
Alex Vinokur wrote:
Ho to get double _as int value_的整数部分的有效值?
Ho to get valid value of integer part of double _as int value_?
如果值为正,则floor()似乎是一个自然的解决方案。
如果值为负,那么ceil()似乎是自然的解决方案。
请记住,在大多数机器上,整数不够大
保存所有可能的floor()/ ceil()值。不要忘记
1e222是一个非常大的整数。
如果你坚持将值存储为整数类型,那么我会在<除非你确定你永远不需要超过10位数的
,否则
最少使用很长的时间。
也许类似于这个:
#include< limits.h>
#include< math.h>
int intpart(double输入,双* dint,长long * lint)
{
if(输入0)
* dint = floor(输入);
其他
* dint = ceil(输入);
if(* dint< LLONG_MAX){
* lint =(long long)* dint;
返回1;
} else {
lint = 0;
返回0;
}
}
If the value is positive then floor() seems a natural solution.
If the value is negative then ceil() seems a natural solution.
Keep in mind that on most machines an integer is not large enough to
hold all possible floor()/ceil() values from a double. Don''t forget
that 1e222 is a very large integer.
If you insist on storing the value in an integral type, then I would at
least use a long long unless you know for sure that you will never need
more than 10 digits.
Maybe something like this:
#include <limits.h>
#include <math.h>
int intpart(double input, double *dint, long long *lint)
{
if (input 0)
*dint = floor(input);
else
*dint = ceil(input);
if (*dint < LLONG_MAX) {
*lint = (long long) *dint;
return 1;
} else {
lint = 0;
return 0;
}
}
Alex Vinokur说:
Alex Vinokur said:
何时获取double _as int value_的整数部分的有效值?
Ho to get valid value of integer part of double _as int value_?
int m = d;
如果它不合适,你就会遇到问题。 />
long int n = d;
可以解决这些问题,但当然可能没有。
如果你只想在你的输出中压制小数,你不需要做任何转换 - 你可以简单地printf(%。0f \ n,d);
-
Richard Heathfield
Usenet是一个奇怪的地方 - dmr 29/7/1999
http://www.cpax.org.uk
电子邮件:rjh在上述域名中, - www。
int m = d;
If it doesn''t fit, you''ve got problems.
long int n = d;
might solve those problems, but of course it might not.
If you just want to suppress decimals in your output, you don''t have to do
any conversion - you can simply printf("%.0f\n", d);
--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.
Richard Heathfield写道:
Richard Heathfield wrote:
Alex Vinokur说:
Alex Vinokur said:
何来获取double _as int value_的整数部分的有效值?
Ho to get valid value of integer part of double _as int value_?
int m = d;
如果它不合适,你就会遇到问题。 />
long int n = d;
可以解决这些问题,但当然可能没有。
如果你只想在你的输出中压制小数,你不需要做任何转换 - 你可以简单地printf(%。0f \ n,d);
int m = d;
If it doesn''t fit, you''ve got problems.
long int n = d;
might solve those problems, but of course it might not.
If you just want to suppress decimals in your output, you don''t have to do
any conversion - you can simply printf("%.0f\n", d);
当然,那轮,而不是截断。
Of course, that rounds, rather than truncates.
-
Richard Heathfield
Usenet是一个奇怪的地方 - dmr 29/7/1999
http://www.cpax.org.uk
电子邮件:rjh在上述域名, - www。
--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.
这篇关于整数部分为双的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!