Julia DISCUSSION

Julia InexactError when converting Float64 to Int: what is the right way to round and index?

Started by arnoldpalmer Julia InexactErrorfloat to integer conversionround trunc floorinteger divisionrounding mode
5 replies 248 views 6 participants
Latest activity · 30 Sep 2026

Julia InexactError when converting Float64 to Int: what is the right way to round and index?

arnoldpalmer Julia Forum
#1

I compute an array position as n / 2 and then use it as an index, and Julia refuses with an error about an invalid index of type Float64. Wrapping it in Int(...) works for even n but throws InexactError for odd n. In C the same code simply truncates.

What is the intended way to go from a floating-point value to an integer in Julia, and which rounding does each option use?

Community replies 5

Re: Julia InexactError when converting Float64 to Int: what is the right way to round and index?

#2

/ always produces a floating-point result for integers: 10 / 2 is 5.0, not 5. Array indices must be integers, hence the first error. For an integer quotient, use integer division: n ÷ 2, which is the same as div(n, 2) and truncates toward zero, so 7 ÷ 2 == 3. You can type the symbol in the REPL as \div followed by Tab. Then no conversion is needed at all, which is the best fix for an index calculation.

Re: Julia InexactError when converting Float64 to Int: what is the right way to round and index?

#3

Int(x) is a lossless conversion, not a rounding operation. Int(4.0) gives 4, but Int(3.5) throws InexactError because there is no integer equal to 3.5. Julia makes you say which rounding you want: trunc(Int, x) toward zero (the C cast behaviour), floor(Int, x) down, ceil(Int, x) up, and round(Int, x) to nearest.

Without the type argument, these return a floating-point value: round(3.7) is 4.0.

Re: Julia InexactError when converting Float64 to Int: what is the right way to round and index?

#4

Watch the tie rule of round. The default mode rounds halves to the nearest even integer, so round(Int, 2.5) is 2 and round(Int, 3.5) is 4. That avoids a systematic upward bias when many values are rounded, but it surprises people who expect 3. For the schoolbook rule, pass the mode: round(Int, 2.5, RoundNearestTiesAway) gives 3.

For negative numbers the variants differ too: trunc(Int, -3.7) is -3, floor(Int, -3.7) is -4.

Re: Julia InexactError when converting Float64 to Int: what is the right way to round and index?

#5

The integer division family has the same distinctions. div(-7, 2) is -3 and fld(-7, 2) is -4 (floored), with cld rounding up. The matching remainders are rem(-7, 2) == -1, which is what % does, and mod(-7, 2) == 1.

For wrapping an index into a circular buffer of length N with 1-based indexing, mod1(i, N) returns a value in 1 to N directly, which avoids the usual off-by-one.

Re: Julia InexactError when converting Float64 to Int: what is the right way to round and index?

#6

Two range limits to keep in mind. The conversion also throws when the value does not fit: round(Int, 1e20) is an InexactError, since Int is 64 bits on a 64-bit system and tops out near 9.22 × 10^18. NaN and Inf fail the same way, so check with isfinite(x) if the input comes from a measurement.

Integer arithmetic itself is not checked: typemax(Int) + 1 silently wraps to typemin(Int). If that can happen, use a wider type such as Int128 or BigInt, or the checked functions like Base.checked_add.

TEP COMMUNITY