josdejong / mathjs

An extensive math library for JavaScript and Node.js
https://mathjs.org
Apache License 2.0
14.35k stars 1.24k forks source link

cgs unit system #1927

Open chongchonghe opened 4 years ago

chongchonghe commented 4 years ago

Hi, thank you for your awesome math library!

I am thinking about taking advantage of this library and build a calculator for physicists and astronomers. Since cgs unit system is still widely used in the astronomy community, I'd like to help to add a cgs unit system to this library.

I found UNIT_SYSTEMS.cgs but didn't find a .toCGS() function. Here's what I plan to do:

  1. Complete UNIT_SYSTEMS.cgs: add a missing basic unit 'Ba = g/(cm⋅s2)'; implement the ESU units system for Electromagnetics.
  2. Define Unit.prototype.toCGS: copy Unit.prototype.toSI and change every instance of UNIT_SYSTEMS.si to UNIT_SYSTEMS.cgs.

Please let me know what do you think, or what is already in math.js that I missed.

josdejong commented 4 years ago

Sounds good, thanks for your suggestions ChongChong.

@ericman314 what do you think?

ericman314 commented 4 years ago

Sounds good to me too, but I wonder if an alternative could be to have a generically named function like .toSystem(string) which converts to any named system?

josdejong commented 4 years ago

That's a good idea 👍 , would be nice to see whether it is doable to implement a more generic solution. What do you think @chongchonghe?

chongchonghe commented 4 years ago

Is there any other unit system other than SI, CGS, and US?

We can simply link .toSystem('SI') to .toSI() and .toSystem('cgs') to .toCGS(). The first step is to complete the CGS system. I'll be working on that soon and make pull requests once finished.

ericman314 commented 4 years ago

I don't think there are any other systems. Some day when I finish UnitMath I'll add your toSystem function too, so we should make sure that we can accommodate custom unit systems if the user has defined any.

josdejong commented 4 years ago

Some day when I finish UnitMath

Still looking forward to that day 😎 👍