Skip to content

Commit 2395bb3

Browse files
authored
use the preference system for nan safe mode (#539)
1 parent e7f21c3 commit 2395bb3

File tree

4 files changed

+19
-3
lines changed

4 files changed

+19
-3
lines changed

Project.toml

+2
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ DiffResults = "163ba53b-c6d8-5494-b064-1a9d43ac40c5"
88
DiffRules = "b552c78f-8df3-52c6-915a-8e097449b14b"
99
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"
1010
NaNMath = "77ba4419-2d1f-58cd-9bb1-8ffee604a2e3"
11+
Preferences = "21216c6a-2e73-6563-6e65-726566657250"
1112
Printf = "de0858da-6303-5e67-8744-51eddeeeb8d7"
1213
Random = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c"
1314
SpecialFunctions = "276daf66-3868-5448-9aa4-cd146d93841b"
@@ -20,6 +21,7 @@ DiffResults = "0.0.1, 0.0.2, 0.0.3, 0.0.4, 1.0.1"
2021
DiffRules = "1.2.1"
2122
DiffTests = "0.0.1, 0.1"
2223
NaNMath = "0.2.2, 0.3"
24+
Preferences = "1"
2325
SpecialFunctions = "0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 0.10, 1.0"
2426
StaticArrays = "0.8.3, 0.9, 0.10, 0.11, 0.12, 1.0"
2527
julia = "1"

docs/src/user/advanced.md

+9-2
Original file line numberDiff line numberDiff line change
@@ -138,8 +138,15 @@ decrease performance (~5%-10% on our benchmarks).
138138

139139
In order to preserve performance in the majority of use cases, ForwardDiff disables this
140140
check by default. If your code is affected by this `NaN` behavior, you can enable
141-
ForwardDiff's `NaN`-safe mode by setting the `NANSAFE_MODE_ENABLED` constant to `true` in
142-
ForwardDiff's source. The constant is located in `src\prelude.jl`.
141+
ForwardDiff's `NaN`-safe mode by using the
142+
[Preferences.jl](https://github.com/JuliaPackaging/Preferences.jl) API to set
143+
the `nansafe_mode` preference to true, for example via:
144+
145+
```julia
146+
julia> using ForwardDiff, Preferences
147+
148+
julia> set_preferences!(ForwardDiff, "nansafe_mode" => true)
149+
```
143150

144151
In the future, we plan on allowing users and downstream library authors to dynamically
145152
enable [NaN`-safe mode via the `AbstractConfig`

src/ForwardDiff.jl

+3
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@ module ForwardDiff
33
using DiffRules, DiffResults
44
using DiffResults: DiffResult, MutableDiffResult, ImmutableDiffResult
55
using StaticArrays
6+
if VERSION >= v"1.6"
7+
using Preferences
8+
end
69
using Random
710
using LinearAlgebra
811

src/prelude.jl

+5-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
1-
const NANSAFE_MODE_ENABLED = false
1+
@static if VERSION >= v"1.6"
2+
const NANSAFE_MODE_ENABLED = @load_preference("nansafe_mode", false)
3+
else
4+
const NANSAFE_MODE_ENABLED = false
5+
end
26

37
const AMBIGUOUS_TYPES = (AbstractFloat, Irrational, Integer, Rational, Real, RoundingMode)
48

0 commit comments

Comments
 (0)