1


package algebra

2


package instances

3



4


import algebra.lattice.DistributiveLattice

5


import algebra.ring.Field

6



7


import java.lang.Math

8



9


trait DoubleInstances extends cats.kernel.instances.DoubleInstances {

10


implicit val doubleAlgebra: Field[Double] =

11

3

new DoubleAlgebra

12



13


// This is not Bounded due to the presence of NaN

14


val DoubleMinMaxLattice: DistributiveLattice[Double] =

15

3

DistributiveLattice.minMax[Double]

16


}

17



18


/**

19


* Due to the way floatingpoint equality works, this instance is not

20


* lawful under equality, but is correct when taken as an

21


* approximation of an exact value.

22


*

23


* If you would prefer an absolutely lawful fractional value, you'll

24


* need to investigate rational numbers or more exotic types.

25


*/

26


class DoubleAlgebra extends Field[Double] with Serializable {

27



28

3

def zero: Double = 0.0

29

3

def one: Double = 1.0

30



31

3

def plus(x: Double, y: Double): Double = x + y

32

3

def negate(x: Double): Double = x

33

3

override def minus(x: Double, y: Double): Double = x  y

34



35

3

def times(x: Double, y: Double): Double = x * y

36

3

def div(x: Double, y: Double): Double = x / y

37

3

override def reciprocal(x: Double): Double = 1.0 / x

38

3

override def pow(x: Double, y: Int): Double = Math.pow(x, y.toDouble)

39



40

3

override def fromInt(x: Int): Double = x.toDouble

41

3

override def fromBigInt(n: BigInt): Double = n.toDouble

42


override def fromDouble(x: Double): Double = x

43


}
