先看一段代码实验:

#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转换中存在的精度损失问题的更多相关文章

  1. Android中 int,float,Double,String 互相转换

    1 如何将字串 String 转换成整数 int?  A. 有两个方法: 1). int i = Integer.parseInt([String]); 或 i = Integer.parseInt( ...

  2. C++11中int,float,double与string的转化

    在C++11中可以使用std::to_string()函数将数值转换为string格式,十分方便. 以下部分来选自cplusplus.com. std::to_string string to_str ...

  3. QT中QString 与 int float double 等类型的相互转换

    Qt中 int ,float ,double转换为QString 有两种方法 1.使用 QString::number(); 如: long a = 63; QString s = QString:: ...

  4. java中int,float,long,double取值范围,内存泄露

    java中int,float,long,double取值范围是多少? 写道 public class TestOutOfBound { public static void main(String[] ...

  5. MySQL中 DECIMAL FLOAT DOUBLE的区别

    第一篇文章: MySQL中Decimal类型和Float Double等区别 MySQL中存在float,double等非标准数据类型,也有decimal这种标准数据类型. 其区别在于,float,d ...

  6. C++中将string类型转换为int, float, double类型 主要通过以下几种方式:

      C++中将string类型转换为int, float, double类型 主要通过以下几种方式: # 方法一: 使用stringstream stringstream在int或float类型转换为 ...

  7. [C++] string与int, float, double相互转换

    参考:http://blog.csdn.net/candadition/article/details/7342380 将string类型转换为int, float, double类型 主要通过以下几 ...

  8. C 语言实例 - 计算 int, float, double 和 char 字节大小

    C 语言实例 - 计算 int, float, double 和 char 字节大小 C 语言实例 C 语言实例 使用 sizeof 操作符计算int, float, double 和 char四种变 ...

  9. [转]不优雅的方式处理 xlrd 中 int/float 的问题

    原址:http://blog.chedushi.com/archives/7258 最近在用 xlrd 写一个题库自动导出的程序,但碰到一个比较 ugly 的问题. 程序要求是将 xls 文件中的数据 ...

随机推荐

  1. java 入门 第二季3

    1.继承 1.java是单继承的,一个子类只有一个父类 父类,基类 子类,派生类 2.继承的好处:子类拥有父类的所有属性和方法,属性的修饰符不能是private 3.语法规则: class 子类 ex ...

  2. 实现Windows Phone 8中ListBox的分页加载

    功能就是ListBox滚动到最下方的时候,能够自动加载下一页的内容. 解决问题的关键就是如何判断ListBox已经加载到了最底部. 网上找了两个解决方法: 1 http://googlers.itey ...

  3. 【leetcode】Integer to Roman & Roman to Integer(easy)

    Roman to Integer Given a roman numeral, convert it to an integer. Input is guaranteed to be within t ...

  4. IOS - AFN

    #import "ViewController.h"#import "AFNetworking.h"#import "SSZipArchive.h&q ...

  5. [Android Pro] Service (startservice , bindservice , unbindservice, stopService)

    1: startService -------stopService (this will call onDestroy) 2: bindService -------unbindService    ...

  6. MAC下GitHub命令操作

    由于GitHub实在太有用了~~ ,各种源代码,开源工程,经常需要下载下来使用和学习,或者自己的代码需要上传之类的,尽管有"GitHub for Mac"工具,但是作为一名程序猿! ...

  7. 创建Oracle10g ASM数据库

    概述      在真正的海量数据库环境中,DBA可能会花费很多的时间来作磁盘管理,比如一个表空间将占满整个磁盘,DBA就需要再添加一块磁盘到操作系统中,然后再在新的磁盘上创建新的数据文件,如果是单个磁 ...

  8. 关于快捷键 Ctrl+Alt+[方向键] 的知识

    在用PS作图时使用 Ctrl+Alt+[方向键]  组合建时屏幕莫名翻转, 平时电脑懒得维护所以略卡,我不会说一般早上起床摁了开机去上完厕所回来还--咳咳 刚按下时瞬间一黑,再黑,,继续黑--真是大吃 ...

  9. TIME_WAIT连接过多解决办法

    问题起因: 自己开发了一个服务器和客户端,通过短连接的方式来进行通讯,由于过于频繁的创建连接,导致系统连接数量被占用,不能及时释放.看了一下18888,当时吓到了. 现象: 1.外部机器不能正常连接S ...

  10. jq 全选和反选以及判断那条被选中

    <body><div><input type="checkbox" id="a" />全选</div><d ...