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

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

  hot3.png

###GCC中printf四舍五入的原则

  • 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://my.oschina.net/aquar/blog/731999

你可能感兴趣的文章
Server系列7:看win2012时代如何强制还原记录数据
查看>>
Linux下查看文件和文件夹大小 du df
查看>>
mongodb数据备份与恢复
查看>>
elf文件解析(cpp版)
查看>>
使用VS2010编译MongoDB C++驱动详解
查看>>
负载均衡(Load Balancing)学习笔记(三)
查看>>
Swing系统中实现帮助文档方法
查看>>
jquery设置和获得checkbox选中问题
查看>>
MySQL修改root密码的各种方法整理
查看>>
少女时代擦玻璃屏保
查看>>
我试试
查看>>
vi 命令 用法
查看>>
星际争霸1的AI设计思路:以人族开局为例
查看>>
我的友情链接
查看>>
查看系统内存 cpu占用率脚本
查看>>
我的友情链接
查看>>
python实例练习-01登录
查看>>
awk编程
查看>>
H3C SR6604 升级IOS
查看>>
Linux 进程与服务管理1
查看>>