博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
GCC中printf四舍五入的原则
阅读量:5037 次
发布时间:2019-06-12

本文共 1513 字,大约阅读时间需要 5 分钟。

 

  • VC++ is using 
  • GCC is using  which is also known as banker's rounding.

  •  In computer science, the nearest integer function of real number x denoted variously by Round(x), is a function which returns the nearest integer to x. To avoid ambiguity when operating on half-integers, a rounding rule must be chosen. On most computer implementations, the selected rule is to round half-integers to the nearest even integer—for example,

Test Program on gcc version 5.4.0 20160609 (Ubuntu 5.4.0-6ubuntu1~16.04.2)

#include 
#include
#include
#include
using namespace std;void printVector(float& elem){ printf("value %f is change to %0.1f \n", elem, elem);}void testRoundRules(){ cout << "using gcc" << endl; float fValues[] = { 3.05f, 3.15f, 3.151f, 3.155f, 3.25f, 3.251f, 3.255f,-0.45f, -0.15f}; // 使用vector的构造函数,将数组第一个元素开始到结束的内容拷贝到vector中 vector
vDatas(fValues, fValues+sizeof(fValues)/sizeof(fValues[0])); for_each(vDatas.begin(), vDatas.end(), printVector);}

  

output:

using gcc

value 3.050000 is change to 3.0

value 3.150000 is change to 3.2
value 3.151000 is change to 3.2
value 3.155000 is change to 3.2
value 3.250000 is change to 3.2
value 3.251000 is change to 3.3
value 3.255000 is change to 3.3
value -0.450000 is change to -0.4
value -0.150000 is change to -0.2

Note that 3.251 is rounding to 3.3 not 3.2, because 3.3 is nearer to 3.251.

转载于:https://www.cnblogs.com/aquar/p/5767000.html

你可能感兴趣的文章
vSphere中Storage vMotion的流程详解
查看>>
Docker-Mysql-proxy Mysql Proxy实现读写分离
查看>>
mysql 的基本使用命令
查看>>
字符串排序之一
查看>>
判断三角形类型方法的单元测试
查看>>
C++学习笔记51:排序
查看>>
spring.factories
查看>>
php使用amqplib方式使用rabbitmq
查看>>
打印控件
查看>>
技术团队的目标管理
查看>>
gitlab的介绍
查看>>
利用logging.basicConfig生成文件--中文乱码解决方法
查看>>
matlab新手入门(二)(翻译)
查看>>
Python 编码规范 PEP8
查看>>
mac os 下安装 nmap网络扫描和嗅探工具包
查看>>
python 迷宫问题
查看>>
Ubuntu 14.04 源
查看>>
android界面开发那点事
查看>>
js事件基础
查看>>
玩转CPU Topology
查看>>