# Vector conversion mixed rational integers

**URL:** <https://discourse.julialang.org/t/vector-conversion-mixed-rational-integers/79546>\
**Category:** General Usage\
**Tags:** rationals, convert, formatting\
**Created:** [April 15, 2022, 5:40pm UTC](https://discourse.julialang.org/t/vector-conversion-mixed-rational-integers/79546 "2022-04-15T17:40:14Z")\
**Posts on this page:** 13\
**Page:** 2

<div class="post-metadata">

**Author:** ![rocco\_sprmnt21](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rocco_sprmnt21/32/20127_2.png) [@rocco\_sprmnt21](https://discourse.julialang.org/u/rocco_sprmnt21)\
**Post date:** [April 16, 2022, 4:28pm UTC](https://discourse.julialang.org/t/vector-conversion-mixed-rational-integers/79546/21 "2022-04-16T16:28:50Z")

</div>

Yes, I am quite familiar with mathematical questions. But naively I thought that in julia it was done differently and I tried to do something like you suggest.  
But I tried with a monster like `Vector {Union {Rational, Int}}[]` that julia didn’t digest 😆

---

<div class="post-metadata">

**Author:** ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)\
**Post date:** [April 16, 2022, 4:31pm UTC](https://discourse.julialang.org/t/vector-conversion-mixed-rational-integers/79546/22 "2022-04-16T16:31:44Z")

</div>

> [@Henrique\_Becker](#):
>
> What I find a little strange that `Int` is not a subtype of `Rational` .

Fyi, there is [this past post](https://discourse.julialang.org/t/numeric-types-and-their-relationships/7869/5) from the boss that might still be valid.

---

<div class="post-metadata">

**Author:** ![goerch](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/goerch/32/29122_2.png) [@goerch](https://discourse.julialang.org/u/goerch)\
**Post date:** [April 16, 2022, 4:34pm UTC](https://discourse.julialang.org/t/vector-conversion-mixed-rational-integers/79546/23 "2022-04-16T16:34:46Z")

</div>

> [@rocco\_sprmnt21](#):
>
> `Vector {Union {Rational, Int}}[]`

You were near!

```julia
@show Union{Real,Int}[0, 1//2, 1, 3//2, 4//2]
@show Union{Rational,Int}[0, 1//2, 1, 3//2, 4//2]

```

yields

```julia
Union{Real, Int}[0, 1 // 2, 1, 3 // 2, 4 // 2] = Real[0, 1//2, 1, 3//2, 2//1]
Union{Rational, Int}[0, 1 // 2, 1, 3 // 2, 4 // 2] = Union{Int64, Rational}[0, 1//2, 1, 3//2, 2//1]

```

---

<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:** [April 16, 2022, 5:17pm UTC](https://discourse.julialang.org/t/vector-conversion-mixed-rational-integers/79546/24 "2022-04-16T17:17:30Z")

</div>

That is a very good point from Stefan. But, yet, I kinda expected an `AbstractRational` which `Integer` inherits, but the concrete `Rational` only is defined after `Int` is defined. The _conventionally identified with this subset, they are not the same as this subset_ still holds however.

---

<div class="post-metadata">

**Author:** ![rocco\_sprmnt21](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rocco_sprmnt21/32/20127_2.png) [@rocco\_sprmnt21](https://discourse.julialang.org/u/rocco_sprmnt21)\
**Post date:** [April 16, 2022, 6:16pm UTC](https://discourse.julialang.org/t/vector-conversion-mixed-rational-integers/79546/25 "2022-04-16T18:16:05Z")

</div>

To stay at the problem Y, why ther are two differente types of rational?

```julia
julia> Rational{Int}===Rational
false

```

What is the meaning of the first type (Rational {Int})?

to return to problem X anhoter function to get nice Xticks

```julia

siwp(n,d)=if isinteger(n//d) string(Int(n/d)) else string(n,"/",d) end

```

---

<div class="post-metadata">

**Author:** ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)\
**Post date:** [April 16, 2022, 6:53pm UTC](https://discourse.julialang.org/t/vector-conversion-mixed-rational-integers/79546/26 "2022-04-16T18:53:38Z")

</div>

> [@rocco\_sprmnt21](#):
>
> `Int(n/d)`

Instead of converting floats, it is better to use proper integer division: `div(n, d)` or `n÷d`.

---

<div class="post-metadata">

**Author:** ![JeffreySarnoff](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jeffreysarnoff/32/1980_2.png) [@JeffreySarnoff](https://discourse.julialang.org/u/JeffreySarnoff)\
**Post date:** [April 16, 2022, 7:16pm UTC](https://discourse.julialang.org/t/vector-conversion-mixed-rational-integers/79546/27 "2022-04-16T19:16:05Z")

</div>

Some reasonably different types of rational

```julia
julia> Rational !- Rational{Int}, Rational{Int} != Rational{BigInt}
(true, true)

julia> typeof(Rational(1, 2)), typeof(Rational(big"1", big"2"))
(Rational{Int64}, Rational{BigInt})

julia> sizeof(Rational(1, 2)), sizeof(Rational(Int16(1), Int16(2)))
(16, 4)

julia> typeof(Rational), Rational.body
UnionAll, Rational{T<:Integer}

```

---

<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:** [April 16, 2022, 7:23pm UTC](https://discourse.julialang.org/t/vector-conversion-mixed-rational-integers/79546/28 "2022-04-16T19:23:41Z")

</div>

This is the definition of `Rational`:

```julia
"""
    Rational{T<:Integer} <: Real

Rational number type, with numerator and denominator of type `T`.
Rationals are checked for overflow.
"""
struct Rational{T<:Integer} <: Real
    num::T
    den::T

    # Unexported inner constructor of Rational that bypasses all checks
    global unsafe_rational(::Type{T}, num, den) where {T} = new{T}(num, den)
end

```

So, `Rational{Int}` stores the numerator and denominator as `Int`, while `Rational{BigInt}` (for example) uses `BigInt` (and it is therefore an “infinite” precision rational).

---

<div class="post-metadata">

**Author:** ![Jean\_Michel](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jean_michel/32/8282_2.png) [@Jean\_Michel](https://discourse.julialang.org/u/Jean_Michel)\
**Post date:** [April 17, 2022, 11:34am UTC](https://discourse.julialang.org/t/vector-conversion-mixed-rational-integers/79546/29 "2022-04-17T11:34:18Z")

</div>

I think the whole tread is related to the issue of the default printing of Rationals which is not I think the best possible. I proposed [https://github.com/JuliaLang/julia/issues/42626](https://github.com/JuliaLang/julia/issues/42626)  
which was rather well received but no action followed…

---

<div class="post-metadata">

**Author:** ![rocco\_sprmnt21](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rocco_sprmnt21/32/20127_2.png) [@rocco\_sprmnt21](https://discourse.julialang.org/u/rocco_sprmnt21)\
**Post date:** [April 17, 2022, 3:53pm UTC](https://discourse.julialang.org/t/vector-conversion-mixed-rational-integers/79546/30 "2022-04-17T15:53:02Z")

</div>

out of curiosity I made these tests:

```julia
julia> 3//true
3//1

julia> false//5
0//1

julia> typeof(false//5)
Rational{Int64}

```

---

<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:** [April 17, 2022, 4:00pm UTC](https://discourse.julialang.org/t/vector-conversion-mixed-rational-integers/79546/31 "2022-04-17T16:00:01Z")

</div>

The type of the numerator and the denominator needs to be the same. So it gets automatically converted to the largest between the two. You can, however, do:

```julia
julia> false // true
false//true

julia> typeof(false // true)
Rational{Bool}

```

---

<div class="post-metadata">

**Author:** ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)\
**Post date:** [April 17, 2022, 4:06pm UTC](https://discourse.julialang.org/t/vector-conversion-mixed-rational-integers/79546/32 "2022-04-17T16:06:41Z")

</div>

`true` and `false` are integer (sub)types and when mixed with `Int64` get converted to `Int64` and:

```julia
false//5 == 0//5 == 0//1 # Rational{Int64}

```

If instead we mix Bool with `UInt8` integers we get:

```julia
false//UInt8(10) # Rational{UInt8}

```

---

<div class="post-metadata">

**Author:** ![rocco\_sprmnt21](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rocco_sprmnt21/32/20127_2.png) [@rocco\_sprmnt21](https://discourse.julialang.org/u/rocco_sprmnt21)\
**Post date:** [April 17, 2022, 4:48pm UTC](https://discourse.julialang.org/t/vector-conversion-mixed-rational-integers/79546/33 "2022-04-17T16:48:26Z")

</div>

Yup. I know.  
For this reason I was certain when I tested that the input would be accepted.

what can be done with rationals {boolean} is another matter entirely.

[Previous page](https://discourse.julialang.org/t/vector-conversion-mixed-rational-integers/79546.md?page=1)
