alexbarry / AlexCalc

Scientific calculator with LaTeX equation display, variables, units, complex numbers. Runs locally in your browser with WebAssembly.
Other
26 stars 1 forks source link

Define custom units and formulas #18

Open AZeed18 opened 4 months ago

AZeed18 commented 4 months ago

Feature Requests:

  1. Define custom units like x*100->x m to cm
  2. Define formulas (variables that depend on other variables) like 100+x*1.5->cost (on updating x, cost is automatically updated)
alexbarry commented 3 months ago

Thanks for the suggestions! I think I should be able to implement these within the next month or so. I've been busy, but I think these features wouldn't be too tricky. (edit: and sorry for the delay!)

Custom units

For custom units, I wish I could support arbitrary expressions (x*100 is easy, but x*100 + 50 would actually be tricky in some cases[1]). So to reflect that limitation, and to distinguish from "->" (stores a variable) and "to cm" (which right now converts the output value), what would you think of this notation:

:unit 1 cm = (1/100) m

I think I could pretty easily handle that, and implement it within the next few weeks. (The text part is probably easy, the the GUI part might take some more time).

Formulas

For 100 + x*1.5 -> cost, how would the calculator know whether or not you want cost to be updated when x changes, or not? Sometimes I re-define variables and forget that I used them when defining other cases.

What would you think instead about a definition like this, with brackets at the end:

100 + x*1.5 -> cost(x)

Then you could get the latest vaue by doing cost(x). (or just cost(123) or any value)

Local variables

As a continuation of the above, what if you did 100 + 1.5*y -> cost(), or something like 100 + x*1.5*y -> cost(x). Should it always take the updated y value? I think that in most programming languages, this would depend on whether or not y was a global variable. My initial feeling is that storing the value of the (non parameterized) variables when you define the function would be the least confusing ("least surprise"). If you define a function, then later change one of its dependent (but not parameterized) variables, you might forget that you used it in the function, especially if its name is something generic like "a". Then if you do want it to update, you can just re-define the function or make sure that it is a parameter.

(Aside: perhaps I could add a shortcut to re-define a function to take the latest variable values that aren't parameters. Something like :redefine f(x).

Other thoughts

[1]: For custom units like x*100 + 50, this would be hard for something like degrees Celsius. In some cases when you say "0 degrees Celsius" you want it to be 273.15 Kelvin. But for the case where you're treating it as a difference ("q = m*c*delta T"), you would want 0 degrees Celsius to equal 0 K. I couldn't figure out a clear way to handle these "non linear" units, so because of that, I never added them. But let me know if you have any ideas and I'd be happy to think about them, I would really like to support arbitrary units (or at least units with an offset, or maybe logarithm).

AZeed18 commented 3 months ago

Thanks for reply!

Your suggestions fill so much gaps!

If a variable is used in function but not in its parameters, then I think should use that variable's value, this would allow using variables represnting constants for example to be used in functions

I wonder why non linear unit conversions can't be defined while, similarly, one would be able to create a non linear function that takes a unit and outputs another (i.e., unitA_to_unitB(unitA_value))?

AZeed18 commented 3 months ago

100 + 1.5*y -> cost() = 100 + 1.5*y -> cost

AZeed18 commented 3 months ago

There is an important feature missing: ability to copy result, you can now only copy LaTeX of result