# Julian way of defining custom numeric types

**URL:** https://discourse.julialang.org/t/julian-way-of-defining-custom-numeric-types/53866
**Category:** New to Julia
**Created:** [January 24, 2021, 4:50pm UTC](https://discourse.julialang.org/t/julian-way-of-defining-custom-numeric-types/53866 "2021-01-24T16:50:22Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![cshen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cshen/32/217287_2.png) [@cshen](https://discourse.julialang.org/u/cshen)
#### Post date: [January 24, 2021, 4:50pm UTC](https://discourse.julialang.org/t/julian-way-of-defining-custom-numeric-types/53866/1 "2021-01-24T16:50:22Z")

</div>

Hello,

I am working with the DifferentialEquations package to play around some differential equations involving angles.

Instead of letting everything go on as a normal float and just afterwards reduce everything modulo 2pi for visualisation, i wanted to try to go the julian way and define a small wrapper for the Floag64 type

```julia
struct Angle <: Real
    x::Float64
end
Angle(x::Real) = Angle(x % 2pi)

```

Unfortunately this is not enough to make for a smooth addition to my program as julia struggles to convert and does not know what to do with this new type.

I know that i can extend all the base operations by hand, but i was left wondering if there is a smoother and easier way to let a simple primitive Number type wrapper just automatically fall in place the rest of the Julia implementation.

More in general i tried to look for some quick guidelines when creating a new custom type but i could not find one. Are there any Best Practices ™ when it comes to seamlessly embed a new custom type in the language?

Thanks!

---

<div class="post-metadata">

### Author: ![Tomas\_Pevny](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tomas_pevny/32/25466_2.png) [@Tomas\_Pevny](https://discourse.julialang.org/u/Tomas_Pevny)
#### Post date: [January 24, 2021, 5:24pm UTC](https://discourse.julialang.org/t/julian-way-of-defining-custom-numeric-types/53866/2 "2021-01-24T17:24:35Z")

</div>

You should find an inspiration in manual describing the implementation of rational number.

[https://docs.julialang.org/en/v1/manual/conversion-and-promotion/](https://docs.julialang.org/en/v1/manual/conversion-and-promotion/)

---

<div class="post-metadata">

### Author: ![Henrique\_Becker](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/henrique_becker/32/15443_2.png) [@Henrique\_Becker](https://discourse.julialang.org/u/Henrique_Becker)
#### Post date: [January 24, 2021, 5:28pm UTC](https://discourse.julialang.org/t/julian-way-of-defining-custom-numeric-types/53866/3 "2021-01-24T17:28:42Z")

</div>

Also, I think you can benefit from the macro `@forward` that I describe in [this reply of mine](https://discourse.julialang.org/t/confused-with-type-definition/53115/13).

---

<div class="post-metadata">

### Author: ![cshen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cshen/32/217287_2.png) [@cshen](https://discourse.julialang.org/u/cshen)
#### Post date: [January 24, 2021, 5:51pm UTC](https://discourse.julialang.org/t/julian-way-of-defining-custom-numeric-types/53866/4 "2021-01-24T17:51:34Z")

</div>

This is extremely interesting but the `@forward` macro seems to not retain the type as in

```julia
struct Angle <: Real
    x::Float64
end

@forward Angle.x Base.sqrt

```

then

```julia
typeof(Base.sqrt(Angle(1))) == Float64 #true

```

and that is not really what i need. Moreover, using forward on `Base.:+` raises a `MethodError` anyway as there is ambiguity as the macro defines

```julia
+(x::Angle, args...; kwargs...)

```

I think i’ll stick the the “not lazy” version of embedding a custom type for now.

---

<div class="post-metadata">

### Author: ![Henrique\_Becker](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/henrique_becker/32/15443_2.png) [@Henrique\_Becker](https://discourse.julialang.org/u/Henrique_Becker)
#### Post date: [January 24, 2021, 6:08pm UTC](https://discourse.julialang.org/t/julian-way-of-defining-custom-numeric-types/53866/5 "2021-01-24T18:08:03Z")

</div>

True, maybe `eval` would be more the case here.

```julia
for single_param_function in (:(Base.sqrt), :(Base.sin))
    eval(quote
        function $(single_param_function)(a :: Angle)
            return Angle($(single_param_function)(a.x))
        end
    end)
end

```

Someone with better knowledge of metaprogramming should check if this is reasonable (it works, but maybe is not ideal).

---

<div class="post-metadata">

### Author: ![sostock](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sostock/32/5546_2.png) [@sostock](https://discourse.julialang.org/u/sostock)
#### Post date: [January 24, 2021, 7:28pm UTC](https://discourse.julialang.org/t/julian-way-of-defining-custom-numeric-types/53866/6 "2021-01-24T19:28:10Z")

</div>

> [@cshen](#):
>
> `Angle(x::Real) = Angle(x % 2pi)`

Note that you will get more accurate results by using `mod2pi(x)` instead of `x % 2pi` (since the latter uses `2pi`, a `Float64` approximation of the actual number 2π).

---

<div class="post-metadata">

### Author: ![Per](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/per/32/10387_2.png) [@Per](https://discourse.julialang.org/u/Per)
#### Post date: [January 25, 2021, 7:10am UTC](https://discourse.julialang.org/t/julian-way-of-defining-custom-numeric-types/53866/7 "2021-01-25T07:10:42Z")

</div>

Is the sine of an angle another angle or just a number? If you don’t need it to be an angle, then simply defining `Base.float` for your type will work for a lot of functions that use this as their fallback.

```julia
julia> Base.float(a::Angle) = a.x

julia> sin(Angle(1))
0.8414709848078965

```
