bjornbm / dimensional

Dimensional library variant built on Data Kinds, Closed Type Families, TypeNats (GHC 7.8+).
BSD 3-Clause "New" or "Revised" License
102 stars 15 forks source link

Can't apply SI prefixes to electronVolt ? #175

Closed arekfu closed 7 years ago

arekfu commented 7 years ago

I am not sure I am using this package correctly, but:

λ> :m Numeric.Units.Dimensional.Prelude Numeric.Units.Dimensional.NonSI 
λ> let en = 100 *~ (mega electronVolt) :: Energy Double
λ> show en
"*** Exception: The resulting conversion factor is not an exact integer.
λ> showIn joule en
"*** Exception: The resulting conversion factor is not an exact integer.

Is there any solution?

dmcclean commented 7 years ago

This is a legitimate bug that is fixed in master but not in the released version.

I will create a maintenance release. Thanks for reporting.

dmcclean commented 7 years ago

Actually I was incorrect, this isn't even fixed in master, there are two issues that combine to make this, only one of which is fixed in master. Working on a fix.

dmcclean commented 7 years ago

Wow. This is surprisingly tricky to get right while maintaining both the polymorphism we have now and the fact that conversion factor arithmetic is done exactly for as long as possible and then rounded at the last minute. I'm starting to wonder if there is a solution that doesn't require either overlapping instances and some ad hoc typeclass or a really zany type for mega and friends. I need to think about this one some more tomorrow. Great test case, thanks.

In a nutshell the issue is that we want to be able to apply mega and friends to things using only a Num constraint, because after all it only involves a multiplication. But we also want to compute the conversion factor using exact arithmetic and only convert it to, say, Double once we have constructed some large unit expression.

dmcclean commented 7 years ago

Prepared a maintenance release for this but won't be deploying it until I can review it with fresh eyes in the morning. If it helps you to have it before that, use it from here: https://github.com/dmcclean/dimensional/tree/fix-175.

arekfu commented 7 years ago

Thanks a lot. It seems to work here.

dmcclean commented 7 years ago

Sure thing. Thanks again for the report, it's much appreciated.

Fixed in 1.0.1.3 which just hit hackage.