精确计算java中float和double的精度
[本文相关的代码放在github上。地址为:https://github.com/VigourJiang/StructuredFloat]
Java中double类型的格式基本遵循IEEE 754标准。
虽然数学意义上的小数是连续的。但double只能表示当中的一些离散点。把这些离散点组成的集合记为S。S的大小还是有限的。假设要保存的小数P刚好在集合S内,那么double类型就能精确的表示P;否则double类型只能从集合S中找一个与P近期的离散点P'取代P。
以上表述对于float也成立。IEEE 754中float和double的表示策略全然同样。差别只体如今各个字段(指数字段、小数字段)的bit数量不同。
也就是说。float和double都是不精确的,因此偶尔会有一些奇怪的事情发生:
double d = 1e16d;
double d2 = d +1.0d - d;
// 下面代码输出0.0,而不是1.0
System.out.println(d2);
上述结果或许并不有趣。由于在学C语言的时候。你的老师就可能提醒过你。但有趣的是,float和double到底有多不精确?给定一个double d = XXX,与d最接近的另外两个double的数值是多少?
我用Java写了一个解析原始数据类型float和double的类StructuredFloat,给定一个float或者double数值P。它能够计算出与P的绝对值近期的、能够被float/double表示的浮点数。以下是使用方法:
double cur = 3.23d; // any valid double value
StructuredFloat sd = com.vigour.StructuredFloat.StructedFloatBuilder.buildDouble(cur);
StructuredFloat smaller = sd.absSmaller();
StructuredFloat bigger = sd.absBigger(); if(smaller != null){
double smaller_double = smaller.getBigDecimal().doubleValue();
// now you get the nearest double value whose absolute value is smaller
}
if(bigger!= null){
double bigger_double = bigger.getBigDecimal().doubleValue();
// now you get the nearest double value whose absolute value is bigger
}
以下是一些有趣的输出,能够看到,在1e16d附近double的精度就小于1了,在1e7f(一千万)附近。float的精度就等于1了,float果然难堪大用。
-------------Some Interesting Resule for Double--------------------
// 0.1d附近的double
Nearest smaller double: 0.09999999999999999167332731531132594682276248931884765625
Current double: 0.1000000000000000055511151231257827021181583404541015625
Nearest bigger double : 0.10000000000000001942890293094023945741355419158935546875 // 1.0d附近的double
Nearest smaller double: 0.99999999999999988897769753748434595763683319091796875
Current double: 1
Nearest bigger double : 1.0000000000000002220446049250313080847263336181640625 // 10.0d附近的double
Nearest smaller double: 9.9999999999999982236431605997495353221893310546875000
Current double: 10.00
Nearest bigger double : 10.0000000000000017763568394002504646778106689453125000 // 1e14d附近的double
Nearest smaller double: 99999999999999.9843750000000000000000000000000000000000000000000000
Current double: 100000000000000.00000000000000000000000000000000
Nearest bigger double : 100000000000000.0156250000000000000000000000000000000000000000000000 // 1e15d附近的double
Nearest smaller double: 999999999999999.8750000000000000000000000000000000000000000000000000
Current double: 1000000000000000.0000000000000000000000000000000000
Nearest bigger double : 1000000000000000.1250000000000000000000000000000000000000000000000000 // 1e16d附近的double
Nearest smaller double: 9999999999999998.0000000000000000000000000000000000000000000000000000
Current double: 10000000000000000.0000000000000000000000000000000000000
Nearest bigger double : 10000000000000002.0000000000000000000000000000000000000000000000000000 // 1e17d附近的double
Nearest smaller double: 99999999999999984.0000000000000000000000000000000000000000000000000000
Current double: 100000000000000000.000000000000000000000000000000000000000
Nearest bigger double : 100000000000000016.0000000000000000000000000000000000000000000000000000 // 1e304d附近的double
Nearest smaller double: 9999999999999998174371273630364736815867488735718786093662414371947263704524926751224722911637244940234972882804879769415602664816552507597839565690480126952738889402600333599657997758603312171995012866291845554976690497648524473448849371595248581587050582985041870802940253992811266476846330599148879872.0000000000000000000000000000000000000000000000000000
Current double: 9999999999999999392535525055364621860040287220117324953190771571323204563013233902843309257440507748436856118056162172578717193742636030530235798840866882774987301441682011041067710253162440905843719802548551599076639682550821832659549112269607949805346034918662572406407604380845959862074904348138143744.000000000000000000000000000000000000000000000000
Nearest bigger double : 10000000000000000610699776480364506904213085704515863812719128770699145421501541054461895603243770556638739353307444575741831722668719553462632031991253638597235713480763688482477422747721569639692426738805257643176588867453119191870248852943967318023641486852283274009874954768880653247303478097127407616.0000000000000000000000000000000000000000000000000000 -------------Some Interesting Resule for Float-------------------- // 0.1f附近的float
Nearest smaller float: 0.0999999940395355224609375
Current float: 0.100000001490116119384765625
Nearest bigger float: 0.10000000894069671630859375 // 1.0f附近的float
Nearest smaller float: 0.999999940395355224609375
Current float: 1
Nearest bigger float: 1.00000011920928955078125 // 10.0f附近的float
Nearest smaller float: 9.99999904632568359375000
Current float: 10.00
Nearest bigger float: 10.00000095367431640625000 // 1e5f附近的float
Nearest smaller float: 99999.99218750000000000000000
Current float: 100000.00000000000
Nearest bigger float: 100000.00781250000000000000000 // 1e6f附近的float
Nearest smaller float: 999999.93750000000000000000000
Current float: 1000000.0000000000000
Nearest bigger float: 1000000.06250000000000000000000 // 1e7f附近的float
Nearest smaller float: 9999999.00000000000000000000000
Current float: 10000000.0000000000000000
Nearest bigger float: 10000001.00000000000000000000000 // 1e8f附近的float
Nearest smaller float: 99999992.00000000000000000000000
Current float: 100000000.000000000000000000
Nearest bigger float: 100000008.00000000000000000000000 // 1e38f附近的float
Nearest smaller float: 99999986661652122824821048795547566080.00000000000000000000
Current float: 99999996802856924650656260769173209088.00000000000000000000000
Nearest bigger float: 100000006944061726476491472742798852096.0000000000000000000000
精确计算java中float和double的精度的更多相关文章
- Java中 float、double使用注意问题
在java中运行一下代码 System.out.println(2.00-1.10);输出的结果是:0.8999999999999999很奇怪,并不是我们想要的值0.9 再运行如下代码:System. ...
- Java中float和double转换的问题
为什么double转float不会出现数据误差,而float转double却误差如此之大? double d = 3.14; float f = (float)d; System.out.prin ...
- Java中float、double、long类型变量赋值添加f、d、L尾缀问题
展开1. 添加尾缀说明 我们知道Java在变量赋值的时候,其中float.double.long数据类型变量,需要在赋值直接量后面分别添加f或F.d或D.l或L尾缀来说明. 其中,long类型最好以 ...
- JAVA中float与double的区别
float是单精度类型,精度是8位有效数字,取值范围是10的-38次方到10的38次方,float占用4个字节的存储空间 double是双精度类型,精度是17位有效数字,取值范围是10的-308次方到 ...
- java中float和double的区别
float表示单精度浮点数在机内占4个字节,用32位二进制描述. double表示双精度浮点数在机内占8个字节,用64位二进制描述.浮点数在机内用指数型式表示,分解为:数符,尾数,指数符,指数四部分. ...
- 【转】JAVA程序中Float和Double精度丢失问题
原文网址:http://blog.sina.com.cn/s/blog_827d041701017ctm.html 问题提出:12.0f-11.9f=0.10000038,"减不尽" ...
- Java中float/double取值范围与精度
Java浮点数 浮点数结构 要说清楚Java浮点数的取值范围与其精度,必须先了解浮点数的表示方法,浮点数的结构组成,之所以会有这种所谓的结构,是因为机器只认识01,你想表示小数,你要机器认识小数点这个 ...
- 【转载】 C#中float、double以及decimal类型有何不同
在C#语言中,float.double以及decimal类型都可以用来表示小数,但三者还是有一定的不同,有效数字为相比的话,decimal类型的有效数字最大,float类型最小.计算浮点类型的运算,如 ...
- Java中long和double的原子性
Java中long和double的原子性 java中基本类型中,long和double的长度都是8个字节,32位(4字节)处理器对其读写操作无法一次完成,那么,JVM,long和double是原子性的 ...
随机推荐
- Python学习日记之文件读取操作
Python内置了文件读写的函数open,read 用法示例: open('/home/root/files.txt ') 在打开文件后,操作完成后可以使用close()关闭文件,但比较好的文件读写方 ...
- Spartan6系列之芯片配置模式详解
1. 配置概述 Spartan6系列FPGA通过把应用程序数据导入芯片内部存储器完成芯片的配置.Spart-6 FPGA可以自己从外部非易失性存储器导入编程数据,或者通过外界的微处理器.DSP等对 ...
- Learning Face Age Progression: A Pyramid Architecture of GANs
前言 作为IP模式识别的CNN初始模型是作为单纯判别式-模式识别存在的,并以此为基本模型扩展到各个方向.基本功能为图像判别模型,此后基于Loc+CNN的检测模型-分离式.end2end.以及MaskC ...
- 00-IT人士必去的10个网站
IT人士必去的10个网站 1.Chinaunix 网址:http://www.chinaunix.net/ 简介:中国最大的linux/unix技术社区. 2.ITPub 网址:http://www. ...
- 并发编程学习笔记(5)----AbstractQueuedSynchronizer(AQS)原理及使用
(一)什么是AQS? 阅读java文档可以知道,AbstractQueuedSynchronizer是实现依赖于先进先出 (FIFO) 等待队列的阻塞锁和相关同步器(信号量.事件,等等)提供一个框架, ...
- Windows下编译64位GSL
GSL (GNU Scientific Library, http://www.gnu.org/software/gsl/)官方并没有提供编译好的Windows版本.首先要保证Windows是64位的 ...
- vue项目国际化实现 vue-i18n使用详细教程
1.安装vue-i18n: npm i vue-i18n -S 当然你也可以这样: <script src="https://unpkg.com/vue/dist/vue.js&quo ...
- PhpStorm 本地管理提交码云和GitHub代码仓库
参考地址:https://www.yflad.cn/1766.html 1:下载Git客户端 2:打开PhpStorm,设置,Version Control → Git.配置git执行文件的路径.gi ...
- layer实现窗口抖动效果
function showMsg(msg, icon){ layer.msg(msg, { //1:正确:2:错误:3:询问:4:锁定:5:失败:6:成功:7:警告:16:加载 icon : icon ...
- [angular1.6]Error: "transition superseded" ui-router 在angular1.6 报错误问题解决
在angular1.6版本里,使用ui-router如果报这个错误,可以将ui-router升级到最近版本即可.ui-router version v0.4.2