2
我有這樣的代碼:Groovy和斯波克:toDouble VS toFloat
static def parseString(String inputRow, Particle particle) {
def map = inputRow.split()
particle.mass = map[0].toDouble()
particle.x = map[1].toDouble()
particle.y = map[2].toDouble()
}
而這種測試代碼:
static final inputRow = "1 -5.2 3.8"
def particle1 = new Particle()
def "string should be parsed into particles"() {
when:
RepulsionForce.parseString(inputRow, particle1);
then:
particle1.mass == 1
particle1.x == -5.2
particle1.y == 3.8
}
上述測試通過原樣;然而,當我改變了parseString代碼,代碼如下:
static def parseString(String inputRow, Particle particle) {
def map = inputRow.split()
particle.mass = map[0].toFloat()
particle.x = map[1].toFloat()
particle.y = map[2].toFloat()
}
同樣的試驗失敗,此錯誤:
Condition not satisfied:
particle1.x == -5.2
| | |
| | false
| -5.2
[email protected]
http://floating-point-gui.de/ –
嘗試:'particle1.x == -5.2f' –
如果有人可以解釋downvote ,我可以在未來提出更好的問題。謝謝! –