joshwlewis / unitwise

Physical quantity and units of measure conversion and math for Ruby
unitwise.org
MIT License
282 stars 32 forks source link

Conversion from ratio to percentage gives strange results #61

Open sergioisidoro opened 5 years ago

sergioisidoro commented 5 years ago

I have some numbers which are a ratios and is by definition unitless.

However I need to convert those ratios to percentages (multiply by 100), so I tried to "fake" the ratio unit by using 0.35 100%

 Unitwise(0.35, "100%").convert_to("%")
 => #<Unitwise::Measurement value=78812993478983675/2251799813685248 unit=%> 

This result happens to be correct,

78812993478983675 / 2251799813685248 = 35

But it's a quite inconvenient value.

This also happens if I try to create a unit for ratio

Unitwise.register(
  names: ["ratio"],
  symbol: ":",
  primary_code: "ratio",
  secondary_code: "ratio",
  scale: {
    value: 100,
    unit_code: '%'
  },
  property: 'ratio'
)

Unitwise(0.35, ":").convert_to("%").value
 => (78812993478983675/2251799813685248)

Of course it's just multiplying by 100, but in my case the situation is a bit more complicated with the internals of the work I'm making.

sergioisidoro commented 5 years ago

Another issue found:

Unitwise(10, "mg/(24.h)").convert_to("g/(24.h)").to_f
0.0100000000224