# Inexact error with rand function

**URL:** <https://discourse.julialang.org/t/inexact-error-with-rand-function/59954>\
**Category:** New to Julia\
**Tags:** distributions, random\
**Created:** [April 24, 2021, 9:20pm UTC](https://discourse.julialang.org/t/inexact-error-with-rand-function/59954 "2021-04-24T21:20:18Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Wei\_Yang](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/wei_yang/32/23951_2.png) [@Wei\_Yang](https://discourse.julialang.org/u/Wei_Yang)\
**Post date:** [April 24, 2021, 9:20pm UTC](https://discourse.julialang.org/t/inexact-error-with-rand-function/59954/1 "2021-04-24T21:20:18Z")

</div>

I have defined the following distribution

```julia
using Distributions, Random

struct ComplexNormal <:ContinuousUnivariateDistribution
    μ::ComplexF64
    σ::Float64
    function ComplexNormal(μ=0,σ=1) new(μ,σ) end
end

Base.rand(rng::AbstractRNG, d::ComplexNormal) =randn(ComplexF64)*d.σ+d.μ;

```

`rand(ComplexNormal(1+2im,1))` generates a single element works.

`rand(ComplexNormal(1+2im,1),2)` (which should generate a vector of 2 elements) don’t quiet work, as it gives  
`InexactError: Float64(0.37414329354088083 + 1.039498256924925im)`

**I wonder how I can make this work.**  
One can certainly use list comprehension `[rand(ComplexNormal()) for i=1:2]`  
But note that the following real counterpart works, it will be nice if the complex case also works.

```julia
using Distributions, Random
struct myNormal <:ContinuousUnivariateDistribution
    μ::ComplexF64
    σ::Float64
    function myNormal(μ=0,σ=1) new(μ,σ) end
end

Base.rand(rng::AbstractRNG, d::myNormal) = randn()*d.σ+d.μ;

rand(myNormal(1+2,1),2)
2-element Vector{Float64}:
 2.9376553287521845
 1.2461248703275385

```

However,

```julia
rand(myNormal(1+2im,1),2)
InexactError: Float64(1.621501920542256 + 2.0im)

```

and I noticed that in Julia, Real is not a subtype of Complex, I wonder if there is a reason behind not to make Real a subtype of Complex.

Thank you !

---

<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:** [April 24, 2021, 9:49pm UTC](https://discourse.julialang.org/t/inexact-error-with-rand-function/59954/2 "2021-04-24T21:49:53Z")

</div>

> [@Wei\_Yang](#):
>
> `rand(ComplexNormal(1+2im,1),2)` (which should generate a vector of 2 elements) don’t quiet work, as it gives  
> `InexactError: Float64(0.37414329354088083 + 1.039498256924925im)`

Define

```julia
Base.eltype(::Type{ComplexNormal}) = ComplexF64

```

so that Julia knows that the output type is `ComplexF64` rather than `Float64` (which is the [default for continuous distributions](https://github.com/JuliaStats/Distributions.jl/blob/a36c613f5434c76ef21d3ed2d4a80fab66068d19/src/common.jl#L59)).

> [@Wei\_Yang](#):
>
> I noticed that in Julia, Real is not a subtype of Complex, I wonder if there is a reason behind not to make Real a subtype of Complex.

See also this discussion: [What problems will one have, if `Real` was made a subtype of `Complex`?](https://discourse.julialang.org/t/what-problems-will-one-have-if-real-was-made-a-subtype-of-complex/26662)

---

<div class="post-metadata">

**Author:** ![Wei\_Yang](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/wei_yang/32/23951_2.png) [@Wei\_Yang](https://discourse.julialang.org/u/Wei_Yang)\
**Post date:** [April 24, 2021, 10:02pm UTC](https://discourse.julialang.org/t/inexact-error-with-rand-function/59954/3 "2021-04-24T22:02:51Z")

</div>

Thank you for the solution!

The linked discussion is interesting, the post (which come up in the linked discussion) [Abstract type between `AbstractFloat` and `Real`](https://github.com/JuliaLang/julia/issues/15086), answered my second question, and provided some insights in the development of Julia.
