# Issue with Binomial distribution and recent ForwardDiff versions

**URL:** https://discourse.julialang.org/t/issue-with-binomial-distribution-and-recent-forwarddiff-versions/134347
**Category:** Statistics
**Tags:** turing, forwarddiff, distributions
**Created:** [December 4, 2025, 6:26am UTC](https://discourse.julialang.org/t/issue-with-binomial-distribution-and-recent-forwarddiff-versions/134347 "2025-12-04T06:26:26Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![Pablo\_Marchant](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pablo_marchant/32/32828_2.png) [@Pablo\_Marchant](https://discourse.julialang.org/u/Pablo_Marchant)
#### Post date: [December 4, 2025, 6:26am UTC](https://discourse.julialang.org/t/issue-with-binomial-distribution-and-recent-forwarddiff-versions/134347/1 "2025-12-04T06:26:26Z")

</div>

Hi,

recently I noticed some code I’ve been using broke after updating Forward Diff. The issue came from sampling with `Turing` using a binomial likelihood. Digging into it it boiled down to this:

```julia
using ForwardDiff
using Distributions
d = ForwardDiff.Dual(1.0,2.0);
Binomial(1,d)

```

which results in the error

```julia-auto
ERROR: DomainError with Dual{Nothing}(1.0,2.0):
Binomial: the condition zero(p) <= p <= one(p) is not satisfied.
Stacktrace:
 [1] #79
   @ ~/.julia/packages/Distributions/psM3H/src/univariate/discrete/binomial.jl:33 [inlined]
 [2] check_args
   @ ~/.julia/packages/Distributions/psM3H/src/utils.jl:89 [inlined]
 [3] #Binomial#77
   @ ~/.julia/packages/Distributions/psM3H/src/univariate/discrete/binomial.jl:33 [inlined]
 [4] Binomial(n::Int64, p::ForwardDiff.Dual{Nothing, Float64, 1})
   @ Distributions ~/.julia/packages/Distributions/psM3H/src/univariate/discrete/binomial.jl:32
 [5] top-level scope
   @ REPL[7]:1

```

Diggin deeper I found it this is caused by changes on how `ForwardDiff` treats comparisons of Dual numbers in between versions `0.10.38` and `1.0.0` (specifically this PR with its changes to `dual.jl`: [https://github.com/JuliaDiff/ForwardDiff.jl/pull/481](https://github.com/JuliaDiff/ForwardDiff.jl/pull/481)). In practice it means that we have:

```julia-repl
julia> d = ForwardDiff.Dual(1.0,2.0); d<=one(d)
false

julia> d = ForwardDiff.Dual(1.0,-2.0); d<=one(d)
true

```

And a similar things happens at zero. I understand the motivation of this change in `ForwardDiff`, but this does produce unusual behavior in instances like this. Wondering if this is an actual bug and some change should be done somewhere. In practice for now I can get almost equivalent results for my use case by slightly translating the probability away from zero and one, e.g.:

```julia
d = ForwardDiff.Dual(1.0,2.0); d<=one(d)
d = d*(1-1e-15)+5e-16
Binomial(1,d)

```

but that’s really just a dirty workaround and can imagine many users running into this not knowing how to sort it out.

---

<div class="post-metadata">

### Author: ![Pablo\_Marchant](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pablo_marchant/32/32828_2.png) [@Pablo\_Marchant](https://discourse.julialang.org/u/Pablo_Marchant)
#### Post date: [December 6, 2025, 9:11am UTC](https://discourse.julialang.org/t/issue-with-binomial-distribution-and-recent-forwarddiff-versions/134347/2 "2025-12-06T09:11:15Z")

</div>

And perhaps a better workaround would be:

```julia
d = ForwardDiff.Dual(1.0,2.0); d<=one(d)
d = d*(1.0-eps(1.0))+eps(zero(d))
Binomial(1,d)

```

That really ensures we’re doing the minimal possible change allowed to machine precision (or whatever precision `d` has)

---

<div class="post-metadata">

### Author: ![mgp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mgp/32/28599_2.png) [@mgp](https://discourse.julialang.org/u/mgp)
#### Post date: [December 12, 2025, 3:57pm UTC](https://discourse.julialang.org/t/issue-with-binomial-distribution-and-recent-forwarddiff-versions/134347/3 "2025-12-12T15:57:48Z")

</div>

I have come with a similar question, which I think is best discussed in the same thread. I found the behaviour below rather unexpected:

```julia-auto
julia> import DynamicPPL, ForwardDiff

julia> x = ForwardDiff.Dual{}(1.0,1e-16);

julia> min(x, one(x)) <= one(x)
false

```
