Giter Club home page Giter Club logo

Comments (13)

PaddiM8 avatar PaddiM8 commented on May 17, 2024 7

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

from kalker.

PaddiM8 avatar PaddiM8 commented on May 17, 2024 4

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

from kalker.

PaddiM8 avatar PaddiM8 commented on May 17, 2024 3

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

from kalker.

PaddiM8 avatar PaddiM8 commented on May 17, 2024

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

from kalker.

joshcol9232 avatar joshcol9232 commented on May 17, 2024

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 ;) ).

from kalker.

PaddiM8 avatar PaddiM8 commented on May 17, 2024

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!

from kalker.

joshcol9232 avatar joshcol9232 commented on May 17, 2024

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:

  • Provide the base SI units (which are the standard units that most other scientific units are derived from, see: https://en.wikipedia.org/wiki/SI_base_unit), which is only 7 units so should be fine in terms of clashes.
  • Units should be defined in terms of SI units. For example, degrees celsius would be 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.
  • During calculation, convert all units into SI units, then once finished convert the answer back into the user's desired unit. This makes it absolutely certain that all scientific equations people may use work fine and produce the correct output.

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.

from kalker.

joshcol9232 avatar joshcol9232 commented on May 17, 2024

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.

from kalker.

matu3ba avatar matu3ba commented on May 17, 2024

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.

from kalker.

jhugman avatar jhugman commented on May 17, 2024

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.

  • add a user-definition language, preferably that the user can re-define the built in one. User-defined sets sound fab!
  • Start at dimensions, and build everything on top of that. Being able to reason about units is almost impossible if you don't.
  • Naming dimensions should be part of the definition language. e.g. unit 1 mph = 1 mile/hour, Speed
  • Simple ratios (e.g. 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:
    • non-linear units e.g. dB get weird when combining with other units e.g. dB/m^2
    • linear units with an offset e.g. temperature. Complex conversions like 1 lb/°F in kg/°C are similarly weird.
  • I love the idea of finding the inverse of a unit definition, but I think this ends in Let's Write A CAS.
  • If you can depend on the number representation's accuracy, converting into and out of a single unit per dimension (e.g. 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.
  • Make every operation unit aware; each familty of operations has its own rules: e.g. 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.
  • In the parser, treat the unit as any other term. I wish I'd discovered this earlier. (e.g. $ 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.

from kalker.

PaddiM8 avatar PaddiM8 commented on May 17, 2024

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

from kalker.

 avatar commented on May 17, 2024

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

from kalker.

ad-si avatar ad-si commented on May 17, 2024

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

from kalker.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.