我有以下用于计算欧几里得距离的代码:
distanceBetween(first:(Double,Double), second:(Double,Double)): Double = {
math.sqrt((first._1 - second._1) + (first._2 - second._2)) //distance formula
}
当我在 Scala 解释器中运行它时,我得到了这个结果:
distanceBetween((0.0,0.0),(20.0,20.0))
res0: Double = NaN
谁能解释我为什么会得到这个结果?
编辑:
根据 Patashu 和 ntalbs,两点之间欧几里得距离的正确代码是:
distanceBetween(first:(Double,Double), second:(Double,Double)): Double = {
scala.math.sqrt( scala.math.pow(second._1 - first._1, 2) + scala.math.pow(second._2 - first._2, 2) ) //distance formula
}