Skip to content

Suggestion: define conversions as rational values #478

Closed
@R2D221

Description

@R2D221

I recently tried to do a simple conversion between US gallons and oil barrels. The conversion is simple, you just need to multiply or divide by 42. However, with this library, the result is the following:

Volume.FromOilBarrels(1).UsGallons == 42.0000197938929

I have already read the Precision page, so I understand how the conversions are made and how it's not the first priority. However, I want to post this as a suggestion.

Instead of defining the units as a double, they could be defined as rational.

How would this work: Each conversion should have the numerator and denominator. This leaves things almost unchanged for e.g. meters to kilometers. But for conversions between metric and imperial systems, it would look as follows:

So, by storing 473176473, 125000000000 and 9936705933, 62500000000 as the conversion values for gallons and barrels, you can then convert between them like so:

var gallons = barrels * 125000000000.0 * 9936705933.0 / 473176473.0 / 62500000000.0

If you input an integer, it will give you an exact multiple of 42.


Of course, this isn't exclusive to gallons, but pretty much any conversion between imperial and metric systems would benefit from this, especially since most if not all of these units are defined in metric terms anyway (like an inch being exactly 0.0254 meters).

I know maybe this isn't in your plans, but I think the system could internally be adapted to make the calculations like this while improving precision. On the other hand, I don't know the performance impact of this, but I don't think it would be that much of a difference.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions