Re: [讨论] 请大家聊聊 JavaScript的缺陷

楼主: oopFoo (3d)   2026-01-16 21:55:26
怎么会是banker's rounding?
完全是float/double无法精准代表fraction的问题。
https://www.h-schmidt.net/FloatConverter/IEEE754.html
https://www.binaryconvert.com/convert_double.html
你可以看到在float,0.005实际上是0.004999999888241291046142578125
但在double, 0.005是0.00500000000000000010408340855861
浮点只能找个最接近0.005,但并不是真正的0.005。
所以rounding会往上或往下,是取决于最接近的浮点是>5或 <5。
你的程式对了,但你的ai怎么结论怎么会是banker's rounding?
※ 引述《descent (“雄辩是银,沉默是金”)》之铭言:
: 对这问题很好奇, c++ 也有类似的情形。
: 1 #include <iostream>
: 2 #include <string>
: 3 #include <cmath>
: 4 using namespace std;
: 5
: 6 int main(int argc, char *argv[])
: 7 {
: 8 std::cout.precision(2);
: 9 cout << fixed << 0.005 << endl;
: 10 cout << 0.015 << endl;
: 11 cout << 0.025 << endl;
: 12 cout << 0.035 << endl;
: 13 cout << 0.045 << endl;
: 14 cout << 0.055 << endl;
: 15 cout << 0.065 << endl;
: 16 cout << 0.075 << endl;
: 17 cout << 0.085 << endl;
: 18 cout << 0.095 << endl;
: 19 return 0;
: 20 }
: n.cpp 执行结果
: 0.01
: 0.01
: 0.03
: 0.04
: 0.04
: 0.06
: 0.07
: 0.07
: 0.09
: 0.10
: 借由 ai, 终于知道是怎么回事, 当决定要印出小数点 2 位数时候用的算法是
: “偶数舍入法”(Banker's Rounding) 这是很多绘图、统计或会计系统的规则:
: 若刚好在 .5 的位置, 则舍入到最接近的“偶数”。
: 0.005 → 靠近 0.00 还是 0.01? 这里 0 是偶数, 所以会趋向 0.00, 但在电脑里
: 0.005 是 0.00500000000000000010 所以它判断靠近 0.01。
: 0.015 → 靠近 0.01 还是 0.02? 这里 2 是偶数, 本应往 0.02 走, 但在电脑里 0.015
: 是 0.0149..., 所以它决定留在 0.01。
: 另外也请 ai 给出一个简易版本的算法, 实作 Banker's Rounding。
: fn.cpp
: 1 #include <iostream>
: 2 #include <iomanip> // 必须包含此库以使用 setprecision
: 3
: 4 #include <cmath>
: 5 #include <string>
: 6 #include <cstdio>
: 7
: 8 using namespace std;
: 9
: 10 /**
: 11 * 模拟 setprecision(2) + fixed 的行为
: 12 * @param value 要输出的数值
: 13 * @param precision 小数点后位数
: 14 */
: 15 void my_print_fixed(double value, int precision) {
: 16 cout << fixed << setprecision(20) << value << endl;
: 17
: 18 // 1. 取得放大倍数 (例如 precision 2 则为 100)
: 19 long double multiplier = std::pow(10, precision);
: 20
: 21 // 2. 模拟底层舍入规则
: 22 // 注意:std::round 在这里会反映出 0.015 储存成 0.01499... 的事实
: 23 double rounded_value = std::round(value * multiplier) / multiplier;
: 24
: 25 // 3. 格式化输出字串
: 26 // 我们用 printf 的格式化字串来模拟输出流的最后一步
: 27 char format[10];
: 28 sprintf(format, "%%.%df", precision);
: 29 printf("Input: %.20f | Result: ", value);
: 30 printf(format, rounded_value);
: 31 printf("\n");
: 32 }
: 33
: 34 int main() {
: 35 double n1 = 0.005;
: 36 double n2 = 0.015;
: 37
: 38 std::cout << "
作者: sceleton (sceleton)   2026-01-16 23:39:00
推!
作者: wuyiulin (龙破坏剑士-巴斯达布雷达)   2026-01-17 02:48:00
同意,我刚刚在看他测资跑起来就想说跟 banker's rounding 有啥关系,不就二进制误差(#。
作者: jonathan793 (pusheen cat)   2026-01-17 12:12:00
就...有人大学没认真念 ai随便唬就信了糗的是还沾沾自喜发文
作者: descent (“雄辩是银,沉默是金”)   2026-01-17 13:28:00
感谢补充

Links booklink

Contact Us: admin [ a t ] ucptt.com