gpt4 book ai didi

java - 2 个双变量乘积的意外结果

转载 作者:行者123 更新时间:2023-12-01 04:16:02 24 4
gpt4 key购买 nike

double price = 4.35; 
double quantity = 100;
double total = price * quantity; // Should be 100 * 4.35 = 435.00
System.out.println(total); // Prints 434.99999999999999

为什么会发生这种情况?

最佳答案

乘法工作正常,但 4.35 不能精确地表示为 double 。

检查floating point Internal Representation .

维基百科说:-

The fact that floating-point numbers cannot precisely represent all real numbers, and that floating-point operations cannot precisely represent true arithmetic operations, leads to many surprising situations. This is related to the finite precision with which computers generally represent numbers.

关于java - 2 个双变量乘积的意外结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19442246/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com