# Best practices in dealing avoiding with precision/floating round of problems when solving ODE's? Thank you

**URL:** <https://discourse.julialang.org/t/best-practices-in-dealing-avoiding-with-precision-floating-round-of-problems-when-solving-odes-thank-you/43394>\
**Category:** General Usage\
**Tags:** ode\
**Created:** [July 20, 2020, 6:53pm UTC](https://discourse.julialang.org/t/best-practices-in-dealing-avoiding-with-precision-floating-round-of-problems-when-solving-odes-thank-you/43394 "2020-07-20T18:53:36Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![cbasile21](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cbasile21/32/13072_2.png) [@cbasile21](https://discourse.julialang.org/u/cbasile21)\
**Post date:** [July 20, 2020, 6:53pm UTC](https://discourse.julialang.org/t/best-practices-in-dealing-avoiding-with-precision-floating-round-of-problems-when-solving-odes-thank-you/43394/1 "2020-07-20T18:53:36Z")

</div>

Here is an extreme case I posed. Here, I end up with a domain error, because I start at 0.1 ( any higher ) one parameter will give a domain error, I chose du[2] to be 0 or close to it with a negative value. I get a domain error because of floating rounding of or precision as I describe it. I wanted to understand best practices of how to solve this, Chris suggested me earlier and it was very helpful to add abs ( ) , I have seen also practices of using max(0, (…)) , (…) being the negative value due to rounding i.e param\_cal= (1-u[2]/0.1)^0.8. I have not tried this , but if param\_cal was= (0.1- u[2])^0.8 , would it have also given similar error IC of u[2] is 0.1, or is it the division that creates this issue.

Hope you find the message clear, thank you again!

Christian

```julia
using DifferentialEquations
using Sundials

function ode_functionminie(du,u, p,t)

k1=0.01
k2=0.01
Dt=2000;

println(u[2])
param_cal= (1-u[2]/0.1)^0.8

du[1] = Dt - u[1]*k1; 
du[2] = -1e-9;]

return du

end

println("Starting....")

solvers= ["sundials", "kencarp4offauto" , "kencarp4auto"]
solver_choose= solvers[2]

if solver_choose=="lsoda"
    solver= lsoda();

    #sol = solve(prob, abstol=1e-6, reltol=1e-6, lsoda())
elseif solver_choose=="sundials"
    solver= CVODE_BDF();

elseif solver_choose=="kencarp4auto"
    solver = KenCarp4();

elseif solver_choose=="kencarp4offauto"
    solver = KenCarp4(autodiff=false);
end

p=[]
uo_e=[0.0, 0.1]
tol= 1e-6
t_start= 0.0;
sim_time_e= 24.0;

prob = ODEProblem(ode_functionminie,uo_e,(t_start,sim_time_e), p)
if solver_choose=="sundials"
    sol = solve(prob, abstol=tol, reltol=tol,solver, dtmax=0.1);
elseif solver_choose=="kencarp4offauto" || solver_choose=="kencarp4auto"
    sol = solve(prob, abstol=tol, reltol=tol,solver, dtmax=0.1);
else
    sol = solve(prob, abstol=tol, reltol=tol,solver, saveat=0.05);
end

times= 0.0:1.0:12.0
time_output, output= collect(times), sol(times).u
final_output= Array(sol(times)')
MATraw= output;
final_output2= permutedims(reshape(hcat(MATraw...), (length(MATraw[1]), length(MATraw))))

println("Done")

```

---

<div class="post-metadata">

**Author:** ![ChrisRackauckas](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chrisrackauckas/32/77_2.png) [@ChrisRackauckas](https://discourse.julialang.org/u/ChrisRackauckas)\
**Post date:** [July 24, 2020, 4:18am UTC](https://discourse.julialang.org/t/best-practices-in-dealing-avoiding-with-precision-floating-round-of-problems-when-solving-odes-thank-you/43394/2 "2020-07-24T04:18:22Z")

</div>

Either way it’s the same thing if you want to reject the step.

---

<div class="post-metadata">

**Author:** ![cbasile21](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cbasile21/32/13072_2.png) [@cbasile21](https://discourse.julialang.org/u/cbasile21)\
**Post date:** [July 24, 2020, 5:31pm UTC](https://discourse.julialang.org/t/best-practices-in-dealing-avoiding-with-precision-floating-round-of-problems-when-solving-odes-thank-you/43394/3 "2020-07-24T17:31:40Z")

</div>

I think I understand , thank you , Chris!
