I repeatedly run into issues handling large values (values beyond 1e+15), and couldn’t find a clear answer on how to address those.
Consider following comparison:
(1.0e+16 + 1 + 1 + 1) > 1.0e+16
will wrongfully return false
, however i can mitigate this issue by using either
(1.0e+16 + (1+1+1) ) > 1.0e+16
or
(BigInt(1.0e+16) + 1 + 1 + 1) > 1.0e+16
both returning true
- the correct answer. Which hints that types as well as bracets have influence on the rounding, even if they should be mathematically equivalent.
The same logic applies to following modulo calculations:
(BigInt(1e+16) + 1) % 2 # is 1 - correct !
BigInt(1e+16 + 1) % 2 # is not 1 - false !
In those simple examples I can address those issues, however applying the same type hints ( BigInt
or BigFloat
) in slightly more complex operations (for example Tupper’s self-referential formula here) I fail to obtain the correct solution:
tupper(x,y) = 0.5 < floor(floor( BigFloat(y) / 17) * 2.0^( -17 * floor(BigFloat(x))-(BigFloat(y) % 17)) % 2) ? 1 : 0
k = BigInt(960939379918958884971672962127852754715004339660129306651505519271702802395266424689642842174350718121267153782770623355993237280874144307891325963941337723487857735749823926629715517173716995165232890538221612403238855866184013235585136048828693337902491454229288667081096184496091705183454067827731551705405381627380967602565625016981482083418783163849115590225610003652351370343874461848378737238198224849863465033159410054974700593138339226497249461751545728366702369745461014655997933798537483143786841806593422227898388722980000748404719)
tupper(0,k) # returns 0 but should be 1
I assume I call BigFloat
at the wrong place, leading to a wrong modulo result. How is the correct version of the tupper function in julia, and where can I learn how to use Arbitrary Precision Arithmetic correctly in functions?
thank you!