Hi, I’m new to Julia.
I’m trying to use the round() command so that an == condition works properly (don’t need 60 decimal points). For example: round(pi, 3) returns:
ERROR: MethodError: no method matching round(::Irrational{:π}, ::Int64)
round(5.6789, 3) returns:
ERROR: MethodError: no method matching round(::Float64, ::Int64)
Attempting to use signif() returns:
ERROR: UndefVarError: signif not defined
How do I fix Julia to follow its documentation for these basic commands?
2 Likes
You must be looking at the documentation from an older version of Julia: in 0.7 and later round
takes a keyword digits
or sigdigits
keyword:
julia> round(pi, digits=3)
3.142
julia> round(pi, sigdigits=3)
3.14
However this is probably not a good way to check for approximate equality since it can be very brittle (e.g. very close numbers can round in opposite directions). I would suggest you use isapprox
(or its infix form ≈
) instead
7 Likes
Ok, thanks for the update on the syntax. I think isapprox might be a better way to do what I want since I’ve got to use an equation as the input, and round() appears to only work with simple arithmetic.
However, I’m still having trouble making it do what I want it to.
@constraint(m, [i = 1:nREGION], isapprox((PRODUCE[i]-CONSUME[i])*10^9 + sum(VolShip[i,j] for j=1:nREGION) - sum(VolShip[j,i] for j=1:nREGION), 0))
spits out this error:
ERROR: LoadError: LoadError: Unrecognized sense isapprox
Stacktrace:
[1] _canonicalize_sense(::Symbol) at C:\Users\kirbo.julia\packages\JuMP\PbnIJ\src\macros.jl:303
[2] @constraint(::LineNumberNode, ::Module, ::Vararg{Any,N} where N) at C:\Users\kirbo.julia\packages\JuMP\PbnIJ\src\macros.jl:484
Essentially I’ve got a mass balance problem with multiple options. right now I’ve got it reduced to 3 nodes, but I want to expand. It worked with three briefly before I made inputs with decimal spaces - now Julia’s excessive decimal places screwed things up.
Is there a better function for dealing with open ended macros like JuMP?
I’m guessing JuMP doesn’t support isapprox
. The easiest option is to make it an inequality:
@constraint(m, [i = 1:nREGION], -tol <= (PRODUCE[i]-CONSUME[i])*10^9 + sum(VolShip[i,j] for j=1:nREGION) - sum(VolShip[j,i] for j=1:nREGION) <= tol)
for some appropriate value tol
.