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

cleaner typeclass instances for Fractional #1306

Closed
erikerlandson opened this issue Dec 16, 2023 · 1 comment
Closed

cleaner typeclass instances for Fractional #1306

erikerlandson opened this issue Dec 16, 2023 · 1 comment

Comments

@erikerlandson
Copy link
Contributor

erikerlandson commented Dec 16, 2023

I need a clean way to express the following ideas for spire types, via typeclasses:

  1. Some types are "fractional" - they can approximate all their answers "closely" (for some value of "close") in a fractional way:
  • Float
  • Double
  • Rational
  • BigDecimal
  • Algebraic
  • Real
  1. for all native and spire types that are fractional, one can compute a "good" approximation (again, for some value of "good") for fpow and nroot

I think a clean way to do this would be to simply define actual typeclass instances of Fractional for all these types. That gives library users a clean way to say either using NRoot or using Fractional in their signatures.

Currently spire provides literally no usable typeclass instance of NRoot[Rational], even though it provides instances for several integral types like NRoot[Int], which doesn't seem numerically safe at all!

There are also currently no typeclass instances that allow one to "filter" on numeric types that actually are fractional. I should be able to say things like using Fractional[V] and have this be satisfiable for Float, Double, Rational, BigDecimal, etc. Currently that doesn't seem possible.

@erikerlandson
Copy link
Contributor Author

This should give me what I want, thanks @armanbilge

implicit final val FloatIsFractional: Fractional[Float] = new FloatIsFractional
implicit final val DoubleIsFractional: Fractional[Double] = new DoubleIsFractional
implicit final val BigDecimalIsFractional: Fractional[BigDecimal] = new BigDecimalIsFractional
implicit final val AlgebraicIsFractional: Fractional[Algebraic] = new AlgebraicIsFractional
implicit final val NumberIsFractional: Fractional[Number] = new NumberIsFractional
implicit final val RationalIsFractional: Fractional[Rational] = new RationalIsFractional

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

1 participant