Divrem maximum precision for the remain

Hi,

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…
    Thanks

Not sure what you mean…?

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

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 ?
tks

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
10

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

julia> join(rand(UInt128,10))
"2997542237536841913023008890862524593892202474444479844540638202305989309433882420702868091517810648071668749093574691691359969235462262670988664056775124475755976522656880499256754584197133220827916588521147525702691343624052586529728561980930665870278414038160354714713611628639534846293764121386897903662099486116256782804070711802348470320571877127214335449134765672772962667057558827"

On linux you could also do something like

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

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
58.8235294117647

julia> 1000/17
58.8235294117647

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
58.8235294117647
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)
0.8235294117647058823529411764705882352941176470588235294117647058823529411764705882352941176470588235294117647058823529411764705882352941176470588235294117647058823529411764705882352941176470588235294117647058823529411764705882352941176470588235294117647058823529411764705882352941176470588235294117647058823539

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 !
:slight_smile:

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)
0.823529411764703

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

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