typelevel / algebra
 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 2 ``` new DoubleAlgebra ``` 12 13 ``` // This is not Bounded due to the presence of NaN ``` 14 ``` val DoubleMinMaxLattice: DistributiveLattice[Double] = ``` 15 2 ``` DistributiveLattice.minMax[Double] ``` 16 ```} ``` 17 18 ```/** ``` 19 ``` * Due to the way floating-point 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 2 ``` def zero: Double = 0.0 ``` 29 2 ``` def one: Double = 1.0 ``` 30 31 2 ``` def plus(x: Double, y: Double): Double = x + y ``` 32 2 ``` def negate(x: Double): Double = -x ``` 33 2 ``` override def minus(x: Double, y: Double): Double = x - y ``` 34 35 2 ``` def times(x: Double, y: Double): Double = x * y ``` 36 2 ``` def div(x: Double, y: Double): Double = x / y ``` 37 2 ``` override def reciprocal(x: Double): Double = 1.0 / x ``` 38 2 ``` override def pow(x: Double, y: Int): Double = Math.pow(x, y.toDouble) ``` 39 40 2 ``` override def fromInt(x: Int): Double = x.toDouble ``` 41 2 ``` override def fromBigInt(n: BigInt): Double = n.toDouble ``` 42 ``` override def fromDouble(x: Double): Double = x ``` 43 ```} ```

Read our documentation on viewing source code .