本文整理汇总了Scala中org.scalactic.TolerantNumerics类的典型用法代码示例。如果您正苦于以下问题:Scala TolerantNumerics类的具体用法?Scala TolerantNumerics怎么用?Scala TolerantNumerics使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了TolerantNumerics类的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Scala代码示例。
示例1: BooleanExpressionTest
//设置package包名称以及导入依赖的类
package kokellab.utils.grammars
import org.scalatest.{Matchers, PropSpec}
import org.scalatest.prop.TableDrivenPropertyChecks
import org.scalatest.prop.Tables.Table
import org.scalactic.TolerantNumerics
class BooleanExpressionTest extends PropSpec with TableDrivenPropertyChecks with Matchers {
val doubleEq = TolerantNumerics.tolerantDoubleEquality(1e-4f)
property(s"Should work, damn it") {
BooleanRealNumberGrammar.eval("5*sin(50+sqrt(10)) - min(5, 10, 15, 20) = -3", 0.01) should equal (false)
BooleanRealNumberGrammar.eval("5*sin(50+sqrt(10)) - min(5, 10, 15, 20) ? -3.788", 0.01) should equal (true)
BooleanRealNumberGrammar.eval("5*sin(50+sqrt(10)) - min(5, 10, 15, 20) ? -3.788", 0.01) should equal (false)
BooleanRealNumberGrammar.eval("(15+20) < 50", 0.01) should equal (true)
BooleanRealNumberGrammar.eval("(15-20) > -50", 0.01) should equal (true)
BooleanRealNumberGrammar.eval("(15-20) > -50 and (50*2)=100", 0.01) should equal (true)
BooleanRealNumberGrammar.eval("5 = 5 = 5", 0.01) should equal (true)
BooleanRealNumberGrammar.eval("5 = 5 = 6", 0.01) should equal (false)
BooleanRealNumberGrammar.eval("5 < 6 < 7", 0.01) should equal (true)
BooleanRealNumberGrammar.eval("5 > 20 < 15", 0.01) should equal (false)
BooleanRealNumberGrammar.eval("5 < 10 = 10", 0.01) should equal (true)
BooleanRealNumberGrammar.eval("2=0 or 50=50 and ((50*2)=100)", 0.01) should equal (true)
BooleanRealNumberGrammar.eval("(2=0 or 50=50) and ((50*2)=100)", 0.01) should equal (true)
BooleanRealNumberGrammar.eval("0=1 or 2<5 and 5=5", 0.01) should equal (true) // tests precedence
BooleanRealNumberGrammar.eval("sqrt(10) < mean(10, 50, 60) nand 50=50", 0.01) should equal (false)
BooleanRealNumberGrammar.eval("sqrt(10) < mean(10, 50, 60) and 50=50", 0.01) should equal (true)
BooleanRealNumberGrammar.eval("sqrt(10) >= mean(10, 50, 60) or 50=50", 0.01) should equal (true)
BooleanRealNumberGrammar.eval("sqrt(10) >= mean(10, 50, 60) nor 50=50", 0.01) should equal (false)
}
}
示例2: RealNumberGrammarTest
//设置package包名称以及导入依赖的类
package kokellab.utils.grammars
import org.scalatest.{Matchers, PropSpec}
import org.scalatest.prop.TableDrivenPropertyChecks
import org.scalatest.prop.Tables.Table
import org.scalactic.TolerantNumerics
class RealNumberGrammarTest extends PropSpec with TableDrivenPropertyChecks with Matchers {
val doubleEq = TolerantNumerics.tolerantDoubleEquality(1e-4f)
property(s"Should work, damn it") {
RealNumberGrammar.eval("5*sin(50+sqrt(10)) - min(5, 10, 15, 20) + mean(10, 20)") should equal (11.211799096658236)
}
property("Exp notation") {
RealNumberGrammar.eval("-5.0E10 + (2E10) + (1E-2) + (3E+5)") should equal (-2.999969999999E10)
}
property("NaN") {
assert(RealNumberGrammar.eval("-NaN*5").isNaN)
}
property("Inf") {
assert(RealNumberGrammar.eval("-?").isNegInfinity)
}
}
示例3: TestPerfectScores
//设置package包名称以及导入依赖的类
package com.github.jongwook
import org.apache.spark.SparkConf
import org.apache.spark.sql.SparkSession
import org.scalactic.{Equality, TolerantNumerics}
import org.scalatest.{FlatSpec, Matchers}
class TestPerfectScores extends FlatSpec with Matchers {
import TestFixture._
implicit val doubleEquality: Equality[Double] = TolerantNumerics.tolerantDoubleEquality(eps)
val perfectScores: Seq[(String, Map[Metric, Seq[Double]])] = {
val spark = SparkSession.builder().master(new SparkConf().get("spark.master", "local[8]")).getOrCreate()
import spark.implicits._
val predictionDF = spark.createDataset(prediction)
val groundTruthDF = spark.createDataset(groundTruth)
for ((name, df) <- Seq("prediction" -> predictionDF, "ground truth" -> groundTruthDF)) yield {
val metrics = new SparkRankingMetrics(df, df, itemCol = "product", predictionCol = "rating")
name -> Map[Metric, Seq[Double]](
NDCG -> metrics.ndcgAt(ats),
MAP -> metrics.mapAt(ats),
Precision -> metrics.precisionAt(Seq(Integer.MAX_VALUE)),
Recall -> metrics.recallAt(Seq(Integer.MAX_VALUE))
)
}
}
for ((name, scores) <- perfectScores) {
for (metric <- Seq(NDCG, MAP, Precision, Recall)) {
s"In $name dataset, our $metric implementation" should s"return 1.0 for the perfect prediction" in {
for (score <- scores(metric)) {
score should equal(1.0)
}
}
}
}
}
示例4: WeightModelSpec
//设置package包名称以及导入依赖的类
package ch.fhnw.ima.saav.model
import ch.fhnw.ima.saav._
import org.scalactic.TolerantNumerics
import org.scalatest.{FunSpec, Matchers}
class WeightModelSpec extends FunSpec with Matchers {
it("should calculate weighted means") {
val valuesWithWeights = Seq((13d, 0.1), (23d, 0.03), (54d, 0.04))
val weightedMean = weight.weightedMean(valuesWithWeights)
implicit val doubleEquality = TolerantNumerics.tolerantDoubleEquality(0.01)
weightedMean match {
case Some(mean) => assert(mean === 24.418)
case wrongValue @ _ => fail(s"Unexpected mean value $wrongValue")
}
}
it("should handle empty value collection") {
weight.weightedMean(Seq.empty) shouldBe None
}
}
示例5: RoomAreaSpec
//设置package包名称以及导入依赖的类
package E07
import fi.kajstrom.efpscala.E07.RoomArea
import org.scalactic.TolerantNumerics
import org.scalatest.{FlatSpec, Matchers}
class RoomAreaSpec extends FlatSpec with Matchers{
implicit val doubleEq = TolerantNumerics.tolerantDoubleEquality(0.001)
val room = new RoomArea(15, 20)
"areaFeet" should "return 300 when size is 15 x 20 feet" in {
room.areaFeet() should be (300)
}
"areaMeters" should "return 27.871 when size is 15 x 20 feet" in {
assert(room.areaMeters() === 27.871)
}
}
示例6: Vector3DSpec
//设置package包名称以及导入依赖的类
package name.ryanboder.maestroid
import org.scalactic.TolerantNumerics
import org.scalatest.FlatSpec
import scala.math._
class Vector3DSpec extends FlatSpec {
implicit val doubleEquality = TolerantNumerics.tolerantDoubleEquality(0.01)
behavior of "Vector3D"
it should "calculate magnitude" in {
val subject = Vector3D(1.0, 1.0, 1.0)
assert(subject.magnitude === sqrt(3))
}
it should "calculate the dot product of 2 vectors" in {
val a = Vector3D(1.0, 1.0, 1.0)
val b = Vector3D(-1.0, -1.0, -1.0)
val c = Vector3D(1.0, 1.0, 0.0)
val d = Vector3D(1.0, -1.0, 0.0)
assert((a dot a) === 3.0)
assert((a dot b) === -3.0)
assert((c dot d) === 0.0)
}
it should "calculate the angle between 2 vector" in {
val a = Vector3D(1.0f, 1.0f, 1.0f)
val c = Vector3D(1.0f, 1.0f, 0.0f)
val d = Vector3D(1.0f, -1.0f, 0.0f)
assert((a angle a) === 0.0)
assert((c angle d) === Pi / 2.0)
}
}