Float is an Inf and I can't use && to assign it a new value

I’m trying to use the short circuit conditional from the julia docs and I have a sneaking feeling that this will correct a misapprehension of what is going on. My “thought” is that this should spot that sdmove is an Inf and change it to the number 999.99.

any help appreciated.

In the expression a && b, the subexpression b is only evaluated if a evaluates to true.
isinf(sdmove)
true

julia> isinf(sdmove) && sdmove = 999.99
ERROR: syntax: invalid assignment location "isinf(sdmove) && sdmove" around REPL[25]:1
Stacktrace:
 [1] top-level scope
   @ REPL[25]:1
julia> typeof(sdmove)
Float64

julia> typeof(999.99)
Float64

julia> 



Due to the relative priorities of infix operators && and =, this is parsed as

(isinf(sdmove) && sdmove) = 999.99

instead of what you really meant:

isinf(sdmove) && (sdmove = 999.9)

You can use this last expression in order to explicitly set the correct order of operations.


(Opinions on this may vary from one developer to another, but I personally tend to dislike short circuits in cases where the RHS is an assignment, precisely for that reason. But I do like short circuits like isinf(sdmove) && break, because they don’t suffer from the same need for explicit parentheses)

9 Likes

@ffevotte you are a LEGEND. Worked like a champ. Thanks for taking the time. I’ll check out the infix operator precedence for future use. I’ll also give some HARD consideration to taking another coding approach.
BUT I’m trying to stop this error happening. I PROMISE I’ll try though :slight_smile: thanks again.

┌ Error: ErrorException("Inf not allowed to be written in JSON spec")
└ @ Stipple /home/dave/.julia/packages/Stipple/qiTHA/src/Stipple.jl:889

Thanks again.
theakson

If you do stick with a “sentinel” value consider something way outside the range of reasonable like the max possible float, or maybe 9.99e99

1 Like

@anon69491625 In case you haven’t come across it, the preferred way is with typemax(typeof(sdmove)) floatmax(sdmove).

2 Likes

For floats this just returns Inf though.

Largest float other than inf is floatmax(Float64)

6 Likes

there’s something actually HHGG’esq about this :slight_smile:

julia> isinf(sdmove) && typemax(typeof(sdmove))
Inf

excellent didn’t know about typemax() thanks @Jollywatt and for the heads up @dlakelan

The 999.99 was chosen as I’m using the WONDERFUL Stipple.jl and wanted something I can sort. Will certainly bear your advice in mind for later on “when” I get this working.
thanks again

I think this is a right of passage for everyone going from beginner to intermediate level of Julia and working with short circuiting. I went through a very similar headache a while ago.

1 Like

hmm @affans my rite of passage seems to be reversed, I started knowing things and I seem to be devolving :slight_smile: