Math.Round(x) rounds imprecisely under Fable 5.26.0

クローズ
#5,251 コメント 0 件 リアクション 0 件 担当者 0 名 GitHub で見る

メンテナーはふだん 1 日以内に返信

関連するプルリクエストがすでにマージされています。

  • #5254 @MangelMaxime による — マージ済み

評価

難易度
3/5
見積もり時間
1〜2日
初心者へのやさしさ
62/100
issue の種類
バグ
明瞭さ
明確に書かれている
活発さ
活発
技術スタック
javascript
領域
compilers

調査の方向性

The bug is in the round helper of fable-library-js (Util.round), which uses toFixed(8) and an epsilon tie check; the reproduction is the Math.Round loop over 2.500000001 and 3.4999999999 in the issue. Start by reading that helper and checking whether the same logic is mirrored in other Fable targets. Done when both values round to 3 and Math.Round(x, digits) matches .NET behaviour, with a regression test added.

索引モデルが issue の本文から書いたものです。

説明

Math.Round(double) calls a value a tie when its fraction is within 1e-8 of 0.5, so values near a half round the wrong way. That way 2.500000001 rounds down to 2 and 3.4999999999 rounds up to 4 under Fable 5.26.0, instead of 3 for both cases.

To reproduce this issue

open System
for x in [ 2.500000001; 3.4999999999 ] do
    printfn "%s" (string (Math.Round x))

fable-library-js’s Util.round reads the value through toFixed(8) and calls it a tie when the fraction is within 1e-8 of 0.5

const n = +(digits ? value * m : value).toFixed(8);
...
const r = (f > 0.5 - e && f < 0.5 + e) ? ((i % 2 === 0) ? i : i + 1) : Math.round(n);

Math.Round(double) works on the exact value and a tie is a fraction of exactly one half.

Suggested fix that would be exact.

const t = Math.trunc(x), f = Math.abs(x - t);
return f > 0.5 ? t + Math.sign(x) : f < 0.5 ? t : (t % 2 === 0 ? t : t + Math.sign(x));

For Math.Round(x, digits) the same applies after scaling by 10^digits, as .NET does.

主要言語
F#
スター
3.2k
フォーク
335
平均マージ
19時間 39分
マージ済み PR(30日)
158

環境構築

Codespaces で開く

このプロジェクトの開発コンテナを、あなたの GitHub アカウントでブラウザ上に起動します。

  • Dockerfile・Docker Compose ファイルなし
  • プルリクエストのテンプレートなし
  • コントリビューションガイドなし

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

fable-compiler/Fable のほかの issue

fable-compiler/Fable の issue をすべて見る

似ている issue

Compilers の issue をもっと見る

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。