Comments (5)
In fact, this appears to be a general problem with the divisor is negative:
% expr {11/-10}
-2
% expr {-11/-10}
2
from molt.
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.
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.
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.
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)
- Molt Book should include more information about External Types.
- Upgrade Molt Book to latest mdbook.
- molt_shell::repl should include commands with errors in the history
- stack overflow on running tests HOT 4
- Passing closures to `add_command`? HOT 1
- Add "string reverse" command
- Add `string tolower` command
- Add `string toupper` command
- Add `string trim`, `string trimleft`, `string trimright` commands
- Dynamic prompts for the REPL HOT 7
- molt_shell::repl configuration HOT 1
- array subcommand links in Molt Book
- The "error" command should support optional errorInfo and errorCode arguments
- molt_throw! macro
- Include proc line number in error stack trace.
- Include "unevaluated" form of command in stack trace.
- Typo in Molt Book Example: Custom Shell Applications HOT 1
- TCL 8 Namespaces HOT 7
- The env() Array HOT 1
- Mirror env() values back to the process HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from molt.