什么与cmath的四舍五入?! [英] What's up with rounding in cmath?!

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

问题描述

我无法编译以下行。


int a = round(2.6);


当然,我有包括< cmathand我可以去地板和


天花板没有问题。然而,四舍五入似乎并没有完全发挥作用。编译器咆哮说:


错误3错误C3861:''round'':找不到标识符


这是'这个故事的结尾。我可以(并且很可能是

)只需添加0.5和地板 - 如果结果但是

那是'错了,所以错了......

可能是什么原因?补救措施?


-

V?nligen Kerstin Viltersten

(酷长颈鹿)

解决方案

2007-05-11 23:01,The Cool Giraffe写道:


我无法制作以下行编译。


int a = round(2.6);


我当然包括< cmathand我可以去地板并且


上限没有问题。然而,四舍五入似乎并没有完全发挥作用。编译器咆哮说:


错误3错误C3861:''round'':找不到标识符


这是'这个故事的结尾。我可以(并且很可能是

)只需添加0.5和地板 - 如果结果但是

那是'错了,所以错了......

可能是什么原因?补救措施?



快速查看标准给出的答案是没有圆()

函数。解决方法是获得符合C99标准的库并包括

< math.h(圆形()首先包含在C99中)。


-

Erik Wikstr?m


5月11日下午5:01,The Cool Giraffe < giraf ... @ viltersten.com>

写道:


我无法编译以下行。


int a = round(2.6);


我当然包括< cmathand我可以去地板和

上限没有问题。然而,四舍五入似乎并没有完全发挥作用。编译器咆哮说:


错误3错误C3861:''round'':找不到标识符


这是'这个故事的结尾。我可以(并且很可能是

)只需添加0.5和地板 - 如果结果但是

那是'错了,所以错了......

可能是什么原因?补救措施?


-

V?nligen Kerstin Viltersten

(酷长颈鹿)


round()没有返回int,虽然这不是你的问题。


#include< iostream>

#include< cmath>


int main()

{

double result = round(2.6);

std :: cout<< " result =" <<结果<< std :: endl;

}


如果你真的需要一个int,请使用static_cast< int>()


Erik Wikstr?m写道:


2007-05 -11 23:01,The Cool Giraffe写道:


>我无法编译以下行。

int a = round (2.6);

我当然包括< cmathand我可以去地板和天花板没有问题。但是,四舍五入似乎根本不起作用。编译器咆哮说:

错误3错误C3861:''round'':找不到标识符

这就是这个故事的结尾。我可以(而且很可能是
会)简单地添加0.5和地板 - 如果结果但是
这是错的,所以错了......

什么可以原因?补救措施?



快速查看标准会给出答案,即没有圆形()

函数。解决方法是获得符合C99标准的库并包括

< math.h(圆形()首先包含在C99中)。



如果您的编译器的C模式使用C99库,您可能只需将标题更改为< math.h>

即可。


-

Ian Collins。


I have trouble making the following line compile.

int a = round (2.6);

I have, of course, included <cmathand i can go flooring and

ceiling with no problems. However, the rounding seems not to

be working at all. The compiler barks out that:

Error 3 error C3861: ''round'': identifier not found

and that''s the end of this story. I can (and most likely
will) simply add 0.5 and the floor-ify the result but
that''s just wrong, so wrong...

What can be the reason? The remedy?

--
V?nligen Kerstin Viltersten
(The Cool Giraffe)

解决方案

On 2007-05-11 23:01, The Cool Giraffe wrote:

I have trouble making the following line compile.

int a = round (2.6);

I have, of course, included <cmathand i can go flooring and

ceiling with no problems. However, the rounding seems not to

be working at all. The compiler barks out that:

Error 3 error C3861: ''round'': identifier not found

and that''s the end of this story. I can (and most likely
will) simply add 0.5 and the floor-ify the result but
that''s just wrong, so wrong...

What can be the reason? The remedy?

A quick look in the standard gives that answer that there is no round()
function. The remedy is to get a C99 compliant library and include
<math.h(round() was included first in C99).

--
Erik Wikstr?m


On May 11, 5:01 pm, "The Cool Giraffe" <giraf...@viltersten.com>
wrote:

I have trouble making the following line compile.

int a = round (2.6);

I have, of course, included <cmathand i can go flooring and

ceiling with no problems. However, the rounding seems not to

be working at all. The compiler barks out that:

Error 3 error C3861: ''round'': identifier not found

and that''s the end of this story. I can (and most likely
will) simply add 0.5 and the floor-ify the result but
that''s just wrong, so wrong...

What can be the reason? The remedy?

--
V?nligen Kerstin Viltersten
(The Cool Giraffe)

round() doesn''t return an int, although thats not your problem.

#include <iostream>
#include <cmath>

int main()
{
double result = round( 2.6 );
std::cout << "result = " << result << std::endl;
}

if you really need an int, use a static_cast< int >( )


Erik Wikstr?m wrote:

On 2007-05-11 23:01, The Cool Giraffe wrote:

>I have trouble making the following line compile.

int a = round (2.6);

I have, of course, included <cmathand i can go flooring and

ceiling with no problems. However, the rounding seems not to

be working at all. The compiler barks out that:

Error 3 error C3861: ''round'': identifier not found

and that''s the end of this story. I can (and most likely
will) simply add 0.5 and the floor-ify the result but
that''s just wrong, so wrong...

What can be the reason? The remedy?


A quick look in the standard gives that answer that there is no round()
function. The remedy is to get a C99 compliant library and include
<math.h(round() was included first in C99).

You might be able to get away with just changing the header to <math.h>
if your compiler''s C mode uses the C99 library.

--
Ian Collins.


这篇关于什么与cmath的四舍五入?!的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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