Need help fixing an sqrt related error

Here is the error message:

ERROR: LoadError: DomainError with -0.038468373019053814:
sqrt will only return a complex result if called with a complex argument. Try sqrt(Complex(x)).

 [12] CO2SYS(PAR1::Vector{Float64}, PAR2::Vector{Float64}, PAR1TYPE::Int64, PAR2TYPE::Int64, SAL::Vector{Float64}, TEMPIN::Vector{Float64}, TEMPOUT::Vector{Float64}, PRESIN::Int64, PRESOUT::Int64, SI::Vector{Float64}, PO4::Vector{Float64}, pHSCALEIN::Int64, K1K2CONSTANTS::Int64, KSO4CONSTANTS::Int64)
    @ CO2System ~/.julia/packages/CO2System/MPyQp/src/CO2System.jl:383

Here is the code at line 383 of CO2System.jl
sqrSal = sqrt.(SAL)

What should be the fix? Many thanks!

You either want sqrSal = sqrt.(Complex(SAL)) or sqrSal = sqrt.(max(0, SAL)). You can’t take the sqrt of of a negative real number.

2 Likes

Many thanks for the solution!