# Divrem maximum precision for the remain

**URL:** <https://discourse.julialang.org/t/divrem-maximum-precision-for-the-remain/49510>\
**Category:** Numerics\
**Created:** [November 3, 2020, 9:12am UTC](https://discourse.julialang.org/t/divrem-maximum-precision-for-the-remain/49510 "2020-11-03T09:12:33Z")\
**Posts on this page:** 15\
**Page:** 1

<div class="post-metadata">

**Author:** ![teriiju](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/teriiju/32/19029_2.png) [@teriiju](https://discourse.julialang.org/u/teriiju)\
**Post date:** [November 3, 2020, 9:12am UTC](https://discourse.julialang.org/t/divrem-maximum-precision-for-the-remain/49510/1 "2020-11-03T09:12:33Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![tomerarnon](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tomerarnon/32/3170_2.png) [@tomerarnon](https://discourse.julialang.org/u/tomerarnon)\
**Post date:** [November 3, 2020, 10:06am UTC](https://discourse.julialang.org/t/divrem-maximum-precision-for-the-remain/49510/2 "2020-11-03T10:06:20Z")

</div>

Not sure what you mean…?

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

```

---

<div class="post-metadata">

**Author:** ![teriiju](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/teriiju/32/19029_2.png) [@teriiju](https://discourse.julialang.org/u/teriiju)\
**Post date:** [November 3, 2020, 5:21pm UTC](https://discourse.julialang.org/t/divrem-maximum-precision-for-the-remain/49510/3 "2020-11-03T17:21:39Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![teriiju](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/teriiju/32/19029_2.png) [@teriiju](https://discourse.julialang.org/u/teriiju)\
**Post date:** [November 3, 2020, 5:30pm UTC](https://discourse.julialang.org/t/divrem-maximum-precision-for-the-remain/49510/4 "2020-11-03T17:30:23Z")

</div>

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

```julia
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

---

<div class="post-metadata">

**Author:** ![Henrique\_Becker](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/henrique_becker/32/15443_2.png) [@Henrique\_Becker](https://discourse.julialang.org/u/Henrique_Becker)\
**Post date:** [November 3, 2020, 5:52pm UTC](https://discourse.julialang.org/t/divrem-maximum-precision-for-the-remain/49510/5 "2020-11-03T17:52:52Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![feanor12](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/feanor12/32/8212_2.png) [@feanor12](https://discourse.julialang.org/u/feanor12)\
**Post date:** [November 3, 2020, 5:53pm UTC](https://discourse.julialang.org/t/divrem-maximum-precision-for-the-remain/49510/6 "2020-11-03T17:53:27Z")

</div>

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
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
julia> join(read("/dev/urandom",10))
"142408250141153206245209121"

```

---

<div class="post-metadata">

**Author:** ![teriiju](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/teriiju/32/19029_2.png) [@teriiju](https://discourse.julialang.org/u/teriiju)\
**Post date:** [November 3, 2020, 6:00pm UTC](https://discourse.julialang.org/t/divrem-maximum-precision-for-the-remain/49510/7 "2020-11-03T18:00:38Z")

</div>

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…

---

<div class="post-metadata">

**Author:** ![tomerarnon](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tomerarnon/32/3170_2.png) [@tomerarnon](https://discourse.julialang.org/u/tomerarnon)\
**Post date:** [November 3, 2020, 6:17pm UTC](https://discourse.julialang.org/t/divrem-maximum-precision-for-the-remain/49510/8 "2020-11-03T18:17:15Z")

</div>

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
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
julia> divrem(1000/17, 1)
(58.0, 0.823529411764703)

julia> 1000/17
58.8235294117647

```

---

<div class="post-metadata">

**Author:** ![feanor12](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/feanor12/32/8212_2.png) [@feanor12](https://discourse.julialang.org/u/feanor12)\
**Post date:** [November 3, 2020, 6:25pm UTC](https://discourse.julialang.org/t/divrem-maximum-precision-for-the-remain/49510/9 "2020-11-03T18:25:16Z")

</div>

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

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

```

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

---

<div class="post-metadata">

**Author:** ![teriiju](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/teriiju/32/19029_2.png) [@teriiju](https://discourse.julialang.org/u/teriiju)\
**Post date:** [November 3, 2020, 6:27pm UTC](https://discourse.julialang.org/t/divrem-maximum-precision-for-the-remain/49510/10 "2020-11-03T18:27:53Z")

</div>

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 !

---

<div class="post-metadata">

**Author:** ![tomerarnon](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tomerarnon/32/3170_2.png) [@tomerarnon](https://discourse.julialang.org/u/tomerarnon)\
**Post date:** [November 3, 2020, 6:34pm UTC](https://discourse.julialang.org/t/divrem-maximum-precision-for-the-remain/49510/11 "2020-11-03T18:34:18Z")

</div>

> [@teriiju](#):
>
> I’ve been browsing juliadoc and other site about remdiv and never saw any mention about the extra parameter " … ,1) "

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`.

> [@teriiju](#):
>
> …could returns more precised results ?

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.

---

<div class="post-metadata">

**Author:** ![teriiju](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/teriiju/32/19029_2.png) [@teriiju](https://discourse.julialang.org/u/teriiju)\
**Post date:** [November 3, 2020, 6:38pm UTC](https://discourse.julialang.org/t/divrem-maximum-precision-for-the-remain/49510/12 "2020-11-03T18:38:17Z")

</div>

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 ! 🙂

---

<div class="post-metadata">

**Author:** ![teriiju](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/teriiju/32/19029_2.png) [@teriiju](https://discourse.julialang.org/u/teriiju)\
**Post date:** [November 3, 2020, 6:39pm UTC](https://discourse.julialang.org/t/divrem-maximum-precision-for-the-remain/49510/13 "2020-11-03T18:39:35Z")

</div>

thanks, I’m testing that too !  
🙂

---

<div class="post-metadata">

**Author:** ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)\
**Post date:** [November 3, 2020, 7:24pm UTC](https://discourse.julialang.org/t/divrem-maximum-precision-for-the-remain/49510/14 "2020-11-03T19:24:14Z")

</div>

> [@tomerarnon](#):
>
> `divrem(1000/17, 1)`

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

```julia
julia> rem(1000/17, 1)
0.823529411764703

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

```

---

<div class="post-metadata">

**Author:** ![teriiju](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/teriiju/32/19029_2.png) [@teriiju](https://discourse.julialang.org/u/teriiju)\
**Post date:** [November 3, 2020, 7:30pm UTC](https://discourse.julialang.org/t/divrem-maximum-precision-for-the-remain/49510/15 "2020-11-03T19:30:14Z")

</div>

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