小编abb*_*bas的帖子

6
推荐指数
1
解决办法
1595
查看次数

将C++中的浮点数舍入到一个小数位

我在c ++中有一个浮点数,数字可以是不同的形式,例如355.5或9.9(这是测试代码的输入).

我有一个功能

float return_max(angle_set_t *angles)  
{  
    float val;  
    float max;  
    max= angles->key;  
    while( angles != NULL )  
    {  
        val= angles->key;  
        if(max<=val)  
        {  
            max=val;  
        }  
        angles = angles->right;  
    }       
    return max;
}  
Run Code Online (Sandbox Code Playgroud)

max可以是浮点值.我想将值舍入到一个小数位.

我需要一个通用的解决方案,因此适用于355.555555和9.999999

float first_aset()
{
    //do somethig
    result=return_max();
    return result;
}

void main()
{
    if( first_aset(S, 357.0, 20.0 ) != 9.9 ||
        first_aset(T, 357.0, 20.0 ) != 9.9 )
    {
         printf("Error in wrap-around interval (3)\n");
         printf(" first in interval [357, 20) in S is %f, should be …
Run Code Online (Sandbox Code Playgroud)

c++ floating-point

2
推荐指数
2
解决办法
2万
查看次数

标签 统计

.net ×1

c++ ×1

code-comments ×1

floating-point ×1

performance ×1