tmilev / calculator

https://calculator-algebra.org
Other
8 stars 0 forks source link

Round to certain decimal place #27

Closed jaker-dotcom closed 2 weeks ago

jaker-dotcom commented 1 month ago

Hello Mr Milev, thanks for the awesome calculator. As I do my study notes in LaTeX, this really helps in my study flow.

I was hesitant to ask for such a simple function but I could not find it amongst the list of examples. How to round e.g. to two or three decimal places? All I found is the function Round() but that rounds to integers only.

e.g. when comparing coefficients of variation, an expression like \frac{\sqrt{1044900}} {35710} = \frac{9}{3571}\sqrt{129} is not very helpful.

A function that rounds to e.g. tree decimal places (or n decimal places, where n can be given as argument) would save me the time of re-typing the final expression in another calculator.

P.S: Sth like TurnOnApproximations(0) which turns approximations on for log and trigonometric functions.

tmilev commented 1 month ago

Hi jaker, Many thanks for the feedback! The command is

DoubleValue( \frac{\sqrt{1044900}} {35710} )

https://calculator-algebra.org/app?%7B%22currentPage%22%3A%22calculator%22%2C%22calculatorInput%22%3A%22%5CnDoubleValue%5C%5Cfrac%7B%5C%5Csqrt%7B1044900%7D%7D%20%7B35710%7D%20%5Cn%3B%5Cn%5Cn%22%7D

You can peek inside as to what the calculator is doing by enclosing everything in LogEvaluationSteps as in:

LogEvaluationSteps ( DoubleValue\frac{\sqrt{1044900}} {35710} )

https://calculator-algebra.org/app?%7B%22currentPage%22%3A%22calculator%22%2C%22calculatorInput%22%3A%22LogEvaluationSteps%20(%5CnDoubleValue%5C%5Cfrac%7B%5C%5Csqrt%7B1044900%7D%7D%20%7B35710%7D%20)%5Cn%5Cn%22%7D

You can also separate multiple commands with semicolon.

I don't have a command to round to a fixed value of decimals. I could easily add one: what syntax would you propose? I will gladly code that up and update. Here's one possibility: I could code:

DoubleValue(\frac{\sqrt{1044900}} {35710}, 2) for two decimal positions would return 0.03.

I could also make a function for significant digits (0.00000119 rounds to 0.00 up to two decimal positions, but rounds to 0.0000012 up to two significant digits).

Interested? Feel free to propose a different keyword if you have better ideas than DoubleValue.

P.S. You can indeed immitate rounding up to a given digit with the good old trick of:

A= DoubleValue (\frac{\sqrt{1044900}} {35710} ) ; DoubleValue (Round (A*1000)/1000)

But that's a lot of work; if the calculator is not saving you time, better not waste your time on it, right?

tmilev commented 1 month ago

I implemented a rounding function, but will not deploy it before I get your response: especially on which syntax you want or - should I reuse DoubleValue, or should I use some other command name?

I am also taking other ideas for behavior

The commit is here:

https://github.com/tmilev/calculator/commit/1c2ff5a9e4dbc69c36e6bb02afe01af08569cb4e

tmilev commented 1 month ago

Just read your second proposal: to have TurnOnApproximations(0) work for square roots. That sounds like a great idea and shouldn't be too difficult to do.

Edit: just coded this, seems to work like a charm. Not deployed yet.

jaker-dotcom commented 4 weeks ago

Hi, thanks for working on this! You seem very enthusiastic about my issue which makes me happy :) I am sorry for the delay, I was on vacation and did not check my github.

The double value function is really useful. I don't know how I missed that it existed. I still think that the TurnOnApproximations(0) should work on square roots. But that may just be my personal preference, so in the end it's up to you to decide.

If you still think a rounding function would add to your calculator, I would suggest going for the significant decimal positions. But again someone else may prefer significant decimal places.

Now that I think about it: Can there be optional arguments? Like in Excel where you can, for comparing lists, add a ; 1 or ; 0 behind the other arguments to choose between exact and approximate lookup. Then you could decide on the default values of e.g. significant decimal places by default but can be overwritten.

E.g.

DoubleValue(\frac{\sqrt{1044900}} {35710}, 2) for two significant decimal positions would return 0.028.

DoubleValue(\frac{\sqrt{1044900}} {109201321}, 2, 1) for two fixed decimal positions would return 0.03. where the last optional option activated the fixed decimal positions.

tmilev commented 3 weeks ago

I still think that the TurnOnApproximations(0) should work on square roots

Implemented and deployed, as per your original suggestion: https://tinyurl.com/wb5xzdku Many thanks for the constructive feedback!

Now that I think about it: Can there be optional arguments? Absolutely. The parser supports an arbitrary number of arguments, so that's no problem.

DoubleValue(\frac{\sqrt{1044900}} {35710}, 2) for two significant decimal positions would return 0.028. DoubleValue(\frac{\sqrt{1044900}} {109201321}, 2, 1) for two fixed decimal positions would return 0.03

Will try to implement this some time this week and deploy - will ping you back when done.

Of course, we can and should have both solutions!

tmilev commented 3 weeks ago

I implemented DoubleValue (x, 3) to be significant digits and DoubleValueFixed(x, 3) to be fixed digits.

Here's how it works:

x=0.0023;
DoubleValue (x, 3);
DoubleValueFixed(x, 3)

https://calculator-algebra.org/app?%7B%22currentPage%22%3A%22calculator%22%2C%22calculatorInput%22%3A%22x%3D0.0023%3B%5CnDoubleValue%20(x%2C%203)%3B%5CnDoubleValueFixed(x%2C%203)%22%7D

Indeed, your proposal of reusing the DoubleValue and adding a third argument is feasible, but I felt a new keyword - DoubleValueFixed - will be easier for me to remember, also it shows up in the autocomplete menu.

I've also fixed a few small issues with the frontend (the menu on the right wasn't collapsing correctly).

jaker-dotcom commented 2 weeks ago

Working flawlessly, thanks!