lcn2 / calc

C-style arbitrary precision calculator
http://www.isthe.com/chongo/tech/comp/calc/index.html
Other
357 stars 51 forks source link

Enhancement: Comma as decimal operator #146

Closed robertoash closed 6 months ago

robertoash commented 6 months ago

Is your feature request related to a problem? Please describe. My Swedish keyboard has a comma instead of a period in the NumKeypad. It would be realldy nice to be able to configure calc to use that instead of dot as a decimal operator.

The majority of European countries use this numbering system so I think a lot would benefit from it.

Describe the solution you'd like To be able to send an argument, have calc read an environment variable or have a config file in order to replace the dot with a comma as a decimal operator.

Describe alternatives you've considered The way I see it, an environment variable would be best since this is most likely a permanent change one would want and not something that one would change on a per operation basis.

Regarding using a bare dot as a reference for the last result, this could be also configured with another environment variable. Hence, for example:

One could go as far as allowing to configure the character desired but I am not sure that would add much value and may even cause unwanted conflicts:

lcn2 commented 6 months ago

Thank you, @robertoash for your enhancement request.

While I very sympathetic to the use of the decimal comma, and in tools such as number we provide a European ruleset, there is a significant barrier to doing so in calc.

Calc is a C-style arbitrary precision calculator. As with C, the calc language uses the comma "," as a fundamental operator.

Consider the following function call:

; round(2.7)
    3

Consider what would happen if that 2.7 were written with the decimal comma:

; round(2,7)
    2                          <<== DIFFERENT value is returned!

This is because the round function takes optional arguments, separated by comma ",";s, In this case 7 becomes number of places to round.

Moreover consider how awkward it would be if the decimal comma were enabled and one wanted to round 2,7 to 2 places.

Consider:

; round(2.35,1)
    2.4

Say somehow the harder were to understand that the 2nd comma separated the next argument, unfortunately existing code would break:

; round(2,35,1)
    2                          <<== DIFFERENT value is returned!

This is because the round function can take a 3rd optional argument (rounding mode).

Even worse, consider the declaration is a matrix:

; mat a[2] = {2,3}
; a

mat [2] (2 elements, 2 nonzero):
     [0] = 2
     [1] = 3

Consider what would happen if the 2,3 were written with the decimal comma:

; mat a[2] = {2,3}
; a

mat [2] (2 elements, 1 nonzero):
     [0] = 2,3
     [1] = 0                      <<== NOT all values are initialized!

Much worse, declaring variables would break. This is valid calc:

 a = 2.5, b = 7

However if a were initialized with a decimal comma:

 a = 2,5, b = 7

one generates a "Constant before comma operator" warning.

I could go on and on, but I think you get the point / comma. 🤓

I don't see an easy way to allow decimal comma's a part of the calc input syntax language, @robertoash. I wish there was a straightforward way to allow for decimal comma's in calc, but unfortunately:

lcn2 commented 6 months ago

While this issue ie closed, @robertoash, you and others are free to comment.