Giter Site home page Giter Site logo

Integer Division is incorrect about molt HOT 5 CLOSED

wduquette avatar wduquette commented on June 10, 2024
Integer Division is incorrect

from molt.

Comments (5)

wduquette avatar wduquette commented on June 10, 2024

In fact, this appears to be a general problem with the divisor is negative:

% expr {11/-10}
-2
% expr {-11/-10}
2

from molt.

wduquette avatar wduquette commented on June 10, 2024

The TCL 7.6 expr code contains a complex little algorithm to give consistent remainders with negative divisors across C compilers. This may still be an issue in Rust across platforms; but the translated code does the wrong thing, and inspecting it I can't see how the C code ever did the right thing. I've replaced it with a naive algorithm, and it seems to work. I'll need to check remainders separately.

from molt.

wduquette avatar wduquette commented on June 10, 2024

Whoops! I notice that TCL 8.6 gives the same answers as TCL 7.6, which implies that the answers are correct if counter intuitive. ????

Reopening until I understand what's going on.

from molt.

wduquette avatar wduquette commented on June 10, 2024

Here's a table of results I've compiled.

Operation TCL 7.6 TCL 8.6 Molt Rust
12 / 10 1 1 1 1
12 % 10 2 2 2 2
-12 / 10 -2 -2 -1 -1
-12 % 10 8 8 -2 -2
12 / -10 -2 -2 -2 -1
12 % -10 -8 -8 -8 2
-12 / -10 1 1 2 1
-12 % -10 -2 -2 8 -2

From this, we see:

  • TCL reliably rounds toward negative infinity; Rust reliably rounds toward 0.
  • C must reliably round toward negative infinity if the denominator is positive, since TCL 7.6 has no special code for that case, and is getting the same answer as TCL 8.6.
  • Rust does not, and so Molt (which like TCL 7.6, does nothing special in that case) is getting the same answer as Rust.
  • And because of this, when both numerator and denominator are negative Molt is simply wrong.

The question now is, which behavior is preferable for Molt? Consistency with the host language (appropriate for an extension language) or consistency with long-established TCL practice?

from molt.

wduquette avatar wduquette commented on June 10, 2024

Per Kevin Kenny:

  • TCL used to do whatever the underlying C compiler did.
  • At some point, it was decided to standardize on rounding toward negative infinity.
    • So that the same code could run on different platforms
    • So that test outcomes weren't platform dependent
    • And there were some nice numerical advantages.
  • Since then, C99, Python 3, and Rust, among others, have standardized on rounding toward zero.
  • Molt will now round toward zero:
    • This is what Rust does, and a Rust extension language should do arithmetic the same way Rust does.
    • It will be less confusing to newcomers.
    • It's simpler to implement, since it's what Rust does.

Kevin insists that the / and % must be defined consistently, i.e., that the following equation holds for integer a and b, where b is not zero:

b*(a/b) + (a%b) == a

from molt.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.