# 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:** 20
**Page:** 1

<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 15, 2022, 5:40pm UTC](https://discourse.julialang.org/t/vector-conversion-mixed-rational-integers/79546/1 "2022-04-15T17:40:14Z")

</div>

Is there (or how could one write) a function that gives me this vector `[0, 1//2, 1, 3//2, 2]` from this vector `[0//2, 1//2, 2//2, 3//2, 4//2]`?

---

<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 15, 2022, 5:48pm UTC](https://discourse.julialang.org/t/vector-conversion-mixed-rational-integers/79546/2 "2022-04-15T17:48:04Z")

</div>

Does

```julia
[numerator(r) == 0 ? 0 : denominator(r) == 1 ? Int(r) : r for r in [0//2, 1//2, 2//2, 3//2, 4//2]]

```

help?

---

<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 15, 2022, 5:56pm UTC](https://discourse.julialang.org/t/vector-conversion-mixed-rational-integers/79546/3 "2022-04-15T17:56:07Z")

</div>

> [@goerch](#):
>
> `[numerator(r) == 0 ? 0 : denominator(r) == 1 ? Int(r) : r for r in [0//2, 1//2, 2//2, 3//2, 4//2]]`

HELP!

 ![belliticks](https://global.discourse-cdn.com/julialang/original/3X/6/4/645f21fa077d633248e2d2814b3b4135fe1826fd.png)

```julia

rng=[numerator(r) == 0 ? 0 : denominator(r) == 1 ? Int(r) : r for r in [n//2 for n in 0:4]]

axes[1].xtickformat = xs->["$(x)π" for x in rng]

```

---

<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 15, 2022, 6:06pm UTC](https://discourse.julialang.org/t/vector-conversion-mixed-rational-integers/79546/4 "2022-04-15T18:06:11Z")

</div>

this too seems to get there

```julia
julia> map(0:4) do n
           try
           Int(n//2)
       catch
           n//2
       end
       end
5-element Vector{Real}:
  0
 1//2
  1
 3//2
  2

```

I wonder what is the logic applied to solve Int (n // d)

---

<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, 7:31am UTC](https://discourse.julialang.org/t/vector-conversion-mixed-rational-integers/79546/5 "2022-04-16T07:31:26Z")

</div>

Something like this seems to work too:

```julia
x = [0//2, 1//2, 2//2, 3//2, 4//2]
map(u -> (y=floor(Int,u); u == y ? y : u), x)

# result:
5-element Vector{Real}:
  0
 1//2
  1
 3//2
  2

```

---

<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, 11:47am UTC](https://discourse.julialang.org/t/vector-conversion-mixed-rational-integers/79546/6 "2022-04-16T11:47:49Z")

</div>

Out of curiosity: what’s this for?

---

<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, 12:58pm UTC](https://discourse.julialang.org/t/vector-conversion-mixed-rational-integers/79546/7 "2022-04-16T12:58:32Z")

</div>

From the plot example, it looks like the goal is to build xticks with rational multiples of π.

There were probably better solutions posted here, but fwiw here is another take on it:

```julia
using Plots
x = rationalize.(-2:0.5:3)
r(x) = map(u -> (y=floor(Int,u); u == y ? y : u), x)
l(x) = typeof(x) <: Rational ? "$(x.num)/$(x.den)π" : x==0 ? "0" :
       x==1 ? "π" : x==-1 ? "-π" : "$(x)π"
plot(sin, xlims= π.*extrema(x))
xticks!(x*π, (l∘r).(x))

```

 ![Plots_gr_xticks_rational_multiples_of_pi](https://global.discourse-cdn.com/julialang/original/3X/4/0/4065f04f192cbafe1bb13935d99c112e206e1aca.png)

---

<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, 1:29pm UTC](https://discourse.julialang.org/t/vector-conversion-mixed-rational-integers/79546/8 "2022-04-16T13:29:22Z")

</div>

> [@rafael.guerra](#):
>
> to build xticks with rational multiples of π.

to build xticks with rational multiples of π

---

<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, 2:12pm UTC](https://discourse.julialang.org/t/vector-conversion-mixed-rational-integers/79546/9 "2022-04-16T14:12:14Z")

</div>

Would not be better to immediately convert from `Rational` to `String`? You are creating a mixed-types array for no reason in the middle of the way.

---

<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, 2:25pm UTC](https://discourse.julialang.org/t/vector-conversion-mixed-rational-integers/79546/10 "2022-04-16T14:25:36Z")

</div>

> [@Henrique\_Becker](#):
>
> Would not be better to immediately convert from `Rational` to `String` ? You are creating a mixed-types array for no reason in the middle of the way.

The reason was to improve the aesthetics of the plot, not performance. Stringing rationals produces double slashes and we should also handle rational integers like 4//2 etc. Any hints?

---

<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, 2:57pm UTC](https://discourse.julialang.org/t/vector-conversion-mixed-rational-integers/79546/11 "2022-04-16T14:57:44Z")

</div>

I was not referring to your solution, mostly because it did not favor legibility so I did not take the time to try to decipher it (looking closely, it also plays with the types without any necessity). I was referring to the post chosen as solution and rocco’s follow-up. My suggestion was to have a single function that took a Rational and returned an String already in the right style, without an conversion to `Int` step, and just broadcast such function. Like:

```julia
julia> x = rationalize.(-2:0.5:3);

julia> function rat2str(r)
           num, den = numerator(r), denominator(r)
           iszero(num) && return "0"
           if isone(den)
               num == -1 && return "-π"
               num == 0 && return "0"
               num == 1 && return "π"
               return "$(num)π"
           end 
           return "$(num)/$(den)π"
       end
rat2str (generic function with 1 method)

julia> rat2str.(x)
11-element Array{String,1}:
 "-2π"
 "-3/2π"
 "-π"
 "-1/2π"
 "0"
 "1/2π"
 "π"
 "3/2π"
 "2π"
 "5/2π"
 "3π"

```

---

<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, 3:01pm UTC](https://discourse.julialang.org/t/vector-conversion-mixed-rational-integers/79546/12 "2022-04-16T15:01:43Z")

</div>

The problem are these fellows:

```julia
 "-1π"
 "0π"
 "1π"

```

But one just need to add some extra conditions.  
Thanks.

> **Edited code from Henrique Becker**
>
> ```julia
> function rat2str(r)
> num, den = numerator(r), denominator(r)
> if iszero(num)
> "0"
> elseif isone(den)
> isone(num) ? "π" : isone(-num) ? "-π" : "$(num)π" 
> else
> "$(num)/$(den)π"
> end
> end
> 
> x = rationalize.(-2:0.5:3)
> rat2str.(x)
> 
> "-2π"
> "-3/2π"
> "-π"
> "-1/2π"
> "0"
> "1/2π"
> "π"
> "3/2π"
> "2π"
> "5/2π"
> "3π"
> 
> ```

---

<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, 3:04pm UTC](https://discourse.julialang.org/t/vector-conversion-mixed-rational-integers/79546/13 "2022-04-16T15:04:13Z")

</div>

Maybe there are finicky corner cases, but this is definitely a _printing_ question, and should not take a detour through mixed-type arrays.

Seems like a classic XY-problem.

---

<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, 3:18pm UTC](https://discourse.julialang.org/t/vector-conversion-mixed-rational-integers/79546/14 "2022-04-16T15:18:08Z")

</div>

ha, in the end both of us edited the answers to fit the cases. The OP can now choose the answer in their preferred style.

---

<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, 3:19pm UTC](https://discourse.julialang.org/t/vector-conversion-mixed-rational-integers/79546/15 "2022-04-16T15:19:36Z")

</div>

Hi Henrique, your code is much more elegant, and I learned from it. Cheers.

---

<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, 3:33pm UTC](https://discourse.julialang.org/t/vector-conversion-mixed-rational-integers/79546/16 "2022-04-16T15:33:50Z")

</div>

If you want to learn more about this style, this is basically a extreme application of the [Bouncer Pattern](https://mauriziopireddu.com/clean-code/the-bouncer-pattern). Most articles define it as “make all validations first and then deal with the happy path”, but I use it more generally as “if it is easy to check for and deal with a corner case, do this in the start of the code and in the rest of the code you can assume no corner case”. In this particular code, basically all the code are corner cases of the more general `"$(num)/$(den)π"` case.

---

<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:05pm UTC](https://discourse.julialang.org/t/vector-conversion-mixed-rational-integers/79546/17 "2022-04-16T16:05:08Z")

</div>

The origin of the question stems from the fact that I was unable to populate a vector with the values [0,1 // 2,1,3 // 2,2] by hand because julia automatically transformed them all into rationals.  
I just wanted to see how the plot with the “better formatted” ticks looked like.  
But then my problem became to understand how to make rational and whole coexist in the same container.  
I understood (only AFTER) that for this the type of the container must be Real which is a super-type of rationals and integers (!?).  
I know very little about the julia type system and therefore I feel a bit groping.  
I have seen the solution you propose and I also believe, as you claim, that it is as natural as possible (as well as very beautiful / elegant) in order to adorn the x axis.  
But, as mentioned, in addition to the aesthetic aspect, also figuring out how to make the integers coexist with the fractionals in the same house, which I was not able to do.

Certainly now I am spoiled for choice for the many solutions proposed.  
But above all I still learned a little something.

---

<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, 4:17pm UTC](https://discourse.julialang.org/t/vector-conversion-mixed-rational-integers/79546/18 "2022-04-16T16:17:40Z")

</div>

> [@rocco\_sprmnt21](#):
>
> The origin of the question stems from the fact that I was unable to populate a vector with the values [0,1 // 2,1,3 // 2,2] by hand because julia automatically transformed them all into rationals.

Oh, yes, this is a little disconcerting. You can just do `Real[0, 1 // 2, 1, 3 // 2, 2]` and have the result you wanted, but there’s nothing in the language that suggests that Julia will try to do an automatic conversion in this situation.

> [@rocco\_sprmnt21](#):
>
> must be Real which is a super-type of rationals and integers (!?).

I suppose you find strange that `Real` is a supertype of integers, but this exactly how math goes (i.e., ℤ ⊂ ℝ, and ℚ ⊂ ℝ). In fact, in Julia:

```julia
julia> supertypes(Int)
(Int64, Signed, Integer, Real, Number, Any)

julia> supertypes(Rational)
(Rational, Real, Number, Any)

```

What I find a little strange that `Int` is not a subtype of `Rational`.

Finally, if you start with an empty Vector, the recommended path would be to define the vector like `Real[]` or `Vector{Real}()` so at least it is not an `Any` vector.

---

<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:21pm UTC](https://discourse.julialang.org/t/vector-conversion-mixed-rational-integers/79546/19 "2022-04-16T16:21:50Z")

</div>

> [@DNF](#):
>
> Seems like a classic XY-problem.

in fact it is a problem on the X axis (on the Y axis there are only float(real) numbers).  
Seriously, you are right to say that I tried to solve a problem (X) in the wrong way and so I self-generated a new problem (Y), which became the problem I posed here, perhaps in an unclear way because I don’t know the structure of the types of julia.  
Then I replied that the question arose from trying to solve X and I had the appropriate solutions for problem X.  
In the end I got more solutions than I was looking for 😁

---

<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:27pm UTC](https://discourse.julialang.org/t/vector-conversion-mixed-rational-integers/79546/20 "2022-04-16T16:27:34Z")

</div>

> [@Henrique\_Becker](#):
>
> but there’s nothing in the language that suggests that Julia will try to do an automatic conversion in this situation.

**NB:** this behavior is covered [in the manual:](https://docs.julialang.org/en/v1/manual/conversion-and-promotion/#Promotion)_Integers mixed with rationals are promoted to rationals_

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