PaddiM8 / kalker

Scientific calculator with math syntax that supports user-defined variables and functions, complex numbers, and estimation of derivatives and integrals
https://kalker.xyz
MIT License
1.66k stars 74 forks source link

Units #9

Open joshcol9232 opened 4 years ago

joshcol9232 commented 4 years ago

Maybe have optional units in calculations, and convert the output into SI units. The units could be distinguished in some way, such as curly brackets or something: 10{m/s}.

Then in calculations, the output could be returned as SI units, or maybe we could add some other syntax like 10{m/s} = {miles/hour} and it would output in the desired unit.

This would be useful for things like physics/engineering etc where you are given units in various forms. So for example you could do 3{cm} * 100{GHz} to get the speed of a wave in {m/s} (c = wavelength frequency), or instead do `3{cm} 100{GHz} = {km/hour}` to get it in kilometers per hour.

I think this would be quite tough to implement but I am just throwing out some ideas :)

PaddiM8 commented 4 years ago

Yeah, I want this too. I'm currently exploring various ways of implementing this. Thanks for the suggestion!

PaddiM8 commented 4 years ago

I created a branch for the development for units. At the moment, it is not stable at all and mostly experimental. Branch: https://github.com/PaddiM8/kalk/tree/units
I was able to do the following:

unit m = 100cm
5m/25cm
>> 20
25cm/5m
>> 0.05
PaddiM8 commented 4 years ago

Update: It now works for a lot more types of expressions. There are still a lot that needs to be done, but I feel like this is actually going to work! When you write eg. unit deg = (rad*180)/pi it will automatically create a inverted one, eg. unit rad = (deg*pi)/180 so that you can do unit conversion both ways without defining it for both.

What's next? Well... making it so that the inverter can invert built-in functions (eg. sin to asin), a to keyword that let's you convert between units, more error checking, unit tests, adjusting the angle unit system so that you can still set degrees as the default angle unit if you want to, and some more things.

When it comes to parsing... What @eggmund suggested would probably be much faster and less awkward to parse, but I think this way might actually work out great! We'll see though, might have to change it.

Damn it, all the files weren't added in the first commit... my enter key is wonky

joshcol9232 commented 4 years ago

Nice work! Looks good :) . I was thinking an external crate such as simple_units (although I have no idea if this crate in particular is any good) could be used to have most common units predefined, so that units like m do not need to be defined. Or instead I wouldn't mind helping out with defining some common units within kalk. However this will probably interfere with variables made by the user unless some special syntax is used (such as the curly brackets i suggested ;) ).

PaddiM8 commented 4 years ago

I just merged it with master (and made it clear in the README it's still experimental). Hm yeah, I've been thinking about which units should be included by default. I've made it simple to define standard units (add them in the INIT constant in prelude.rs), and did include deg/rad units by default. As you said, having too many default units could clash with user-defined variables... A solution could be to provide sets of units, that the user can choose to use. Eg. one for physics units, one for day-to-day units, etc. Thank you for helping out with this btw!

joshcol9232 commented 4 years ago

Nice, and no worries happy to try and help :) It is a great project and I will probably end up using it quite a bit myself lol. I have an idea on conversion:

I like the idea of having sets of units, maybe even allow users to create/save/share unit sets would be cool. I am happy to help out wherever possible.

joshcol9232 commented 4 years ago

With that last suggestion, I guess the user can already do that with the to syntax. However for making conversion easier to handle, maybe having the base SI units as built in units will make conversion easier, as every defined unit will then be able to be boiled down to the base SI units, and can then be converted into the desired unit.

PaddiM8 commented 4 years ago

Hm yes, providing the SI units is probably a good idea.

matu3ba commented 3 years ago

Hey @PaddiM8 , you can likely just use parts of rink or ping the maintainer. The project is specialised on unit conversion. You project looks to have more focus on math formula stuff with variables, sums etc.

jhugman commented 3 years ago

Not sure if this helps. I wrote the guts of https://turo.io : a calculator/notepad that supports units.

I saw this issue, so thought I'd write down some things that I learned or wish I'd learned earlier. I hope it's helpful.

I haven't a lot of bandwidth at the moment to help, but I wish you well in your endeavour.

PaddiM8 commented 3 years ago

@jhugman Thank you so much, this is incredibly valuable! This will definitely help when I find the time to do proper units!

ghost commented 3 years ago

As a sidenote, I would also suggest looking at qalc for ideas on how to handle this feature.

ad-si commented 2 years ago

And I would suggest looking at https://github.com/sharkdp/insect, which also has very good unit support 😁