forked from typelevel/cats
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbigDecimal.scala
More file actions
31 lines (21 loc) · 1020 Bytes
/
Copy pathbigDecimal.scala
File metadata and controls
31 lines (21 loc) · 1020 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
package algebra
package instances
import java.math.MathContext
import algebra.ring._
package object bigDecimal extends BigDecimalInstances
trait BigDecimalInstances extends cats.kernel.instances.BigDecimalInstances {
implicit val bigDecimalAlgebra: BigDecimalAlgebra = new BigDecimalAlgebra()
}
class BigDecimalAlgebra(mc: MathContext) extends Field[BigDecimal] with Serializable {
def this() = this(MathContext.UNLIMITED)
val zero: BigDecimal = BigDecimal(0, mc)
val one: BigDecimal = BigDecimal(1, mc)
def plus(a: BigDecimal, b: BigDecimal): BigDecimal = a + b
def negate(a: BigDecimal): BigDecimal = -a
override def minus(a: BigDecimal, b: BigDecimal): BigDecimal = a - b
def times(a: BigDecimal, b: BigDecimal): BigDecimal = a * b
def div(a: BigDecimal, b: BigDecimal): BigDecimal = a / b
override def pow(a: BigDecimal, k: Int): BigDecimal = a.pow(k)
override def fromInt(n: Int): BigDecimal = BigDecimal(n, mc)
override def fromBigInt(n: BigInt): BigDecimal = BigDecimal(n, mc)
}