alexbarry / AlexCalc

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

Add the eV derived units #20

Open ethanc8 opened 1 month ago

ethanc8 commented 1 month ago

This might be a bit tricky.

These units are commonly used in high-energy physics and particle physics. Also, they are often all referred to as "eV", when you set c = 1.

The next three are super common in physics:

1 eV = 1.602176634×10^-19 J 1 eV/c² = 1 eV = 1.78266192×10^-36 kg 1 eV/k_B = 1 eV = 11604.51812 K

A few less common ones can be seen at https://en.wikipedia.org/wiki/Electronvolt#Relation_to_other_physical_properties_and_units.

The tricky thing about this is that if we want to be able to parse expressions with "eV", we will need to figure out if the user is talking about mass, temperature, or energy.

alexbarry commented 1 month ago

First, it occurred to me that I should add some pre-defined constants like below. Would that be useful, or do you think people would prefer to define their own constants? (perhaps without the units, I imagine it could be a hassle to be forced to use units sometimes, maybe I should add a toggle to just ignore them-- though I don't know how that could work for constants that people use in different units sometimes)

3e8 m s^-1 -> c 1.380649e-23 J K^-1 -> k_B

This way, 1 eV, 1 eV/c^2, and 1 eV/k_B should then all have the same values that you listed above.

Could you give me a few examples on what you'd expect to happen? If you tried to do 1 eV + 1 K then I could easily detect the mismatch in units, and suggest "trying to add energy and temperature, would you like to divide the energy value by Boltzmann's constant?" I could also add a "do this every time" button.

But if you are multiplying, I don't know how you could tell how the user wants to treat 1 eV.

If you just want to convert the final result, then you could simply add to K or to kg at the end of your expression, which is how I allow setting the output units. This currently only converts the values if they are the same base units, but multiplied by a constant (e.g. "cm" to "mm", "km" to "miles"). But I could easily modify it to say something like "trying to convert an energy to a temperature, dividing by Boltzmann's constant", and I think that would be intuitive enough.

But if you had an expression where you need the conversion to happen as soon as you enter the value, I don't know how I could figure out which units to use. A simple example of this is with degrees Celsius. I considered adding it, but I thought it would be too ambiguous. If the user says "0 degrees Celsius to Kelvin" then it is obvious. But if you are treating the temperature as a difference, i.e. "q = m c delta T", then putting "0 degrees Celsius" in for "delta T" would need to convert to 0 K, not 273.15 K.

If the ambiguity is too difficult to manage, what would you think of simply creating new units like "1 eVkg" or "1 eVm" (m for mass), or "1 eVK" or "1 eVT" etc. This might look confusing in plaintext, but if you use the GUI popup I could simply show the unit as "1 eV (mass)", or perhaps "1 eV (1.78266192e-36 kg)".

This is similar to how I have "pounds (force)" (lbf) and "pounds (mass)" (lb) in the unit popup now. I think that convention is somewhat more universal. If there's some convention for "eV" then I could easily implement that. And possibly (referencing the other issue) I could add a separating dot to make it slightly more clear, e.g. 1 eV.m or 1 eV.T.

Another possibility: another user suggested adding the ability to define custom units. Then users could use whatever convention makes sense to them. I could easily implement this.