Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Conversion between m/s^2 and EarthGravities possibly invalid #103

Closed
maciej-work opened this issue Aug 20, 2015 · 2 comments
Closed

Conversion between m/s^2 and EarthGravities possibly invalid #103

maciej-work opened this issue Aug 20, 2015 · 2 comments
Labels

Comments

@maciej-work
Copy link

scala> val ok = 10.metres / second / second
ok: squants.motion.Acceleration = 10.0 m/s²

scala> val o2k = EarthGravities(10.metres / second / second)
o2k: squants.motion.Acceleration = 10.0 g

EarthGravities(ok)
res8: squants.motion.Acceleration = 10.0 g

Shouldn't this result in ~1g ? I've only just picked up the library just now, please excuse me if I'm misunderstanding something fundamental here.

@garyKeorkunian
Copy link
Contributor

It looks like the issue here is an implicit Numeric type class is in scope for Acceleration and it's converting the value to Double in order to initialize the EarthGravity quantity.

Normally you shouldn't be able to initialize EarthGravity (or any specific unit) with an another quantity value (ok is an Acceleartion). However, the AccelerationNumeric is in scope in the console so it is converting the "value" portion of your quantity. Therefore,

EarthGravities(10.meters / second / second) == 10.g  
// the "10m/s²" is being implicitly converted to 10d so you get 
EarthGravities(10d) == 10.g

I think what you are trying to do is convert some acceleration quantity from m/s² to g. To do that, use the in or to method as follows:

val mpss = 10.meters / second / second
mpss: squants.motion.Acceleration = 10.0 m/s²

val g = mpss in EarthGravities // Converts to a new Quantity in EarthGravity units
g: squants.motion.Acceleration = 1.0197162129779282 g

val d = mpss to EarthGravities // Extracts the numeric value of the quantity in EarthGravities
d: Double = 1.0197162129779282

Of course implicit conversions are a common source of confusion in libraries, and in this case seems to allow one of the things the library is intending stop: mixing up when to use plain numerics and when to use quantities.

The numeric conversion for quantities has some limited value. For example, it allows us to use the sum method on a list of quantities. However, as this issue points out, automatically importing it in the console could lead to incongruent expressions.

I will consider another approach. Thanks for bringing this up. I hope my suggestion above gets you what you need.

@garyKeorkunian
Copy link
Contributor

garyKeorkunian commented Apr 7, 2017

The issue described here is that the implicit numerics for each quantity allow user code to create quantities of any type and initialize them with another type. The implicit converts the initialization quantity to a number which then is used to seed the new quantity. For example,

scala> val length = Meters(10)
length: squants.space.Length = 10.0 m
scala> val area = SquareMeters(length)
area: squants.space.Area = 10.0

In the code above, when initializing SquareMeters with length, the implicit LengthNumeric is transforming from Length 10.0 m to Double 10.0. It is this value that is initializing the area resulting in what seems to be an invalid dimensional conversion.

This can cause confusion and should be prevented by the library.

The work on generics in issue #15 should address this as the Quantity factories would no longer use Numeric type classes for seeded values, but new Squants specific type classes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants