/ does integer division for integer operands (should be float per ISO 9.1.4) #17
Labels
No labels
bug
documentation
duplicate
enhancement
good first issue
help wanted
invalid
question
wontfix
No milestone
No project
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set.
Reference
navicore/patch-prolog#17
Loading…
Add table
Add a link
Reference in a new issue
No description provided.
Delete branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Symptom
X is Int1 / Int2returns an integer (truncated toward zero) instead of the float quotient ISO 13211-1 §9.1.4 requires. Mixed-type and float-only operands behave correctly.Repro
Root cause
crates/patch-prolog-core/src/builtins.rs:706(arith_div) — the Int/Int arm callsi64::checked_divand wraps the result inArithVal::Int. That's what//should do;/should evaluate to a float per ISO. The other arms (Float/Float,Int/Float,Float/Int) all go throughcheck_float, so they already produce float results correctly.Impact
Subtle for casual use, sharp for anything that mixes Prolog with code that expects ISO-conforming arithmetic. Teaching it correctly in the loglings curriculum requires steering students to
//for the "integer dozens" exercise instead of letting them use/— which is awkward because/is the universal "divide" symbol everywhere else.It also produces silently wrong answers — no error, no warning, just the wrong number. That's the worst kind of language gap.
Suggested fix
In
arith_div, change the Int/Int arm to compute(*a as f64) / (*b as f64)and feed it throughcheck_float(...). The zero-divisor guard above it stays.//(arith_int_divaround line 738) is already a separate function and is unaffected.Update the divide-by-zero error label for
/from "integer division" to "float division" since the operation is now float-valued (or just drop the qualifier;1 / 0would otherwise inherit a misleading label).Add a unit test in
arith_div's test cluster covering:10 / 3 = 3.333...,-10 / 3 = -3.333...,1 / 0throwsevaluation_error(zero_divisor).ISO reference
ISO 13211-1 §9.1.4 — the
(/)/2evaluable functor. The Number Result table specifies float result for both integer and float operands.#22