gentooboontoo / js-quantities

JavaScript library for quantity calculation and unit conversion
http://gentooboontoo.github.io/js-quantities/
MIT License
396 stars 102 forks source link

Issue with conversion from mmol/L to mg/dL. #69

Open sunnykgupta opened 8 years ago

sunnykgupta commented 8 years ago

mmol/l = mg/dl / 18 mg/dl = 18 × mmol/l

Trying to make a medical calculator with js-quantities. But can't seem to convert between the two frequently used units to measure blood sugar.

Anything I'm missing?

I tried - Qty.swiftConverter('mmol/l', 'mg/dl');

Results in : "QtyError", "Incompatible units"

Thanks in advance.

gentooboontoo commented 8 years ago

This error is expected. mmol/l and mg/dl are not dimensionally compatible (the kind of the former is a molar concentration and the latter is density), so they cannot be converted to each other.

In specific domain like yours, it could be convertible because some units are assimilated to others. In your case, mol is considered as a mass (the mass of 1 mole of glucose).

I don't know yet how those very domain-specific cases could be handled.

sunnykgupta commented 8 years ago

Thanks. I'll implement my functions separately. 😃

luzlab commented 7 years ago

In the case of mmol/l to mg/dl conversion, the missing step that's assumed is multiplying by the molecular weight of glucose g/mol.

Does js-quantities handle units during multiplication and division?

If so, would this work to do this conversion? 'Qty(' mmol / l').mul(Qty('180.16 g / mol')).to('mg / dl');'

gentooboontoo commented 7 years ago

Yes it should work as js-quantities handles units multiplication and division (but it seems to be a small rounding error here, I will try to track it down).