# \[ANN\] SuffixConversion.jl - make it easier to write generic code with numerical literals

**URL:** https://discourse.julialang.org/t/ann-suffixconversion-jl-make-it-easier-to-write-generic-code-with-numerical-literals/101679
**Category:** Package Announcements
**Created:** [July 16, 2023, 6:53pm UTC](https://discourse.julialang.org/t/ann-suffixconversion-jl-make-it-easier-to-write-generic-code-with-numerical-literals/101679 "2023-07-16T18:53:57Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![simonbyrne](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/simonbyrne/32/19_2.png) [@simonbyrne](https://discourse.julialang.org/u/simonbyrne)
#### Post date: [July 16, 2023, 6:53pm UTC](https://discourse.julialang.org/t/ann-suffixconversion-jl-make-it-easier-to-write-generic-code-with-numerical-literals/101679/1 "2023-07-16T18:53:57Z")

</div>

I’m announcing [SuffixConversion.jl](https://github.com/simonbyrne/SuffixConversion.jl), which aims to make it easier to write generic code involving numeric literals without unintentional type promotion. An example:

```julia
using SuffixConversion

function addhalf(x)
    @suffix FT = typeof(x)
    return x + 0.5_FT
end    

```

Thoughts/comments welcome!

---

<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: [July 16, 2023, 7:00pm UTC](https://discourse.julialang.org/t/ann-suffixconversion-jl-make-it-easier-to-write-generic-code-with-numerical-literals/101679/2 "2023-07-16T19:00:25Z")

</div>

It would be helpful to show the normal way of doing the conversion without syntactic sugar so we can have something familiar to compare to.

---

<div class="post-metadata">

### Author: ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)
#### Post date: [July 16, 2023, 7:02pm UTC](https://discourse.julialang.org/t/ann-suffixconversion-jl-make-it-easier-to-write-generic-code-with-numerical-literals/101679/3 "2023-07-16T19:02:15Z")

</div>

Note that this still seems to lose precision in many cases if the starting number is not exactly represented as a `Float64` literal, e.g.:

```julia
julia> using DecFP

julia> @suffix Dec128
SuffixConversion.SuffixConverter{Dec128}()

julia> 0.1_Dec128
0.1000000000000000055511151231257827

```

(You work around this [for `BigFloat` only](https://github.com/simonbyrne/SuffixConversion.jl/blob/ac4cba6b7629c619ad6328c6e6a3fc37915def42/src/SuffixConversion.jl#L17) by converting to `string` first. You could do this for all types, of course, but then performance would really suffer.)

---

<div class="post-metadata">

### Author: ![simonbyrne](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/simonbyrne/32/19_2.png) [@simonbyrne](https://discourse.julialang.org/u/simonbyrne)
#### Post date: [July 16, 2023, 7:05pm UTC](https://discourse.julialang.org/t/ann-suffixconversion-jl-make-it-easier-to-write-generic-code-with-numerical-literals/101679/4 "2023-07-16T19:05:08Z")

</div>

> [@jar1](#):
>
> It would be helpful to show the normal way of doing the conversion without syntactic sugar so we can have something familiar to compare to.

See the bottom of the README?

---

<div class="post-metadata">

### Author: ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)
#### Post date: [July 16, 2023, 7:06pm UTC](https://discourse.julialang.org/t/ann-suffixconversion-jl-make-it-easier-to-write-generic-code-with-numerical-literals/101679/5 "2023-07-16T19:06:33Z")

</div>

> [@jar1](#):
>
> It would be helpful to show the normal way of doing the conversion without syntactic sugar so we can have something familiar to compare to.

`FT(0.5)`. If you have a rational number that is not exactly representable by a `Float64` literal, however, you’re better off using a rational expression, e.g. `FT(1//10)` rather than `FT(0.1)`. (See my post above.)

---

<div class="post-metadata">

### Author: ![Mason](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mason/32/2423_2.png) [@Mason](https://discourse.julialang.org/u/Mason)
#### Post date: [July 16, 2023, 7:14pm UTC](https://discourse.julialang.org/t/ann-suffixconversion-jl-make-it-easier-to-write-generic-code-with-numerical-literals/101679/6 "2023-07-16T19:14:41Z")

</div>

> [@jar1](#):
>
> It would be helpful to show the normal way of doing the conversion without syntactic sugar so we can have something familiar to compare to.

in this case it’s just

```julia
struct Converter{T} end
Base.:(*)(x, ::Converter{T}) where {T} = convert(T, x)

```

```julia
function addhalf(x)
    _FT = Converter{typeof(x)}()
   return x + 0.5_FT
end

```

---

<div class="post-metadata">

### Author: ![simonbyrne](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/simonbyrne/32/19_2.png) [@simonbyrne](https://discourse.julialang.org/u/simonbyrne)
#### Post date: [July 16, 2023, 7:24pm UTC](https://discourse.julialang.org/t/ann-suffixconversion-jl-make-it-easier-to-write-generic-code-with-numerical-literals/101679/7 "2023-07-16T19:24:25Z")

</div>

Good point, I can add that to the README.
