# Detecting low precision contamination of DoubleFloats.jl

**URL:** https://discourse.julialang.org/t/detecting-low-precision-contamination-of-doublefloats-jl/77408
**Category:** Numerics
**Tags:** question
**Created:** [March 4, 2022, 3:34pm UTC](https://discourse.julialang.org/t/detecting-low-precision-contamination-of-doublefloats-jl/77408 "2022-03-04T15:34:39Z")
**Posts on this page:** 16
**Page:** 1

<div class="post-metadata">

### Author: ![HexSpin](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/hexspin/32/19319_2.png) [@HexSpin](https://discourse.julialang.org/u/HexSpin)
#### Post date: [March 4, 2022, 3:34pm UTC](https://discourse.julialang.org/t/detecting-low-precision-contamination-of-doublefloats-jl/77408/1 "2022-03-04T15:34:40Z")

</div>

For the last many months I’ve very happily used DoubleFloats.jl to compute series akin to 1+1/2+1/3-1/4-1/5… with varying signs changes and ranges up to (and fractions as as small as) 1/10^15 – or smaller. As these summations grow large, I recognize the precision will fall off in the last decimal places, say in the 18th place, I’m comfortable with that.  
I’ve gone through my code and believe every number that touches the final summation is Double64. When I print the final result the typeof(result) = Double64. It’s all good.  
Yet …would I bet my life that the final results are absolutely free of 15 digit contamination? No, I wouldn’t.  
So, my question is this: If the final typeof = Double64, should I feel confident it is contamination free?  
If not, is there some tool that might show where a result lost precision and find something I’m overlooking?  
Thanks!

---

<div class="post-metadata">

### Author: ![tbeason](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tbeason/32/15898_2.png) [@tbeason](https://discourse.julialang.org/u/tbeason)
#### Post date: [March 4, 2022, 3:40pm UTC](https://discourse.julialang.org/t/detecting-low-precision-contamination-of-doublefloats-jl/77408/2 "2022-03-04T15:40:45Z")

</div>

I suppose one check would be to make all numbers in your code be extended precision and then see if you get the same result.

If your code uses only basic operations, you could look through the type promotion rules in Base and/or DoubleFloats.jl to make certain that you are hitting the right methods and that they promote to Double64 appropriately.

---

<div class="post-metadata">

### Author: ![HexSpin](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/hexspin/32/19319_2.png) [@HexSpin](https://discourse.julialang.org/u/HexSpin)
#### Post date: [March 4, 2022, 4:11pm UTC](https://discourse.julialang.org/t/detecting-low-precision-contamination-of-doublefloats-jl/77408/3 "2022-03-04T16:11:16Z")

</div>

I should have been clearer - the only time non-Double64 appear are as loop counters, or in prime generation (which are then made Double64), that sort of thing.

---

<div class="post-metadata">

### Author: ![HexSpin](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/hexspin/32/19319_2.png) [@HexSpin](https://discourse.julialang.org/u/HexSpin)
#### Post date: [May 23, 2022, 6:39pm UTC](https://discourse.julialang.org/t/detecting-low-precision-contamination-of-doublefloats-jl/77408/4 "2022-05-23T18:39:03Z")

</div>

Bueller? Bueller? Anyone?

---

<div class="post-metadata">

### Author: ![jondea](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jondea/32/39086_2.png) [@jondea](https://discourse.julialang.org/u/jondea)
#### Post date: [May 23, 2022, 7:23pm UTC](https://discourse.julialang.org/t/detecting-low-precision-contamination-of-doublefloats-jl/77408/5 "2022-05-23T19:23:59Z")

</div>

I don’t know of any existing tools. But, one idea is to define your own type, say called Double64Canary, which wraps a Double64. Then you could define the basic mathematical operators so that they error if they are performed with types which are lower precision.

---

<div class="post-metadata">

### Author: ![HexSpin](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/hexspin/32/19319_2.png) [@HexSpin](https://discourse.julialang.org/u/HexSpin)
#### Post date: [May 23, 2022, 7:41pm UTC](https://discourse.julialang.org/t/detecting-low-precision-contamination-of-doublefloats-jl/77408/6 "2022-05-23T19:41:06Z")

</div>

Thank you Jondea. I’ll give that some thought. Would you have any thoughts on “If the final typeof = Double64, should I feel confident it is contamination free?” Or will a value claim that high precision even if it’s been poorly used along the way?

---

<div class="post-metadata">

### Author: ![jondea](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jondea/32/39086_2.png) [@jondea](https://discourse.julialang.org/u/jondea)
#### Post date: [May 23, 2022, 7:52pm UTC](https://discourse.julialang.org/t/detecting-low-precision-contamination-of-doublefloats-jl/77408/7 "2022-05-23T19:52:57Z")

</div>

Even if the final type is Double64, I suspect you cannot be confident that there wasn’t a lower precision number used at some point in the calculation. To test this, multiply a Double64 with a Float32 (or 64) and see what comes out.

---

<div class="post-metadata">

### Author: ![greg\_plowman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/greg_plowman/32/8100_2.png) [@greg\_plowman](https://discourse.julialang.org/u/greg_plowman)
#### Post date: [May 24, 2022, 11:58am UTC](https://discourse.julialang.org/t/detecting-low-precision-contamination-of-doublefloats-jl/77408/8 "2022-05-24T11:58:38Z")

</div>

Would this help?

> [@ANN: ChangePrecision.jl](https://discourse.julialang.org/t/ann-changeprecision-jl/6721):
>
> [ChangePrecision.jl](https://github.com/stevengj/ChangePrecision.jl), which has been mentioned recently on a [couple](https://discourse.julialang.org/t/change-default-precision/6520/47) of [other](https://discourse.julialang.org/t/performing-arithmetic-operations-in-quadruple-precision/4265/14) threads, is now a registered package. This package allows you to effectively change the “default” precision in a large body of Julia code, simply by doing e.g. @changeprecision Float32 include("foo.jl") or, for example: using ChangePrecision @changeprecision BigFloat begin x = pi/2 y = 2.1 \* x \* rand(3,3) \ linspace(0,1,3) end and all floating-point literals, as well as many functions like rand() or expressions…

> **[GitHub - JuliaMath/ChangePrecision.jl: macro to change the default...](https://github.com/JuliaMath/ChangePrecision.jl)**
>
> macro to change the default floating-point precision in Julia code - GitHub - JuliaMath/ChangePrecision.jl: macro to change the default floating-point precision in Julia code

---

<div class="post-metadata">

### Author: ![JeffreySarnoff](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jeffreysarnoff/32/1980_2.png) [@JeffreySarnoff](https://discourse.julialang.org/u/JeffreySarnoff)
#### Post date: [May 24, 2022, 12:19pm UTC](https://discourse.julialang.org/t/detecting-low-precision-contamination-of-doublefloats-jl/77408/9 "2022-05-24T12:19:33Z")

</div>

Hi @HexSpin,

Somehow I missed you March 4th post.  
If you are allowed to share the code in question, please do.  
I am happy to examine it and see what is up.  
If you are loosing that much precision either you have encountered a bug, or your approach is likely to perform better reorganized.  
You may dm me or email the code in question (my email is easy to find).

Best,  
Jeffrey

---

<div class="post-metadata">

### Author: ![HexSpin](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/hexspin/32/19319_2.png) [@HexSpin](https://discourse.julialang.org/u/HexSpin)
#### Post date: [May 24, 2022, 3:45pm UTC](https://discourse.julialang.org/t/detecting-low-precision-contamination-of-doublefloats-jl/77408/10 "2022-05-24T15:45:30Z")

</div>

Thanks for the response. Directly mixing values results in the lower precision value. I think I’m worried about something more subtle. Like an undetected illegal operation etc. But thanks!

---

<div class="post-metadata">

### Author: ![HexSpin](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/hexspin/32/19319_2.png) [@HexSpin](https://discourse.julialang.org/u/HexSpin)
#### Post date: [May 24, 2022, 3:46pm UTC](https://discourse.julialang.org/t/detecting-low-precision-contamination-of-doublefloats-jl/77408/11 "2022-05-24T15:46:23Z")

</div>

Very interesting… I’ll have to fiddle with that and check it out.

---

<div class="post-metadata">

### Author: ![HexSpin](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/hexspin/32/19319_2.png) [@HexSpin](https://discourse.julialang.org/u/HexSpin)
#### Post date: [May 24, 2022, 3:47pm UTC](https://discourse.julialang.org/t/detecting-low-precision-contamination-of-doublefloats-jl/77408/12 "2022-05-24T15:47:58Z")

</div>

I have nothing too top secret in here. I’ll shoot you a copy. No snickering - the beauty of my code can only be described as brutalist.  
Tad

---

<div class="post-metadata">

### Author: ![cjdoris](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cjdoris/32/213133_2.png) [@cjdoris](https://discourse.julialang.org/u/cjdoris)
#### Post date: [May 24, 2022, 9:18pm UTC](https://discourse.julialang.org/t/detecting-low-precision-contamination-of-doublefloats-jl/77408/13 "2022-05-24T21:18:38Z")

</div>

I’d suggest using interval arithmetic ([GitHub - JuliaIntervals/IntervalArithmetic.jl: Rigorous floating-point calculations using interval arithmetic in Julia](https://github.com/JuliaIntervals/IntervalArithmetic.jl)) to bound the actual precision of the result.

---

<div class="post-metadata">

### Author: ![JeffreySarnoff](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jeffreysarnoff/32/1980_2.png) [@JeffreySarnoff](https://discourse.julialang.org/u/JeffreySarnoff)
#### Post date: [May 24, 2022, 9:21pm UTC](https://discourse.julialang.org/t/detecting-low-precision-contamination-of-doublefloats-jl/77408/14 "2022-05-24T21:21:21Z")

</div>

JuliaIntervals.jl is very useful for this sort of thing … as is ArbNumerics.jl

---

<div class="post-metadata">

### Author: ![HexSpin](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/hexspin/32/19319_2.png) [@HexSpin](https://discourse.julialang.org/u/HexSpin)
#### Post date: [May 25, 2022, 11:30am UTC](https://discourse.julialang.org/t/detecting-low-precision-contamination-of-doublefloats-jl/77408/15 "2022-05-25T11:30:13Z")

</div>

Thanks, cjdoris! I’ll investigate.

---

<div class="post-metadata">

### Author: ![HexSpin](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/hexspin/32/19319_2.png) [@HexSpin](https://discourse.julialang.org/u/HexSpin)
#### Post date: [May 28, 2022, 7:56am UTC](https://discourse.julialang.org/t/detecting-low-precision-contamination-of-doublefloats-jl/77408/16 "2022-05-28T07:56:22Z")

</div>

[  
20h](https://discourse.julialang.org/t/precision/81600/48)

Thank you all, but especially @JeffreySarnoff, for your thoughts. While I’m not sure there was an answer to determine if Double64 has been contaminated (and if my thousands of computer hours are valid), you’ve provided me some interesting thoughts about moving forward and coming work!  
In the meantime, I will take comfort in my my results since they tie to BigFloat at high-ish values and that using 32 digit precision assures my first 10 digit result is accurate at the ranges I’m investigating.
