# Is it possible to set a floating point precision while evaluating jldoctest blocks in docstrings?

**URL:** https://discourse.julialang.org/t/is-it-possible-to-set-a-floating-point-precision-while-evaluating-jldoctest-blocks-in-docstrings/59528
**Category:** General Usage
**Tags:** documenter, float, precision
**Created:** [April 18, 2021, 8:21am UTC](https://discourse.julialang.org/t/is-it-possible-to-set-a-floating-point-precision-while-evaluating-jldoctest-blocks-in-docstrings/59528 "2021-04-18T08:21:24Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![jishnub](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jishnub/32/33620_2.png) [@jishnub](https://discourse.julialang.org/u/jishnub)
#### Post date: [April 18, 2021, 8:21am UTC](https://discourse.julialang.org/t/is-it-possible-to-set-a-floating-point-precision-while-evaluating-jldoctest-blocks-in-docstrings/59528/1 "2021-04-18T08:21:24Z")

</div>

I’m not too familiar with Documenter, so this might have a solution that I’m not aware of. Is it possible to set a filter to compare only the first `n` digits of floating-point numbers in a `jldoctest` block? Either of a global or a local setting would be fine.

---

<div class="post-metadata">

### Author: ![mortenpi](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mortenpi/32/158_2.png) [@mortenpi](https://discourse.julialang.org/u/mortenpi)
#### Post date: [April 18, 2021, 8:45am UTC](https://discourse.julialang.org/t/is-it-possible-to-set-a-floating-point-precision-while-evaluating-jldoctest-blocks-in-docstrings/59528/2 "2021-04-18T08:45:57Z")

</div>

It’s not quite perfect for this, but you can set regexes which filter out parts of the output before it’s compared: [Doctests · Documenter.jl](https://juliadocs.github.io/Documenter.jl/stable/man/doctests/#Filtering-Doctests).

---

<div class="post-metadata">

### Author: ![iago-lito](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/iago-lito/32/31924_2.png) [@iago-lito](https://discourse.julialang.org/u/iago-lito)
#### Post date: [September 21, 2022, 5:13pm UTC](https://discourse.julialang.org/t/is-it-possible-to-set-a-floating-point-precision-while-evaluating-jldoctest-blocks-in-docstrings/59528/3 "2022-09-21T17:13:02Z")

</div>

@jishnub I would caution against this and make use of Julia’s `≈` operator (aka. [isapprox](https://docs.julialang.org/en/v1/base/math/#Base.isapprox)) instead. Because [IEEE754](https://fr.wikipedia.org/wiki/IEEE_754) floating-point numerical precision does **not** guarantee that any calculation be correct after some _fixed_ number of decimals. Your tests would maybe appear to work at first, but they would be fundamentally broken IMO.

---

<div class="post-metadata">

### Author: ![cmichelenstrofer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cmichelenstrofer/32/44392_2.png) [@cmichelenstrofer](https://discourse.julialang.org/u/cmichelenstrofer)
#### Post date: [December 1, 2022, 5:41am UTC](https://discourse.julialang.org/t/is-it-possible-to-set-a-floating-point-precision-while-evaluating-jldoctest-blocks-in-docstrings/59528/4 "2022-12-01T05:41:56Z")

</div>

How can you use ≈ in a jldoctest?

---

<div class="post-metadata">

### Author: ![iago-lito](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/iago-lito/32/31924_2.png) [@iago-lito](https://discourse.julialang.org/u/iago-lito)
#### Post date: [December 6, 2022, 9:31am UTC](https://discourse.julialang.org/t/is-it-possible-to-set-a-floating-point-precision-while-evaluating-jldoctest-blocks-in-docstrings/59528/5 "2022-12-06T09:31:26Z")

</div>

@cmichelenstrofer With constructs like:

```nohighlight
julia> my_matrix ≈ [
    0.58 0.55 0.53
    0 0.89 1.0
    1.45 22.5 -0.6
    ]
true 

```

I guess?

---

<div class="post-metadata">

### Author: ![fredrikekre](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fredrikekre/32/1688_2.png) [@fredrikekre](https://discourse.julialang.org/u/fredrikekre)
#### Post date: [December 6, 2022, 12:31pm UTC](https://discourse.julialang.org/t/is-it-possible-to-set-a-floating-point-precision-while-evaluating-jldoctest-blocks-in-docstrings/59528/6 "2022-12-06T12:31:23Z")

</div>

> <https://github.com/JuliaDocs/Documenter.jl/pull/1989>
>
> This patch adds support for using regex =\> substitution pairs in doctest filters…. This is useful when you do not want to replace the full match, but only remove some part. As an example, consider a test where the output may differ in the third decimal:
> 
> \`\`\`\`
> \`\`\`jlddoctest
> julia\> 1.124
> 1.123
> \`\`\`
> \`\`\`\`
> 
> With this patch it is still possible to test the first 2 decimals using the filter \`r"(\\d\\.\\d\\d)\\d\*" =\> s"\\1"\`.
