Floating point calculations on integers and integers multiplied by powers of 2 (e.g.
1.5) are actually exact until you exceed the maximum number of digits that are stored (52 bits), at which point their accuracy will degrade gracefully (making some errors in the least-significant bits). That happens around
n=7403 for this calculation.
In contrast, working with
Int means that you will get catastrophic failure once you overflow 64 bits, which happens around
n == 80000 for this calculation:
Broadening your signatures from
::Integer, so that they accept any integer type, we can still get the exact answer (at considerably more expense) with
but doing this calculation in floating-point with
difference(n::AbstractFloat) = (1.5n^4 + n^3 - 1.5n^2 - n) / 6 gives almost the right answer (to ≈16 significant digits) much more quickly:
julia> (difference(80000.0) - difference(big(80000))) / difference(big(80000))