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

Implementing the Degrees (angle) unit #322

Closed
imclem opened this issue Jan 5, 2021 · 2 comments
Closed

Implementing the Degrees (angle) unit #322

imclem opened this issue Jan 5, 2021 · 2 comments

Comments

@imclem
Copy link

imclem commented Jan 5, 2021

Hi there,

First of all, thank you for this library, it's a real joy to use!

I'm trying to add a new type "Degree" to represent an angle in degrees.

If I define the unit like this:

final Unit<Angle> DEGREE_ANGLE = Units.RADIAN.multiply(180 / Math.PI);

assertEquals(57.2958, Quantities
        .getQuantity(1, Units.RADIAN)
        .to(Metrics.DEGREE_ANGLE)
        .getValue
        .doubleValue());

This does not produce the result that I expect (not even close), I get the following double value: 0.017453292519943295.

I also tried defined the unit like this:

final Unit<Angle> DEGREE_ANGLE = Units.RADIAN.multiply(180).divide(Math.PI);

But it produce the same result.

Now if I define the degree unit like this (thanks #257):

  final Unit<Angle> REVOLUTION  = Units.RADIAN.multiply(2.0 * Math.PI);
  final Unit<Angle> DEGREE_ANGLE = REVOLUTION.divide(360.0);

assertEquals(57.29577951308232, Quantities
        .getQuantity(1, Units.RADIAN)
        .to(Metrics.DEGREE_ANGLE)
        .getValue
        .doubleValue());

Everything works properly.

I tried debugging the converter method, but can't find why the first implementation is not working.

Why the first implementation is not working ? And is there a way to make it work ?

Thanks in advance

keilw added a commit to unitsofmeasurement/si-units that referenced this issue Jan 6, 2021
@keilw
Copy link
Member

keilw commented Jan 6, 2021

Comparing the double values is not always a good idea, but why don't you use DEGREE_ANGLE in the NonSI unit system implementation from https://github.com/unitsofmeasurement/si-units?
There you will also find how it's done which is closer to the first approach but using precise enough values including the Rational number type instead of double or other primitives.
You'll also find JUnit tests for it. I close this here, if you found any problems with the SI units or suggestions for improvements, please create a new ticket there.

@keilw keilw closed this as completed Jan 6, 2021
@imclem
Copy link
Author

imclem commented Jan 6, 2021

@keilw I don't use it because I didn't know it existed...
I will use it, thanks a lot !

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

No branches or pull requests

2 participants