- About Scala
- In the Enterprise
- Scala Community
- Language Research
- In the Press
- The Scala Team
- Scala's Prehistory
- Contact Us
- Learning Scala
- Tour of Scala
- Scala API
- Setup & Getting Started
- Programming Guides
- Other Guides
- Code Examples
- Scala Developers
Improving Numeric's division operation
Wed, 2011-10-26, 07:14
What are the issues and options for improving scala.math.Numeric's
1. The complaint that comes up over and over is that Numeric doesn't
define division, ie /. Instead, you have to use subtypes Integral (for
integer floor division via 'quot') or Fractional (for regular division
Could and should division be lifted up to the Numeric level?
+ The same type signature, (T, T) => T fits both styles of division.
- An existing implementation of Numeric, outside the core library,
would break if an abstract division method were added to Numeric. Is
that acceptable in a version jump like 2.9.x => 2.10.x or similar?
Alternately, is it feasible to define a default implementation for the
trait? I can't think of one.
Some lesser issues around division:
2. I think the following operation makes sense (conceptually):
Integral / Integral => Fractional. After all, this is the /definition/
of Rational numbers: the ratio of two integers. Is there a way to
support this kind of operation on an Integral typeclass of some
(sub)type? It seems to imply parametrizing the Integral type with an
associated Fractional type.
3. For integral division, should the fundamental division operation be
(T, T) => (T, T), returning both quotient and remainder in one step?
It seems inefficient to compute the same division twice if you need
both values, although probably only significant for tight loops &/
huge integer values.