关于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 文件中的数据 ...
随机推荐
- mybatis跨XML引用
resultMap中association标签的select属性使用嵌套查询的时候需要引用其它xml文件的配置 此时可以用要引用xml的namespace.引用select的ID 如 <resu ...
- MPAndroidChart 教程
以前没用过MPAndroidChart,为了方便学习查找,引用下别个大神的笔记. 其余文章索引: MPAndroidChart 教程:概述MPAndroidChart 教程:开始 Getting St ...
- 【leetcode】triangle(easy)
Given a triangle, find the minimum path sum from top to bottom. Each step you may move to adjacent n ...
- [Python] dir() 与 __dict__,__slots__ 的区别
首先需要知道的是,dir() 是 Python 提供的一个 API 函数,dir() 函数会自动寻找一个对象的所有属性,包括搜索 __dict__ 中列出的属性. 不是所有的对象都有 __dict__ ...
- Java反射实战
一.背景 最近的项目中需要使用到Java 反射的知识,以前不怎么了解,也基本没怎么用过,抽出一片时间,来具体学习和实战下Java的反射!拿来和大家分享以及记录方便以后学习! 二.反射相关概念解析 1. ...
- FTL标签
<#if blockObject ??> <#else> </if>判断对象是否存在 <#if componentid ?? &&compon ...
- 借教室(codevs 1217)
1217 借教室 2012年NOIP全国联赛提高组 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 钻石 Diamond 题解 查看运行结果 题目描述 Descrip ...
- Java动态代理一Proxy
什么是动态代理? 动态代理可以提供对另一个对象的访问,同时隐藏实际对象的具体事实.代理一般会实现它所表示的实际对象的接口.代理可以访问实际对象,但是延迟实现实际对象的部分功能,实际对象实现系统的实际功 ...
- HTML5 – 3.加强版ol
<ol> 标签定义了一个有序列表. 列表排序以数字来显示. 使用<li> 标签来定义列表选项. 提示和注释 提示: 如果需要无序列表,请使用 <ul> 标签. 提示 ...
- Myeclipse的web工程和Eclipse互相转换
eclipse的web工程转myeclipse的web工程1.原eclipse工程叫netschool 2.在myeclipse中新建一个工程叫netschool 并在新建的时修改 web root ...