Skip to content

Commit

Permalink
Merge pull request #10355 from liang3zy22/tastyerror
Browse files Browse the repository at this point in the history
Fix tasty/TestLogarithms.scala failed in some platform
  • Loading branch information
bishabosha committed Mar 28, 2023
2 parents 8269bdd + 2cbff24 commit 9fb1419
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 8 deletions.
8 changes: 4 additions & 4 deletions test/tasty/run/src-2/tastytest/TestLogarithms.scala
Expand Up @@ -4,10 +4,10 @@ import Logarithms._

object TestLogarithms extends Suite("TestLogarithms") {

val Some(l1) = Logarithm.of(2)
val Some(l2) = Logarithm.of(3)
val Some(l1) = Logarithm.of(10)
val Some(l2) = Logarithm.of(100)

test(assert((l1 + l2).toDouble == 4.999999999999999))
test(assert((l1 * l2).toDouble == 6.0))
test(assert((l1 + l2).toDouble == 109.99999999999997))
test(assert((l1 * l2).toDouble == 1000.0))

}
10 changes: 6 additions & 4 deletions test/tasty/run/src-3/tastytest/Logarithms.scala
@@ -1,5 +1,7 @@
package tastytest

import java.lang.StrictMath

object Logarithms {

opaque type Logarithm = Double
Expand All @@ -8,16 +10,16 @@ object Logarithms {

// These are the two ways to lift to the Logarithm type

private[Logarithms] def apply(d: Double): Logarithm = math.log(d)
private[Logarithms] def apply(d: Double): Logarithm = StrictMath.log10(d)

def of(d: Double): Option[Logarithm] =
if (d > 0.0) Some(math.log(d)) else None
if (d > 0.0) Some(StrictMath.log10(d)) else None
}

// implicit define cross compatible public APIs for opaque types
final implicit class LogarithmOps(val logarithm: Logarithm) extends AnyVal {
def toDouble: Double = math.exp(logarithm)
def + (other: Logarithm): Logarithm = Logarithm(math.exp(logarithm) + math.exp(other))
def toDouble: Double = StrictMath.pow(10.0, logarithm)
def + (other: Logarithm): Logarithm = Logarithm(StrictMath.pow(10.0, logarithm) + StrictMath.pow(10.0, other))
def * (other: Logarithm): Logarithm = logarithm + other
}

Expand Down

0 comments on commit 9fb1419

Please sign in to comment.