From 2df70686158e4c716f10c3bafb397aa9cfca43f1 Mon Sep 17 00:00:00 2001
From: Norbert Grunwald <norbert.grunwald@ufz.de>
Date: Wed, 16 Oct 2024 13:11:43 +0200
Subject: [PATCH] Expression may be negative, so max(0,value) is used.

Although the expression is generally prevented from becoming negative by a conditional, it can still be negative for specific values, which is why the absolute value of the expression is taken.
---
 MathLib/Nonlinear/cubic_roots.hpp | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/MathLib/Nonlinear/cubic_roots.hpp b/MathLib/Nonlinear/cubic_roots.hpp
index ebb42e10a2a..cae09ec1b49 100644
--- a/MathLib/Nonlinear/cubic_roots.hpp
+++ b/MathLib/Nonlinear/cubic_roots.hpp
@@ -89,7 +89,7 @@ std::array<Real, 3> cubic_roots(Real a, Real b, Real c, Real d)
         // branch, and we have 3 real roots, two are a double root. Take
         // (x+1)^2(x-2) = x^3 - 3x -2 as an example. This clearly has a double
         // root at x = -1, and it gets sent into this branch.
-        Real arg = R * R - Q * Q * Q;
+        Real arg = std::max(0., R * R - Q * Q * Q);
         Real A = (R >= 0 ? -1 : 1) * cbrt(abs(R) + sqrt(arg));
         Real B = 0;
         if (A != 0)
-- 
GitLab