舍入浮点数 [英] Rounding a floating point number

查看:108
本文介绍了舍入浮点数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




"我如何将数字舍入到x小数位? ?


这个问题不断出现。我建议以下

解决方案


#include< float.h>

#include< math.h>


double roundto(双x,int数字)

{

int sgn = 1;

if(x == 0)

返回0;

if(digit DBL_DIG)

digits = DBL_DIG;

else if(digits< 1)

digits = 1;


if(x< 0.0){

sgn = -sgn;

x = -x;

}

double p = floorl(log10l(x));

p--;

p = digits-p;

double pow10 = pow(10.0,p);

返回sgn * floor(x * pow10 + 0.5)/ pow10;

}


long double roundtol(long double x,int digits)

{

int sgn = 1;


if(x == 0)

返回0;

if(digits LDBL_DIG)

digits = LDBL_DIG;

else if(digits< 1)

digits = 1 ;


if(x <0.0){

sgn = -sgn;

x = -x;

}

long double p = floorl(log10l(x));

p--;

p = digits-p;

long double pow10 = powl(10.0,p);

返回sgn * floorl(x * pow10 + 0.5)/ pow10;

}

#include< stdio.h>

int main(无效)

{

double d = 1.7888889988996678;

long double ld = 1.7888889988996678998877L;


for(int i = 0; i< = DBL_DIG; i ++){

printf(" i =%d:%。15g \ n",i,roundto(d,i));

}

printf(" \ n 1.7888889988996678 \ n");

for(int i = 0; i< = LDBL_DIG; i ++){

printf(" i =%d:%。18Lg \ n",i,roundtol(ld,i));

}

printf (&\\; \ n 1.7888889988996678998877L \ n");

返回0;

}


---- -------------------------------------------------- --------------

我建议将它添加到FAQ中。

-

jacob navia

jacob at jacob point remcomp point fr

logiciels / informatique
http://www.cs.virginia.edu/~lcc-win32

推荐答案

jacob navia说:
jacob navia said:




"我怎样才能将数字舍入到x小数位 ?


这个问题不断出现。
Hi

"How can I round a number to x decimal places" ?

This question keeps appearing.



< snip>

<snip>


我建议将其添加到常见问题解答中。
I would propose it to add it to the FAQ.



好​​主意。你可以称之为问题14.6。


-

Richard Heathfield< http://www.cpax.org.uk>

电子邮件:-http:// www。 + rjh @

谷歌用户:< http://www.cpax.org.uk/prg/writings/googly.php>

Usenet是一个奇怪的放置" - dmr 1999年7月29日

Good idea. You could call it Question 14.6.

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999




" Richard Heathfield" < rj*@see.sig.invalidwrote in message

news:xc *************************** *** @ bt.com ...

"Richard Heathfield" <rj*@see.sig.invalidwrote in message
news:xc******************************@bt.com...

jacob navia说:
jacob navia said:

>嗨

如何将数字舍入到x小数位 ?

这个问题不断出现。
>Hi

"How can I round a number to x decimal places" ?

This question keeps appearing.



< snip>


<snip>


>我建议将其添加到常见问题解答中。
>I would propose it to add it to the FAQ.



好​​主意。你可以称之为问题14.6。


Good idea. You could call it Question 14.6.



14.6已用于如何舍入数字[到一个整数]


-

Bart

14.6 is already used by How to Round a Number [to an integer]

--
Bart


Richard Heathfield写道:
Richard Heathfield wrote:

jacob navia说:
jacob navia said:

>嗨

如何将数字舍入到x小数位 ?

这个问题不断出现。
>Hi

"How can I round a number to x decimal places" ?

This question keeps appearing.



< snip>


<snip>


>我建议将其添加到常见问题解答中。
>I would propose it to add it to the FAQ.



好​​主意。你可以称之为问题14.6。


Good idea. You could call it Question 14.6.



你的杀戮文件显然不起作用

:-)


那个问题解决了舍入到最接近的整数。

这回答了舍入到小数位数的问题


不一样的事情。但是,是的,我们可以将其添加到14.6


-

jacob navia

jacob at jacob point remcomp point fr

logiciels / informatique
http:// www.cs.virginia.edu/~lcc-win32


这篇关于舍入浮点数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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