I just came across this website and tried doing that in Java and C++. Why writing the following in Java gives 0.30000000000000004

double x = 0.1 + 0.2;

System.out.print(x);

Whereas writing the following in C++ gives 0.3?

double x = 0.1 + 0.2;

cout<

解决方案

There is no guarantee from the C++ standard that IEEE 754 floating point arithmetic is used, so the result is actually implementation defined. However, most implementations will do it.

In Java, float and double are defined to be IEEE 754 floating point types. In addition, you can add the strictfp modifier to a class or method declaration to require strict IEEE 754 floating point arithmetic be used even for intermediary results.

When dealing with floating point numbers, in case of doubt, it is often useful to look at the actual bit representation.

#include

#include

int

main()

{

static_assert(sizeof(double) == sizeof(uint64_t), "wrong bit sizes");

const double x = 0.1 + 0.2;

const uint64_t bits = *reinterpret_cast(&x);

printf("C++: 0x%016lX\n", bits);

return 0;

}

public final class Main {

public static void main(final String[] args) {

final double x = 0.1 + 0.2;

final long bits = Double.doubleToLongBits(x);

System.out.printf("Java: 0x%016X\n", bits);

}

}

When I execute both programs on my computer (GNU/Linux with GCC and OpenJDK), the output is

C++: 0x3FD3333333333334

Java: 0x3FD3333333333334

which shows that both yield the exact same result. However, a portable program should not rely on this.

JAVA和C浮点数,为什么在C ++和Java中使用float函数会产生不同的结果?相关推荐

  1. java怎么进行浮点数运算_怎样在Java中实现浮点数的精确运算?

    package com.lv; import java.math.BigDecimal; public class Arith { public static double add(double v1 ...

  2. java浮点数化为整数_[Java教程]javascript浮点数转换成整数三种方法

    [Java教程]javascript浮点数转换成整数三种方法 0 2014-06-24 04:00:27 将浮点数转换成整数方法有很多,分享三种常用方法. Summary 暂时我就想到3个方法而已.如 ...

  3. java中单精度浮点数和双精度浮点数_什么是浮点型?单精度浮点数(float)和双精度浮点数(double)介绍...

    作为一名java学习者,怎能不懂这些java基础中的基础呢?本文就带各位温顾温顾java浮点型.单精度浮点数.双精度浮点数. 浮点型首先明确java中浮点型数据类型主要有:单精度float.双精度do ...

  4. Java虚拟机规范阅读(二)IEEE754简介以及Java虚拟机中的浮点算法

    什么是浮点数 在计算机系统的发展过程中,曾经提出过多种方法表达实数.典型的比如相对于浮点数的定点数(Fixed Point Number).在这种表达方式中,小数点固定的位于实数所有数字中间的某个位置 ...

  5. Java标识符首字母可以是哪些_Java编程中你不知道的一些标识符有哪些呢?

    java语言标识规则 ①包名所有字母一律小写,例如cn. itcast. testo ②类名和接口名每个单词的首字母都要大写,例如 ArrayList. terator ③常量名所有字母都大写,单词之 ...

  6. java char字符转编码_一、java基础-数据类型_数据类型转化_字符编码_转义字符

    1.Java  支持的8种基本数据类型: java的四种整数数据类型:byte 1    short 2     int4     long8 byte     8位带符号整数 -128到127之间的 ...

  7. Java入门1.2.3—一个老鸟的Java学习心得

    Java入门1.2.3-一个老鸟的Java学习心得 基本信息 作者: 臧萌    出版社:清华大学出版社 ISBN:9787302217831 上架时间:2010-3-30 出版日期:2010 年3月 ...

  8. java url 格式化_String.format()的使用:Java字符串格式化

    今天技术之家陪你一起了解Java开发:String.format()的使用:Java字符串格式化 1 问题背景 当我们在开发中需要格式化时或者当一字符串中有某一部分是需要变化时,我们应该如何快速的进行 ...

  9. 《14天从0到1学Java》第一天之04第一行Java代码

    这个是一套系列教程,一共14天,每天8篇,每篇需要5分钟.也就是说,你需要每天抽出40分钟来看教程,坚持14天就可以,轻松玩转JavaSE. 我是Feri,我为自己代言,专注IT职场教育N年,希望对于 ...

最新文章

  1. Android内存管理之道
  2. java double 小数点后两位小数_Java中double类型的数据精确到小数点后两位
  3. python的std函数_Python numpy.nanstd函数方法的使用
  4. python中if语句求最大值_python 判断三个数字中的最大值实例代码
  5. Switchhosts软件安装包
  6. 程序员用C/C++打造车牌识别系统!同学眼中只有膜拜!
  7. 元素查找(xpath)
  8. 小学计算机编程学什么,小学生编程都学什么
  9. matlab三相电路基波图形,非正弦稳态对称三相电路如图a所示。A相电源电压为,其中基波角频率为ω1=1rad/s。负载参数为R=...
  10. npm安装 elementui 报错:404 Not Found - GET https://registry.npmjs.org/@vue%2fvue-loader-v15 - Not found
  11. MOOS-ivp简介
  12. android绘制view的撤销,DrawingView android 上的一个涂鸦控件。可以设置画笔的粗细,颜色,撤销上一笔涂鸦,提供保存图片的接口。 @codeKK Android开源站...
  13. 一次悲惨的被挖矿经历
  14. PXE配置中ks.cfg文件介绍
  15. Apache修改服务器监听端口,练习 - 04 - 配置 Apache 服务器的监听端口
  16. 基于希克斯需求价格弹性计算_#炳哥经济学临考密押DAY1#微观计算题考点大盘点(上)...
  17. 04_nand基本操作
  18. 键盘与ASCII码对应表
  19. 什么是ERC20,ERC721?两者的区别是什么?
  20. 详细教你用NPS搭建内网穿透服务,外出时轻薄本轻松连接家里的游戏主机远程玩耍

热门文章

  1. Linux eBPF:bcc 用法和原理初探之 kprobes 注入
  2. Linux内核开发:内核模块参数
  3. linux内核 struct page结构的三种存放方式
  4. 在Linux下写一个简单的驱动程序
  5. linux内核:__user,__kernel,__safe,__force,__iomem
  6. 闭包 python_根据面试题谈谈 Python 闭包
  7. 光线追踪技术 清华大学 pdf_实时光线追踪技术:业界发展近况与未来挑战
  8. 网络编程t-io介绍
  9. python的for语句要用冒号吗_python中循环的写法 for
  10. mysql约束条件整型_MySQL 表的操作