关于c中 int, float, double转换中存在的精度损失问题

先看一段代码实验:

?





1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

#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;

   

 <span style="color: rgb(255, 0, 0);"> <strong> double
d = 0.6L; // change this value  to 0.5L, you will see different result

</strong></span>   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

时间: 2024-08-05 15:13:25

关于c中 int, float, double转换中存在的精度损失问题的相关文章

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

java中int,float,long,double取值范围是多少? 写道 public class TestOutOfBound { public static void main(String[] args) { System.out.println(Integer.MAX_VALUE-(-Integer.MAX_VALUE)); //内存溢出System.out.println(Integer.MAX_VALUE); //2的31次方-1,10个数位,正的20亿左右,用在钱上面不一定够Sy

MySQL中 DECIMAL FLOAT DOUBLE的区别

第一篇文章: MySQL中Decimal类型和Float Double等区别 MySQL中存在float,double等非标准数据类型,也有decimal这种标准数据类型. 其区别在于,float,double等非标准类型,在DB中保存的是近似值,而Decimal则以字符串的形式保存数值.float,double类型是可以存浮点数(即小数类型),但是float有个坏处,当你给定的数据是整数的时候,那么它就以整数给你处理.这样我们在存取货币值的时候自然遇到问题,我的default值为:0.00而实

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

参考:http://blog.csdn.net/candadition/article/details/7342380 将string类型转换为int, float, double类型 主要通过以下几种方式: # 方法一: 使用stringstream stringstream在int或float类型转换为string类型的方法中已经介绍过, 这里也能用作将string类型转换为常用的数值类型. Demo: #include <iostream> #include <sstream>

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

使用 sizeof 操作符计算int, float, double 和 char四种变量字节大小. sizeof 是 C 语言的一种单目操作符,如C语言的其他操作符++.--等,它并不是函数. sizeof 操作符以字节形式给出了其操作数的存储大小. #include <stdio.h>int main(){    int integerType;    float floatType;    double doubleType;    char charType;    // sizeof

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

原址:http://blog.chedushi.com/archives/7258 最近在用 xlrd 写一个题库自动导出的程序,但碰到一个比较 ugly 的问题. 程序要求是将 xls 文件中的数据导出成文本,但 xlrd 在读取数据时,会将 xls 单元格中所有可能是数字的数据都自动转换成 python 的 float.这时候,我们通过 str(cell.value) 会得到 12.0 (假设 cell.value = 12.0). 我明明需要字符串 12 却给我 12.0,太 ugly 了

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

# 方法一: 使用stringstream stringstream在int或float类型转换为string类型的方法中已经介绍过, 这里也能用作将string类型转换为常用的数值类型. Demo: [cpp] view plaincopy #include <iostream> #include <sstream>    //使用stringstream需要引入这个头文件 using namespace std; //模板函数:将string类型变量转换为常用的数值类型(此方法

解决java float double 浮点型参与计算失精度

本人前段时间做一个社区电商应用,发现了一个 天坑   ...................让我哭会 . 下面听听我的踩坑之路吧 ,电商肯定跟¥打交道了,计算少不了的.由于本人太菜 单纯的以为  float  double 可以直接参与加减乘除 感觉这种代码万无一失没去想这么多直接 float*int . 之前简单测试没问题,项目上线后自己偶然才发现的(不知道公司测试干嘛的) ,当选择多件商品时 , 还是直接说现象吧 比如 在代码中    0.1 f  * A         A为int 型  

数据中 int 转 double 方式

在mysql 中,得出一个int整数型数值 int整数值/int整数值   在被引用时,发现还是int类型 但是实际需要转换为 double 小数类型 查看相关函数,没有找到好的方法 后采用了 round 函数 Round函数返回一个数值,该数值是按照指定的小数位数进行四舍五入运算的结果 原文地址:https://www.cnblogs.com/Alanf/p/9235684.html

Day1 (let and var)/(String,Int,Float,Double)/(Tuples,contains: array,dictionary)

let secondSystem:Int = 0b10001 let eightSystem:Int  = 0o21 let tenSystem:Int    = 17 let sixthSystem:Int  = 0x11 // summary: 1.u need insert a "Enter" behind equal symbol let num_a:Float = 1 let num_b:Int = 1 let a:Int = 3 let b:Double = 0.14159