关于c中 int, float, double转换中存在的精度损失问题
先看一段代码实验:
#include<limits>
#include<iostream> using namespace std; int main()
{
unsigned int i = numeric_limits<unsigned int >::max();
float f = i;
unsigned int j = (unsigned int )f;
bool flag 1 = i==j;
cout<<"i = "<<endl;
cout<<"j = "<<endl;
cout<<"flag1 = " <<flag1<<endl; double d = 0.6L; // change this value to 0.5L, you will see different result
float e = (float)d;
double d2 = e;
bool flag2 = d==d2;
cout<<"d2: "<<d2<<endl;
cout<<"d: "<<d<<endl;
cout<<"flag2: "<<flag2<<endl; }
从这个例子中可以看出flag1和flag2均为flase,且虽然d2和d在输出的时候虽然看上去一致,但实际并不相等;而i与j的实际值就已经查了很远。具体原因参见参考文献[1]。
深层原理分析还需进一步学习中。 。。。。待补充。
Reference
[1]int, float, double之间不得不说的故事, http://www.cnblogs.com/wodehuajianrui/archive/2009/03/18/1415173.html
关于c中 int, float, double转换中存在的精度损失问题的更多相关文章
- Android中 int,float,Double,String 互相转换
1 如何将字串 String 转换成整数 int? A. 有两个方法: 1). int i = Integer.parseInt([String]); 或 i = Integer.parseInt( ...
- C++11中int,float,double与string的转化
在C++11中可以使用std::to_string()函数将数值转换为string格式,十分方便. 以下部分来选自cplusplus.com. std::to_string string to_str ...
- QT中QString 与 int float double 等类型的相互转换
Qt中 int ,float ,double转换为QString 有两种方法 1.使用 QString::number(); 如: long a = 63; QString s = QString:: ...
- java中int,float,long,double取值范围,内存泄露
java中int,float,long,double取值范围是多少? 写道 public class TestOutOfBound { public static void main(String[] ...
- MySQL中 DECIMAL FLOAT DOUBLE的区别
第一篇文章: MySQL中Decimal类型和Float Double等区别 MySQL中存在float,double等非标准数据类型,也有decimal这种标准数据类型. 其区别在于,float,d ...
- C++中将string类型转换为int, float, double类型 主要通过以下几种方式:
C++中将string类型转换为int, float, double类型 主要通过以下几种方式: # 方法一: 使用stringstream stringstream在int或float类型转换为 ...
- [C++] string与int, float, double相互转换
参考:http://blog.csdn.net/candadition/article/details/7342380 将string类型转换为int, float, double类型 主要通过以下几 ...
- C 语言实例 - 计算 int, float, double 和 char 字节大小
C 语言实例 - 计算 int, float, double 和 char 字节大小 C 语言实例 C 语言实例 使用 sizeof 操作符计算int, float, double 和 char四种变 ...
- [转]不优雅的方式处理 xlrd 中 int/float 的问题
原址:http://blog.chedushi.com/archives/7258 最近在用 xlrd 写一个题库自动导出的程序,但碰到一个比较 ugly 的问题. 程序要求是将 xls 文件中的数据 ...
随机推荐
- Python 开发轻量级爬虫04
Python 开发轻量级爬虫 (imooc总结04--url管理器) 介绍抓取URL管理器 url管理器用来管理待抓取url集合和已抓取url集合. 这里有一个问题,遇到一个url,我们就抓取它的内容 ...
- 在Win7下使用sphinx-build建立开源软件文档
最近想看看odoo的使用文档,在线看不方便,而且还没有提供离线文档下载,由于是开源项目,此项目托管在Github上,于是就有了想通过Github把文档git下来,可是git下来的文档是.rst文件,无 ...
- ASIHTTPRequest详解 [经典]
ASIHTTPRequest 对CFNetwork API进行了封装,并且使用起来非常简单,用Objective-C编写,可以很好的应用在Mac OS X系统和iOS平台的应用程序中.ASIHTTPR ...
- CityEngine基于规则贴图的实现技巧
转自:http://blog.sina.com.cn/s/blog_841eeb5201010p3e.html CityEngine在贴图的实现过程有两种方式:第一种是通过i(geometryPath ...
- 第四章 面向对象与IO操作
一.类(类中可以写字段.属性.方法.构造函数)1.定义一个类用关键字class,后面加类名,类名第一个字母用大写,可用private或public修饰符定义访问级别,类可定义在同一命名空间中,也可定义 ...
- iOS 文件大小转换成 KB、MB、GB 。。。
-(NSString *) convertFileSize:(long long)size { ; ; ; if (size >= gb) { return [NSString stringWi ...
- Xcode常用代码块
Xcode的代码片段(Code Snippets)创建自定义的代码片段,当你重用这些代码片段时,会给你带来很大的方便. 常用的: 1.strong:@property (nonatomic,stron ...
- JavaScript基础——处理字符串
String对象是迄今为止在JavaScript中最常用的对象.在你定义一个字符串数据类型的变量的任何时候,JavaScript就自定为你创建一个String对象.例如: var myStr = &q ...
- mysqli扩展库的预处理技术 mysqli stmt
//预编译演示 //1,创建mysqli对象 $mysqli=new mysqli("localhost","root",""," ...
- Loadrunner11.0 录制手机App脚本的方法
使用Loadrunner录制手机终端App脚本 1. 说明 目前手机APP上的功能日益丰富,对手机应用功能的性能测试需求也越来越多.公司比较抠门没有花钱买Loadrunner,可怜我们工作中一直用的破 ...