Skip to content

Commit cafcc96

Browse files
committed
- fix comparing floating numbers (double)
1 parent d8f9f8a commit cafcc96

File tree

1 file changed

+19
-24
lines changed

1 file changed

+19
-24
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,34 @@
11
package com.jalgoarena.judge
22

3-
import java.math.RoundingMode
4-
import java.text.DecimalFormat
3+
import com.jalgoarena.type.Pair
54
import java.util.*
6-
import com.jalgoarena.type.*
75

86
class JudgeResultVerifier {
9-
fun isValidAnswer(a: Any?, b: Any?): Boolean {
10-
return equalForObjectsOrArrays(a, b)
7+
fun isValidAnswer(expected: Any?, toCheck: Any?): Boolean {
8+
return equalForObjectsOrArrays(expected, toCheck)
119
}
1210

13-
private fun equalForObjectsOrArrays(a: Any?, b: Any?): Boolean {
11+
private fun equalForObjectsOrArrays(expected: Any?, toCheck: Any?): Boolean {
1412
return when {
15-
a === b -> return true
16-
a == null || b == null -> return false
17-
a is Pair && b is Pair -> return comparePairs(a, b)
18-
a is Array<*> && b is Array<*> -> return Arrays.deepEquals(a, b)
19-
a is IntArray && b is IntArray -> Arrays.equals(a, b)
20-
a is Double && b is Double -> equalDouble(a, b)
21-
a is Int && b is Double -> equalDouble(a.toDouble(), b)
22-
a is Double && b is Int -> equalDouble(a, b.toDouble())
23-
a == b -> return true
24-
else -> return a.toString() == b.toString()
13+
expected === toCheck -> return true
14+
expected == null || toCheck == null -> return false
15+
expected is Pair && toCheck is Pair -> return comparePairs(expected, toCheck)
16+
expected is Array<*> && toCheck is Array<*> -> return Arrays.deepEquals(expected, toCheck)
17+
expected is IntArray && toCheck is IntArray -> Arrays.equals(expected, toCheck)
18+
expected is Double && toCheck is Double -> equalDouble(expected, toCheck)
19+
expected is Int && toCheck is Double -> equalDouble(expected.toDouble(), toCheck)
20+
expected is Double && toCheck is Int -> equalDouble(expected, toCheck.toDouble())
21+
expected == toCheck -> return true
22+
else -> return expected.toString() == toCheck.toString()
2523
}
2624
}
2725

28-
private fun comparePairs(a: Pair, b: Pair): Boolean {
29-
return equalForObjectsOrArrays(a.first, b.first) &&
30-
equalForObjectsOrArrays(a.second, b.second)
26+
private fun comparePairs(expected: Pair, toCheck: Pair): Boolean {
27+
return equalForObjectsOrArrays(expected.first, toCheck.first) &&
28+
equalForObjectsOrArrays(expected.second, toCheck.second)
3129
}
3230

33-
private fun equalDouble(a: Double, b: Double): Boolean {
34-
val df = DecimalFormat("#.#####")
35-
df.roundingMode = RoundingMode.HALF_UP
36-
37-
return df.format(a) == df.format(b)
31+
private fun equalDouble(expected: Double, toCheck: Double): Boolean {
32+
return Math.abs(toCheck - expected) / Math.max(1.0, expected) < 1e-5
3833
}
3934
}

0 commit comments

Comments
 (0)