Java中浮点数和双精度的包含范围是多少?
为什么不建议在精度至关重要的地方使用float或double?
我的同事做了这个实验:
public class DoubleDemo {
public static void main(String[] args) {
double a = 1.435;
double b = 1.43;
double c = a - b;
System.out.println(c);
}
}
Run Code Online (Sandbox Code Playgroud)
对于这个一年级的操作,我期望这个输出:
0.005
Run Code Online (Sandbox Code Playgroud)
但出乎意料的是输出是:
0.0050000000000001155
Run Code Online (Sandbox Code Playgroud)
为什么在这么简单的操作中双重失败?如果double不是这项工作的数据类型,我应该使用什么?
我试图确定双精度的最大精度是多少.在此链接中的已接受答案的注释中,在Java中保留精度为double @PeterLawrey表示最大精度为15.
你怎么决定这个?
我想了解Java double类型如何在Java 中将其值存储在内存中.当我运行以下代码时,我得到意外的输出:
public static void main(String[] args) {
float a = 1.5f;
float b= 0.5f;
double c= 1.5;
double d = 0.5;
float a1 = 1.4f;
float b1= 0.5f;
double c1= 1.4;
double d1 = 0.5;
System.out.println(" a- b is " + (a-b));
System.out.println(" c- d is " + (c-d));
System.out.println("a1-b1 is " + (a1-b1));
System.out.println("c1-d1 is " + (c1-d1));
Run Code Online (Sandbox Code Playgroud)
}
输出:
a- b is 1.0 c- d is 1.0 a1-b1 is 0.9 c1-d1 is 0.8999999999999999
为什么c1-d1 …
我有这个代码
#include <iostream>
using namespace std;
int main(int argc,char **argv) {
unsigned long long num1 = 99999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999995LL;
unsigned long long num2 = 99999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999996LL;
unsigned long long num3 = 99999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999997LL;
unsigned long long num4 = 99999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999998LL;
unsigned long long num5 = 99999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999LL;
cout << (unsigned long long)(num1 * num2 * num3 * num4 * num5) << endl;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
你可以看到数字是巨大的,但当我在那里做数学时我得到了这个:18446744073709551496
在编译时我得到这些警告:
warning: integer constant is too large for its type|
In function `int main(int, char**)':|
warning: this decimal constant is …Run Code Online (Sandbox Code Playgroud) 我有以下代码:
Double x = 17.0;
Double y = 0.1;
double remainder = x.doubleValue() % y.doubleValue();
Run Code Online (Sandbox Code Playgroud)
当我运行这个时,我得到余数= 0.09999999999999906
知道为什么??
我基本上需要检查x是否可被y完全整除.你能在java中建议其他方法吗?
谢谢
我正在制作直方图课程,我遇到了一个奇怪的问题.
以下是该类的基础知识,有更多方法,但它们与问题无关.
private int[] counters;
private int numCounters;
private double min, max, width;
public Histogram(double botRange, double topRange, int numCounters) {
counters = new int[numCounters];
this.numCounters = numCounters;
min = botRange;
max = topRange;
width = (max - min) / (double) numCounters;
}
public void plotFrequency() {
for (int i = 0; i < counters.length; i++) {
writeLimit(i * width, (i + 1) * width);
System.out.println(counters[i]);
}
}
private void writeLimit(double start, double end) {
System.out.print(start …Run Code Online (Sandbox Code Playgroud) 可能重复:
在java中保留双精度的精度
你知道Java中这两个操作之间的区别吗?
final double m1 = 11d / 1e9; // gives 1.1e-8
final double m2 = 11d * 1e-9; // gives 1.1000000000000001e-8
Run Code Online (Sandbox Code Playgroud)
我在生成的字节码中看到m2的预编译结果已经不是我的预期.
在输出中javap -verbose -c我可以看到以下值:
const #3 = double 1.1E-8d;
[...]
const #6 = double 1.1000000000000001E-8d;
Run Code Online (Sandbox Code Playgroud)
当我在其他表达式中使用m1或m2时,我没有相同的结果.
当我在C中尝试相同的事情时,m1和m2严格地是1.1e-8
我认为我的问题在于java处理双精度计算的方式,但我无法解释自己错过了什么.
我使用下面的方法得到了错误的结果.
public double evaluate(final double leftOperand, final double rightOperand) {
Double rtnValue = new Double(leftOperand * rightOperand);
return rtnValue.doubleValue();
}
Run Code Online (Sandbox Code Playgroud)
输入参数值为:leftOperand = 100和rightOperand = 2.55
我得到了错误答案: 254.99999999999997
正确答案是255.0
可能重复:
在java 中使用双精度保留精度在双精度中
移动小数位
例如,像这样简单:
public class WrongAnswer {
public static void main(String[] args) {
System.out.println(100*1.1);
}
}
Run Code Online (Sandbox Code Playgroud)
打印110.00000000000001而不是110.使用其他数字而不是100*1.1也会给出很多数字,最后一些随机数字不对.
有任何想法吗?