XPath: mod uses floored modulo instead of truncating remainder

未關閉 適合新手
#385 0 則留言 0 個 reaction 已指派 0 人 在 GitHub 檢視

還沒有人認領這個 Issue。

評估

難度
2/5
預估耗時
1-3 小時
新手友好度
78/100
Issue 類型
缺陷
描述清晰度
描述清楚
活躍度
活躍
技術堆疊
ruby
領域
backend

研究方向

從 lib/rexml/xpath_parser.rb 中的 mod 運算開始,issue 指出 left % right 會造成符號不一致。閱讀周邊對除數為零以及運算元為無限大或 NaN 的處理,接著檢查是否有涵蓋負運算元和零的 XPath 測試。完成的標準是:範例符合 XPath 1.0 餘數的符號規則,同時 x mod 0 和指定的特殊情況仍回傳 NaN。

由索引模型根據 Issue 內容生成。

描述

XPath mod gives the wrong sign when an operand is negative:

require "rexml/document"
doc = REXML::Document.new("<r/>")
REXML::XPath.first(doc, "5 mod -2")    # => -1.0, expected 1
REXML::XPath.first(doc, "-5 mod 2")    # => 1.0, expected -1
REXML::XPath.first(doc, "-0.5 mod 3")  # => 2.5, expected -0.5

The first two are the examples in the spec (https://www.w3.org/TR/xpath-10/#numbers):

The mod operator returns the remainder from a truncating division. For example,
5 mod 2 returns 1
5 mod -2 returns 1
-5 mod 2 returns -1
-5 mod -2 returns -1

libxml2 gives 1, -1 and -0.5.

Cause: lib/rexml/xpath_parser.rb uses left % right. Ruby's % is a floored modulo (the result takes the sign of the right side). left.remainder(right) truncates like the spec asks; it raises ZeroDivisionError for a zero right side, so x mod 0 (and an infinite or NaN left side) needs to keep returning NaN.

主要語言
Ruby
星號
181
分支
105
PR 合併指標
30 天內沒有已合併 PR

環境準備

這個專案沒有提供開發容器、Dockerfile 或貢獻指南,環境需要你自己搭建:先看它的 README,通用步驟見我們的新手貢獻指南。

從這裡開始

  1. 先讀完整個 Issue,再讀專案的貢獻指南。
  2. 在 Issue 下留言說明你要接手 —— 這能避免兩個人做同樣的事。
  3. Fork 儲存庫,在一個分支上完成修改。
  4. 送出 Pull Request,並在描述裡引用這個 Issue 編號。

ruby/rexml 的其他 Issue

查看 ruby/rexml 的全部 Issue

相似的 Issue

更多 Ruby Issue

把新 issue 寄到你的電子郵件信箱

精選適合新手參與的 GitHub issue 摘要。