soveran / clac

Command-line, stack-based calculator with postfix notation
BSD 2-Clause "Simplified" License
356 stars 30 forks source link

Decimal separator and thousands separator #22

Open waltertross opened 4 years ago

waltertross commented 4 years ago

I understand that this is a controversial PR, because adapting to the different conventions for writing numbers can be done in many different ways. First of all, there is the choice between program arguments (like, e.g., -c to accept commas), that change the behavior of clac for the whole execution, and words (like, e.g., comma or cma), that change the behavior only when they are evaluated and only until they are superseded (e.g., by dot). Then there is the choice of modes and combinations of modes. Should input and output modes be independent or not? Should the sets of alternative modes be completely orthogonal or not? Should there be a minimum number of modes in order not to confuse people, or should maximum flexibility be the goal? As the PR stands now, I'm more undecided about the first part (arguments or words) than about the second (modes and their combinations), although the second depends on the first (e.g., because it is less useful to recognize both the dot and the comma at the same time as decimal separators.)

waltertross commented 4 years ago

Thinking about my previous comment, I found the first big problem with this PR (and with the handling of the thousands separator in general): if you have pi 3.14159265358979323846 in your words file, you can't use it any more if you start clac with -ct, because the dot is seen as a thousands separator and ignored :-(

waltertross commented 4 years ago

OK, I fixed that by parsing words without -c and -t. I didn't want to ditch the -t, because I really stumbled upon that when pasting numbers from my (meager) bank account...

waltertross commented 4 years ago

I noticed only now that -d can be mistaken to mean “dot”. That's unfortunate. I'm thinking of the following solution: 3 input modes: -b “both”, -c “comma”, -d “dot”. The default is -d. The output mode follows the input mode for -c and -d. For -b, it uses commas if it finds any commas in the input (excluding word expansion), dots otherwise. -t is allowed with -c and -d, but not with -b. Alternatively, there is no -t, but there is -C replacing -ct and -D replacing -dt, for a total of 5 modes: -b, -c, -C, -d (default), -D. The only drawback would be that capital letters often negate an option, but we could live with that.

waltertross commented 4 years ago

Other ideas: -b accepts both dots and commas as decimal separator, but always output dots (less smart but more predictable). One more mode: -B is the “smart” -b, behaving like I previously proposed for -b.

waltertross commented 4 years ago

I decided to go with the 5 separator modes (with the “dumb” -b and without -B).

waltertross commented 4 years ago

One last idea: -e, European mode ;-) Like -b, but displaying , as the decimal separator. Admittedly, that's a bit stretched, but -B would break orthogonality, and I haven't found a better initial yet.