Code:

class Main {

public static void main (String[] args) {

System.out.print("float: ");

System.out.println(1.35f-0.00026f);

System.out.print("double: ");

System.out.println(1.35-0.00026);

}

}

Output:

float: 1.34974

double: 1.3497400000000002

??? float got the right answer, but double is adding extra stuff from no where, Why??

Isn't double supposed to be more precise than float?

解决方案

A float is 4 bytes wide, whereas a double is 8 bytes wide.

Surely the double has more precision so it has slightly less rounding error.

Squeezing infinitely many real numbers into a finite number of bits

requires an approximate representation. Although there are infinitely

many integers, in most programs the result of integer computations can

be stored in 32 bits. In contrast, given any fixed number of bits,

most calculations with real numbers will produce quantities that

cannot be exactly represented using that many bits. Therefore the

result of a floating-point calculation must often be rounded in order

to fit back into its finite representation. This rounding error is the

characteristic feature of floating-point computation.

On a side note:-

I would suggest if you want the exact decimal values then use java.math.BigDecimal

java double精确比较,Java float比double更精确?相关推荐

  1. java double 赋值语句_Java中float、double、long类型变量赋值添加f、d、L尾缀问题

    展开 1. 添加尾缀说明 我们知道Java在变量赋值的时候,其中float.double.long数据类型变量,需要在赋值直接量后面分别添加f或F.d或D.l或L尾缀来说明. 其中,long类型最好以 ...

  2. Java 分数的实现 | 将float或double转换为分数

    最近想要做到一个数学计算的项目,其中需要把float或double转换为分数(a/b)的表达形式,但苦于网上没有合适的教程,于是自己研究了float和double的数据类型,结果成功,决定和大家分享代 ...

  3. mysql中的double类型_MySQL中float、double、decimal三个浮点类型的区别与总结!

    作者:极客小俊 一个专注于web技术的80后 我不用拼过聪明人,我只需要拼过那些懒人 我就一定会超越大部分人! CSDN@极客小俊,原创文章, B站技术分享 个人博客: cnblogs.com 前端h ...

  4. python double转int_int、float、double数据类型之间转换的原则

    C语言提供了两种不同的浮点数据:float 和 double,即单精度和双精度浮点. 当在int(假设int是32位的).float和double格式之间进行强制类型转换时,原则如下: 从 int 转 ...

  5. linux中double大小,linux 下 float 和 double 精度计算差别

    今天在根据需求写代码时候,偶尔发现linux 下 设置变量类型 float 和double 计算时, 得到的结果是不一样的. 要求: 设定值 = 传入值 * 10 * 122.88 /1000; ca ...

  6. c语言double存储方式,C - float和double在内存存储解析

    float和double在内存存储解析 C语言中,对于浮点类型的数据采用单精度类型(float)和双精度类型(double)来存储. float数据占用32bit,double数据占用64bit. 我 ...

  7. Java String转int、float、double

    int i; String s1="134"; i=Integer.parseInt(s1); 如果是float可以用强制类型转换. Float.parseFloat(); Dou ...

  8. mysql:列类型之float、double

    环境: window10 vs2022 .net 6 mysql 8.0.25 DBeaver 参考: <MSDN:浮点数值类型(C# 引用)> <mysql:11.1.4 Floa ...

  9. Java 命名规范---阿里Java开发手册

    0)Service/DAO层方法命名规约 1)获取单个对象的方法用get做前缀. 2)获取多个对象的方法用list做前缀. 3)获取统计值的方法用count做前缀. 4)插入的方法用save(推荐)或 ...

  10. 阿里巴巴Java开发手册 (Alibaba Java Coding Guidelines)

    参考资料: 阿里巴巴Java开发手册  https://www.cntofu.com/book/78/index.html 一.编程规约 ##(一)命名风格 [强制]代码中的命名均不能以下划线或美元符 ...

最新文章

  1. Vue 中多个元素、组件的过渡,及列表过渡
  2. oracle 11g rac asm磁盘组增加硬盘
  3. 皇牌空战无限服务器,《皇牌空战:无限》正式停服 一个搏击长空的时代终结[多图]...
  4. linux 磁盘资源管理以及IO
  5. cvMorphology形态学原理解析及源码分析
  6. 为什么 SQL 正在击败 NoSQL,这对未来的数据意味着什么?
  7. oracle java 并发_【转】JAVA并发教程(ORACLE官网资料)
  8. qt显示rgba8888 如何改 frame_Qt开源作品17-IP地址输入控件
  9. 转载文章:Microsoft 将僵尸网络威胁智能分析程序引入云中以提供近实时数据
  10. [www.infoshare.cc]【uiautomator】报告简介及查看
  11. order by case when then end
  12. Keli 编译遇到 *** FATAL ERROR L250: CODE SIZE LIMIT IN RESTRICTED VERSION EXCEEDED且已经破解
  13. HCNA-Cloud云计算认证
  14. lumion自动保存_19条新人必看的Lumion良好操作习惯
  15. nodejs解压文件
  16. 零基础能不能学计算机专业,零基础能学计算机专业吗?
  17. 微带线和带状线的SIEMC分析以及对DDR等长设置的影响
  18. 阿里云服务器安装mongodb
  19. excel怎么设置打印区域_用offset函数设置打印区域(有新增内容时自动扩展)
  20. nginx可以用来做什么

热门文章

  1. 增益比值 dB 以及 dBw-dBmv 等之详解
  2. electron sqlite3_electron集成sqlite3,win10上折腾了2天
  3. mysql执行系统命令_mysql 命令行执行 sql
  4. 几道题帮你搞定数据选择器
  5. C++一天一个程序(一)
  6. redis集群scan_Redis scan命令的一次坑
  7. sqoop 导入到hive字段全是null_Sqoop 一点通
  8. java取网页源码_Java获取任意http网页源代码的方法
  9. mysql两台服务器怎么做数据同步_两台mysql服务器实现双机互备配置并测试数据同步...
  10. NOIP模拟测试18「引子·可爱宝贝精灵·相互再归的鹅妈妈」