halogenandtoast / alchemist

A scientific conversion library.
155 stars 28 forks source link

Weird conversions #28

Closed astratto closed 11 years ago

astratto commented 11 years ago

Hi! Maybe I'm missing something, but I get this weird conversion:

2.0.0p247 :014 > 1.kg.to.kg
 => #<Alchemist::Measurement:0x007fbe66c34d68 @value=1000.0, @unit_name=:g, @exponent=1000.0>
2.0.0p247 :015 > 1.kg.to.kg.to_i
 => 1000
halogenandtoast commented 11 years ago

I'll look into this, it's likely I missed some exponent behavior in the 0.1.6 release.

astratto commented 11 years ago

Great! At first I thought it was something related to conversions from A to A when the exponent is > 1.0, but there must be something wrong with the exponents in general:

2.0.0p247 :006 > 1.dl
 => #<Alchemist::Measurement:0x007f9155672058 @value=1.0, @unit_name=:l, @exponent=0.1>
2.0.0p247 :007 > 1.liter
 => #<Alchemist::Measurement:0x007f915567ab90 @value=1.0, @unit_name=:liter, @exponent=1.0>
2.0.0p247 :008 > 1.cup.to.dl.value
 => 0.2365882
2.0.0p247 :009 > 1.cup.to.liter.value
 => 0.2365882
halogenandtoast commented 11 years ago

This is partially fixed on master. I have some work ahead of me for making some changes but essentially there is a notable difference between #value and #to_f on Measurement. #value will give you the value unaffected by the exponent, whereas #to_f will be affected by the exponent. For example:

irb(main):009:0> 1.km.value
=> 1.0
irb(main):010:0> 1.km.to_f
=> 1000.0

Essentially to_f always gives you the value in terms of the base unit e.g. meter for kilometer. In the future I plan to make this more transparent, but that requires a few changes internally.