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

Add scalar angle multiplication #815

Merged
merged 2 commits into from
Oct 2, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions modules/core/shared/src/main/scala/lucuma/core/math/Angle.scala
Original file line number Diff line number Diff line change
Expand Up @@ -300,6 +300,20 @@ object Angle extends AngleOptics {
def -(a: Angle): Angle =
Angle.fromMicroarcseconds(toMicroarcseconds - a.toMicroarcseconds)

/**
* Scalar multiplication of this angle and and natural number `a`. Exact
* @group Operations
*/
def *(a: Long): Angle =
Angle.fromMicroarcseconds(toMicroarcseconds * a)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unfortunately this overflows when a gets large. You may need to go to BigDecimal and renormalize it back into 0..360° before constructing the return value.

scala> Angle.Angle90 * 4000000
val res14: lucuma.core.math.Angle = 0
                                                                                                                                                                            
scala> Angle.Angle90 * 40000000
val res15: lucuma.core.math.Angle = 230290448384

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks I pushed a new commit doing as you suggest and some tests


/**
* Scalar multiplication of this angle and double precission number `a`. Approximate
* @group Operations
*/
def *?(a: Double): Angle =
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I made it *? to denote that is approximate. not sure if it is a good idea, WDYT?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We just document it when it's inexact. Pretty much anything involving Double is inexact by definition.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I reverted to *

Angle.fromMicroarcseconds((toMicroarcseconds * a).toLong)

/**
* This angle as an Offset.P. Exact, invertible.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -182,4 +182,23 @@ final class AngleSuite extends munit.DisciplineSuite {
assertEquals(a.tan, scala.math.tan(a.toDoubleRadians))
}
}

test("Scalar int multiplication") {
assertEquals(Angle.Angle0 * 2, Angle.Angle0)
assertEquals(Angle.Angle90 * 2, Angle.Angle180)
assertEquals(Angle.Angle90 * 4, Angle.Angle0)
assertEquals(Angle.Angle90 * -2, Angle.Angle180)
assertEquals(Angle.Angle90 * 10, Angle.Angle180)
}

test("Scalar double multiplication") {
assertEquals(Angle.Angle0 *? 2, Angle.Angle0)
assertEquals(Angle.Angle90 *? 2, Angle.Angle180)
assertEquals(Angle.Angle90 *? 4, Angle.Angle0)
assertEquals(Angle.Angle90 *? -2, Angle.Angle180)
assertEquals(Angle.Angle90 *? 10, Angle.Angle180)
assertEquals(Angle.Angle180 *? 0.5, Angle.Angle90)
assert(((Angle.Angle180 *? 0.001) - Angle.fromDoubleDegrees(0.18)).toMicroarcseconds < 1000)
assert(((Angle.Angle180 *? -0.001) - Angle.fromDoubleDegrees(359.82)).toMicroarcseconds < 1000)
}
}
Loading