-
-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: kata/floating-point-comparison (#735)
* docs: kata description * docs: sync progress * feat: kata/floating-point-comparison --------- Co-authored-by: ParanoidUser <5120290+ParanoidUser@users.noreply.github.com>
- Loading branch information
1 parent
f5ad41e
commit 163b5ce
Showing
5 changed files
with
73 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
# [Floating point comparison](https://www.codewars.com/kata/floating-point-comparison "https://www.codewars.com/kata/5f9f43328a6bff002fa29eb8") | ||
|
||
## Introduction | ||
|
||
`float`s have limited precision and are unable to exactly represent some values. Rounding errors accumulate with repeated computation, and | ||
numbers expected to be equal often differ slightly. | ||
|
||
As a result, it is common advice to not use an exact equality comparison (`==`) with floats. | ||
|
||
``` | ||
>>> a, b, c = 1e-9, 1e-9, 3.33e7 | ||
>>> (a + b) + c == a + (b + c) | ||
False | ||
>>> 0.1 + 0.1 + 0.1 + 0.1 + 0.1 + 0.1 + 0.1 + 0.1 + 0.1 + 0.1 == 1.0 | ||
False | ||
``` | ||
|
||
The solution is to check if a computed value is close to an expected value, without requiring them to be exactly equal. It seems very easy, | ||
but many katas test float results the wrong way. | ||
|
||
## Task | ||
|
||
You have: | ||
|
||
- a float value that comes from a computation and may have accumulated errors up to ±0.001 | ||
- a reference value | ||
|
||
The function is bugged and sometimes returns wrong results. | ||
|
||
Your task is to correct the bug. | ||
|
||
## Note | ||
|
||
This kata uses fixed tolerance for simplicity reasons, but usually relative tolerance is better. Fixed tolerance is useful for comparisons | ||
near zero or when the magnitude of the values is known. |
5 changes: 5 additions & 0 deletions
5
kata/8-kyu/floating-point-comparison/main/FloatingPointComparison.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
interface FloatingPointComparison { | ||
static boolean approxEquals(double a, double b) { | ||
return Math.abs(a - b) < .001; | ||
} | ||
} |
29 changes: 29 additions & 0 deletions
29
kata/8-kyu/floating-point-comparison/test/FloatingPointComparisonTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import static org.junit.jupiter.api.Assertions.assertFalse; | ||
import static org.junit.jupiter.api.Assertions.assertTrue; | ||
|
||
import org.junit.jupiter.params.ParameterizedTest; | ||
import org.junit.jupiter.params.provider.CsvSource; | ||
|
||
class FloatingPointComparisonTest { | ||
@ParameterizedTest | ||
@CsvSource(textBlock = """ | ||
-156.24037, -156.24038 | ||
123.2345, 123.234501 | ||
-1.234, -1.233999 | ||
98.7655, 98.7654999 | ||
-75158.25030, -75158.25017 | ||
""") | ||
void sample(double a, double b) { | ||
assertTrue(FloatingPointComparison.approxEquals(a, b)); | ||
} | ||
|
||
@ParameterizedTest | ||
@CsvSource(textBlock = """ | ||
175.9827, 82.25 | ||
1456.3652, 1456.3641 | ||
-7.28495, -7.28596 | ||
""") | ||
void notEquals(double a, double b) { | ||
assertFalse(FloatingPointComparison.approxEquals(a, b)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters