Open joshcol9232 opened 4 years ago
Yeah, I want this too. I'm currently exploring various ways of implementing this. Thanks for the suggestion!
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
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
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 ;) ).
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!
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:
unit C = K + 273
, where K is the SI unit Kelvin. Or another example, unit miles = m * 1000 * 1.609344
where m
is the SI unit.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.
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.
Hm yes, providing the SI units is probably a good idea.
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.
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.
unit 1 mph = 1 mile/hour, Speed
1s = 1000ms
, 1 fortnight = 14 days
) between units seem to work for 95% of the time. It makes the maths /much/ easier if you can depend upon it. It becomes possible to do arbitrarily complicated unit conversions. I never worked out how to deal with:
dB
get weird when combining with other units e.g. dB/m^2
1 lb/°F in kg/°C
are similarly weird.Let's Write A CAS
.12 inch to feet
converts 12 inches to m
then m to feet
) simplifies things hugely. It's not the end of the world if you can't depend on the accuracy of your numbers, but you'll need to eat the complexity of avoiding accumulating errors.1 lb + 1 kg
, 5 miles / 10 seconds
, sqrt(1 acre)
, sin(pi/4 radians)
. Freeing the user from /when/ to convert as well as /how/ to convert becomes magical.$ x
== x $
and (x + y) m
), with a precedence between *
(explicit multiplication) and ^
(raised to the power of).I haven't a lot of bandwidth at the moment to help, but I wish you well in your endeavour.
@jhugman Thank you so much, this is incredibly valuable! This will definitely help when I find the time to do proper units!
As a sidenote, I would also suggest looking at qalc for ideas on how to handle this feature.
And I would suggest looking at https://github.com/sharkdp/insect, which also has very good unit support 😁
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 :)