Skip to content

Commit fe1013f

Browse files
Set default tolerances to match CoordRefSystems.jl values (#1231)
* Set default tolerances to match CoordRefSystems.jl values * Update src/tolerances.jl Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> * Update test/tolerances.jl Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> * Adjust tests * Adjust tests * Explain tolerance formula in docs --------- Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
1 parent dfa924b commit fe1013f

File tree

6 files changed

+18
-23
lines changed

6 files changed

+18
-23
lines changed

docs/src/tolerances.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
# Tolerances
22

33
The absolute tolerance used for floating point comparisons is hard-coded in
4-
the project to `1e-10` for `Float64` and to `1f-5` for `Float32`. You can use
5-
[ScopedValues.jl](https://github.com/vchuravy/ScopedValues.jl) to customize
6-
these tolerance values in specific computations:
4+
the project to `eps(T) ^ (3 // 4)` where `T` is either `Float64` or `Float32`.
5+
This formula preserves roughly 3/4 of the significant digits of the type `T`.
6+
You can use [ScopedValues.jl](https://github.com/vchuravy/ScopedValues.jl) to
7+
customize these tolerance values in specific computations:
78

89
```julia
910
using Meshes

src/tolerances.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22
# Licensed under the MIT License. See LICENSE in the project root.
33
# ------------------------------------------------------------------
44

5-
const ATOL64 = ScopedValue(1.0e-10)
6-
const ATOL32 = ScopedValue(1.0f-5)
5+
const ATOL64 = ScopedValue(eps(Float64)^(3 // 4))
6+
const ATOL32 = ScopedValue(eps(Float32)^(3 // 4))
77

88
"""
99
atol(T)

test/intersections.jl

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1011,9 +1011,7 @@ end
10111011
# Special case: the direction vector is not long enough to cross triangle
10121012
r = Ray(cart(0.2, 0.2, 1.0), vector(0.0, 0.0, -0.00001))
10131013
@test intersection(r, t) |> type == Crossing
1014-
if T === Float64
1015-
@test r t cart(0.2, 0.2, 0.0)
1016-
end
1014+
@test evaluate(Euclidean(), r t, cart(0.2, 0.2, 0.0)) < 1000atol(ℳ)
10171015
# Special case: reverse direction vector should not hit the triangle
10181016
r = Ray(cart(0.2, 0.2, 1.0), vector(0.0, 0.0, 1.0))
10191017
@test intersection(r, t) |> type == NotIntersecting

test/polytopes.jl

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -247,8 +247,8 @@ end
247247
@test nvertices(ur1) < nvertices(r1)
248248
@test nvertices(ur2) < nvertices(r2)
249249
if T === Float32
250-
@test nvertices(ur1) == 10
251-
@test nvertices(ur2) == 10
250+
@test nvertices(ur1) == 12
251+
@test nvertices(ur2) == 12
252252
else
253253
@test nvertices(ur1) == 17
254254
@test nvertices(ur2) == 17
@@ -652,7 +652,7 @@ end
652652
upoly = unique(poly)
653653
@test nvertices(upoly) < nvertices(poly)
654654
if T === Float32
655-
@test nvertices(upoly) == 10
655+
@test nvertices(upoly) == 12
656656
else
657657
@test nvertices(upoly) == 17
658658
end

test/tolerances.jl

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,17 @@
22
=
33
𝒜 = typeof(zero(ℳ)^2)
44
𝒱 = typeof(zero(ℳ)^3)
5-
if T === Float32
6-
@test atol(T) == 1.0f-5
7-
@test atol(ℒ) == 1.0f-5 * u"m"
8-
@test atol(𝒜) == 1.0f-5^2 * u"m^2"
9-
@test atol(𝒱) == 1.0f-5^3 * u"m^3"
10-
else
11-
@test atol(T) == 1.0e-10
12-
@test atol(ℒ) == 1.0e-10 * u"m"
13-
@test atol(𝒜) == 1.0e-10^2 * u"m^2"
14-
@test atol(𝒱) == 1.0e-10^3 * u"m^3"
15-
end
5+
6+
@test atol(T) == eps(T)^(3 // 4)
7+
@test atol(ℒ) == atol(T) * u"m"
8+
@test atol(𝒜) == atol(T)^2 * u"m^2"
9+
@test atol(𝒱) == atol(T)^3 * u"m^3"
10+
1611
@test atol(zero(T)) == atol(T)
1712
@test atol(zero(ℒ)) == atol(ℒ)
1813
@test atol(zero(𝒜)) == atol(𝒜)
1914
@test atol(zero(𝒱)) == atol(𝒱)
15+
2016
@inferred atol(T)
2117
@inferred atol(ℒ)
2218
@inferred atol(𝒜)

test/transforms.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2233,7 +2233,7 @@ end
22332233
@test nvertices(rpoly) == nvertices(poly)
22342234
@test length(first(rings(rpoly))) > length(first(rings(poly)))
22352235
opoly = TB.revert(repair, rpoly, cache)
2236-
@test opoly == poly
2236+
@test opoly poly
22372237
end
22382238

22392239
@testitem "Repair(11)" setup = [Setup] begin

0 commit comments

Comments
 (0)