# Way to enforce \`BigFloat\` math by default

**URL:** <https://discourse.julialang.org/t/way-to-enforce-bigfloat-math-by-default/112557>\
**Category:** General Usage\
**Tags:** question\
**Created:** [April 5, 2024, 4:18am UTC](https://discourse.julialang.org/t/way-to-enforce-bigfloat-math-by-default/112557 "2024-04-05T04:18:19Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![tgross35](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tgross35/32/38591_2.png) [@tgross35](https://discourse.julialang.org/u/tgross35)\
**Post date:** [April 5, 2024, 4:18am UTC](https://discourse.julialang.org/t/way-to-enforce-bigfloat-math-by-default/112557/1 "2024-04-05T04:18:19Z")

</div>

```julia
julia> big(pi)/2
1.570796326794896619231321691639751442098584699687552910487472296153908203143099

julia> big(pi/2)
1.5707963267948965579989817342720925807952880859375

```

That is quite an easy mistake to make. Is there a way to do something like set the default numeric type for literals to `BigFloat`, to help avoid silently wrong math?

Overriding the builtin values with something like `pi = big(pi)` would also help, but unfortunately this is not allowed.

---

<div class="post-metadata">

**Author:** ![jar1](https://avatars.discourse-cdn.com/v4/letter/j/c0e974/32.png) [@jar1](https://discourse.julialang.org/u/jar1)\
**Post date:** [April 5, 2024, 5:37am UTC](https://discourse.julialang.org/t/way-to-enforce-bigfloat-math-by-default/112557/2 "2024-04-05T05:37:24Z")

</div>

I bet it would be possible to write a JET analyzer type that checks for floats.

> **["Unstable API" Analysis · JET.jl](https://aviatesk.github.io/JET.jl/dev/generated-plugin-examples/find_unstable_api/#%22Unstable-API%22-Analysis)**
>
> Documentation for JET.jl.

---

<div class="post-metadata">

**Author:** ![rfourquet](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rfourquet/32/3610_2.png) [@rfourquet](https://discourse.julialang.org/u/rfourquet)\
**Post date:** [April 5, 2024, 6:51am UTC](https://discourse.julialang.org/t/way-to-enforce-bigfloat-math-by-default/112557/3 "2024-04-05T06:51:52Z")

</div>

That’s the purpose of the SwapLiterals.jl and [SafeREPL.jl](https://github.com/rfourquet/SafeREPL.jl) packages. At the REPL:

```julia
julia> using SafeREPL

julia> swapliterals!(Float64 => :BigFloat) # `BigFloat` function is applied to Float64 literals

julia> 1.2 # identical to `BigFloat(1.2)`
1.1999999999999999555910790149937383830547332763671875

julia> big"1.2"
1.200000000000000000000000000000000000000000000000000000000000000000000000000007

julia> swapliterals!(Float64 => "@big_str") # apply `@big_str` macro to stringified Float64 literals
1.200000000000000000000000000000000000000000000000000000000000000000000000000007

julia> pi/2 # no Float64 literals, so vanilla julia
1.5707963267948966

julia> pi/2.0
1.570796326794896619231321691639751442098584699687552910487472296153908203143099

julia> swapliterals!(Float64 => :BigFloat, Int => :BigInt) # convert Int literals into BigInt

julia> pi/2
1.570796326794896619231321691639751442098584699687552910487472296153908203143099

```

To swap literals in code (not in REPL) :

```julia
julia> using SwapLiterals; @swapliterals Float64 => "@big_str" 1.2
1.200000000000000000000000000000000000000000000000000000000000000000000000000007

```

---

<div class="post-metadata">

**Author:** ![photor](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/photor/32/14343_2.png) [@photor](https://discourse.julialang.org/u/photor)\
**Post date:** [April 5, 2024, 9:02am UTC](https://discourse.julialang.org/t/way-to-enforce-bigfloat-math-by-default/112557/4 "2024-04-05T09:02:03Z")

</div>

that’s not “swap”, that’s "assign"😆

---

<div class="post-metadata">

**Author:** ![nsajko](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nsajko/32/221187_2.png) [@nsajko](https://discourse.julialang.org/u/nsajko)\
**Post date:** [April 5, 2024, 9:20am UTC](https://discourse.julialang.org/t/way-to-enforce-bigfloat-math-by-default/112557/5 "2024-04-05T09:20:38Z")

</div>

Just do `pi = BigFloat(MathConstants.pi)`.

---

<div class="post-metadata">

**Author:** ![mkitti](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mkitti/32/12459_2.png) [@mkitti](https://discourse.julialang.org/u/mkitti)\
**Post date:** [April 5, 2024, 6:28pm UTC](https://discourse.julialang.org/t/way-to-enforce-bigfloat-math-by-default/112557/6 "2024-04-05T18:28:25Z")

</div>

You may be interested in SafeREPL.jl:

> [@\[ANN\] SafeREPL: use BigInt by default at the REPL](https://discourse.julialang.org/t/ann-saferepl-use-bigint-by-default-at-the-repl/41271):
>
> With Julia 1.5 around the corner, I’m happy to present [SafeREPL](https://github.com/rfourquet/SafeREPL.jl), a little experimental package which by default interprets Int and Int128 REPL literals as BigInt, and Float64 literals as BigFloat: julia\> using SafeREPL julia\> factorial(40) 815915283247897734345611269596115894272000000000 julia\> sqrt(2.0) 1.414213562373095048801688724209698078569671875376948073176679737990732478462102 julia\> c = 299\_792\_458; c^3 # the cube of the speed of light can even be computed safely! 26944002417373989…

```julia-repl
julia> using SafeREPL

julia> π
π = 3.1415926535897...

julia> π/2
1.570796326794896619231321691639751442098584699687552910487472296153908203143099

julia> BigFloat(π)/2
1.570796326794896619231321691639751442098584699687552910487472296153908203143099

```

---

<div class="post-metadata">

**Author:** ![mtfishman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mtfishman/32/30755_2.png) [@mtfishman](https://discourse.julialang.org/u/mtfishman)\
**Post date:** [April 5, 2024, 8:37pm UTC](https://discourse.julialang.org/t/way-to-enforce-bigfloat-math-by-default/112557/7 "2024-04-05T20:37:12Z")

</div>

Or “replace”.
