# Code specialization on literals for addition and multiplication operators

**URL:** <https://discourse.julialang.org/t/code-specialization-on-literals-for-addition-and-multiplication-operators/60775>\
**Category:** General Usage\
**Created:** [May 8, 2021, 12:27pm UTC](https://discourse.julialang.org/t/code-specialization-on-literals-for-addition-and-multiplication-operators/60775 "2021-05-08T12:27:19Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Lecrapouille](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lecrapouille/32/24071_2.png) [@Lecrapouille](https://discourse.julialang.org/u/Lecrapouille)\
**Post date:** [May 8, 2021, 12:27pm UTC](https://discourse.julialang.org/t/code-specialization-on-literals-for-addition-and-multiplication-operators/60775/1 "2021-05-08T12:27:19Z")

</div>

Hi !

This code is working:

```julia
struct MP v::Float64 end
Base.:(*)(x::MP, y::MP) = MP(x.v + y.v)
Base.:(*)(x::MP, y::Real) = MP(x.v + y)
Base.:(*)(x::Real, y::MP) = MP(x + y.v)

```

But on a corner case, this is giving me correct result point of view a computer science :

```julia
julia> MP(-Inf) * MP(Inf)
MP(NaN)

```

But I want to force it to return a different result in accordance to my algebra =\> `MP(-Inf)`. I tried to follow the idea base on `literal_pow` but without success:

```julia
@inline Base.:(*)(::Val{MP(Inf)}, ::Val{MP(-Inf)}) = MP(-Inf)
@inline Base.:(*)(::Val{MP(-Inf)}, ::Val{MP(Inf)}) = MP(-Inf)

```

Still returning me NaN. I succeeded with literal\_pow but for operators + and \* seems the compiler is not working in the same manner. How to reach the same result without adding an if-then-else in my function ?

Thanks in advance !

---

<div class="post-metadata">

**Author:** ![simeonschaub](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/simeonschaub/32/216566_2.png) [@simeonschaub](https://discourse.julialang.org/u/simeonschaub)\
**Post date:** [May 8, 2021, 12:33pm UTC](https://discourse.julialang.org/t/code-specialization-on-literals-for-addition-and-multiplication-operators/60775/2 "2021-05-08T12:33:11Z")

</div>

> [@Lecrapouille](#):
>
> How to reach the same result without adding an if-then-else in my function ?

You can’t. You can only dispatch on types, not on values. `literal_pow` is a special case in lowering, so there is no way to extend it to other functions or non-literals. Using an `if` condition should be just as fast though.

---

<div class="post-metadata">

**Author:** ![Lecrapouille](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lecrapouille/32/24071_2.png) [@Lecrapouille](https://discourse.julialang.org/u/Lecrapouille)\
**Post date:** [May 8, 2021, 12:36pm UTC](https://discourse.julialang.org/t/code-specialization-on-literals-for-addition-and-multiplication-operators/60775/3 "2021-05-08T12:36:18Z")

</div>

Thanks ! Bad news ☹

This is working `Val(MP(Inf)) * Val(MP(-Inf))` so maybe I’ll have to create a special type.
