Divrem maximum precision for the remain


Can divrem return a remainder with a higher digit precision ? more digits after the dot…

  • or is it just meant to round the remainder to 2 digits most always…

Not sure what you mean…?

julia> divrem(100*rand(), 5)
(19.0, 0.2026559586181662)

hi @tomerarnon
, I ll test that right now !
… somehow, I was only getting results such as (19, 0)…
… could be what I m looking for, tks

Ok, I’m still stuck with only 2 digit result for the remainder…
tested on various values above 1000…

drem = divrem(val, 17)

results : drem(58,16), drem(58,2)

So, i’m never getting a longer string as the one you displayed : 0.2026559586181662.
All I get is 1 or 2 digits…always
any idea what I could tweak somewhere ?
pkg ?

If you are dividing a integer, then probably either:

  1. The fractional part is just two digits.
  2. Julia display numbers in a human-readable way but imprecise (i.e., 0.1 display 0.1 but ten 0.1 summed are 0.9999... so it is not exactly correct), to be able to see the “whole” number you may use Printf.@printf("%a", 0.1) but this will show the number in hexadecimal notation.
1 Like

100*rand() produces a non interger number most of the time. I think you passed an integer to divrem.

If you just want to produce a long string of numbers as a string you could do something like

julia> length=10

julia> join(rand(0:9,length))

julia> join(rand(UInt128,10))

On linux you could also do something like

julia> join(read("/dev/urandom",10))

thanks for the details, 1 thing, I cant use rand, beside, diving 1000,1001, by 17 most always return a bunch of digit…so maybe its only while using rand/x that Julia returns more digits (19.0, 0.2026559586181662)…while if using selected values ie 1000/17…Julia only returns the first 2 digits…
I’m guessing.
@feanor12 …i’ll test those now…

I think I see the misunderstanding here. divrem does integer flooring division (div), and also returns the remainder of the operation (the same remainder as in long division). rem is not the the fractional part of a floating point number (some languages have a function called frac that extracts this).

This is the expected behavior of divrem:

julia> divrem(1000, 17)
(58, 14)

julia> 58 + 14/17

julia> 1000/17

The 14 returned by rem is the remainder after long division by 17, so 14/17 is the fractional part.

I think you were expecting this?

julia> divrem(1000/17, 1)
(58.0, 0.823529411764703)

julia> 1000/17
1 Like

Just to mention it. There is also BigFloat. I’m just not sure about the limitations.

julia> BigFloat(rem(1000,17)//17,precision=1024)

Update: Fixed the example to use a fraction as a source for the BigFloat

correct @tomerarnon ! Yes, that is what I was trying to get - sorry if my writtings where not clear enuff from the begining…i tried not to say too much to keep it clear enuff, but yes, thats it !
I’ve been browsing juliadoc and other site about remdiv and never saw any mention about the extra parameter " … ,1) "
Do you have any link about that or is this some rule in Julia with other functions too ?
adding ,1 at the end of a function …could returns more precised results ?
Thanks very much any way to all !

It’s not an extra parameter, I am taking the remainder after dividing by one… The remainder is necessarily anything that doesn’t 1 doesn’t “fit in to”. There are still only two parameters to the function. The first is (1000/17). The second is 1.

This doesn’t have anything to do with precision actually. The 14 I was getting before was equally precise, it just didn’t mean what you thought it meant.

1 Like

thanks again, yes, I probably have to wrap my head arround what remdiv actualy is about, I probably had another idea…and much much more to learn about Julia’s tricks ! :slight_smile:

thanks, I’m testing that too !

If you’re just looking for the remainder, it’s faster to just use rem, and even faster to use trunc:

julia> rem(1000/17, 1)

julia> x = 1000/17; x - trunc(x)

Waah, BIG thanks for pointing me to those @DNF,
will test all that !