# Rational ^ Rational

**URL:** https://discourse.julialang.org/t/rational-rational/91063
**Category:** General Usage
**Created:** [November 30, 2022, 8:06pm UTC](https://discourse.julialang.org/t/rational-rational/91063 "2022-11-30T20:06:52Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![jar1](https://avatars.discourse-cdn.com/v4/letter/j/c0e974/32.png) [@jar1](https://discourse.julialang.org/u/jar1)
#### Post date: [November 30, 2022, 8:06pm UTC](https://discourse.julialang.org/t/rational-rational/91063/1 "2022-11-30T20:06:52Z")

</div>

Why does this make a Float64 instead of a Rational?

```julia
julia> ((2//1)^(-4//1))
0.0625

```

Is this the only way to get a Rational out of exponentiation?

```julia
julia> Rational((2//1)^(-4//1))
1//16

```

---

<div class="post-metadata">

### Author: ![BioTurboNick](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bioturbonick/32/6380_2.png) [@BioTurboNick](https://discourse.julialang.org/u/BioTurboNick)
#### Post date: [November 30, 2022, 8:14pm UTC](https://discourse.julialang.org/t/rational-rational/91063/2 "2022-11-30T20:14:21Z")

</div>

This is the method being called:

```julia
^(x::Number, y::Rational) = x^(y.num/y.den)

```

I suppose the thinking is that you probably don’t want this:

```julia
a = 2 // 1
b = -3 // 8

julia> Rational(a ^ b)
6945500098633947//9007199254740992

```

---

<div class="post-metadata">

### Author: ![aplavin](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/aplavin/32/222056_2.png) [@aplavin](https://discourse.julialang.org/u/aplavin)
#### Post date: [November 30, 2022, 8:56pm UTC](https://discourse.julialang.org/t/rational-rational/91063/3 "2022-11-30T20:56:40Z")

</div>

Rational to the power of rational is typically irrational:

```julia
julia> (2//1)^(1//2)
1.4142135623730951

```

---

<div class="post-metadata">

### Author: ![sgaure](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sgaure/32/14779_2.png) [@sgaure](https://discourse.julialang.org/u/sgaure)
#### Post date: [November 30, 2022, 9:16pm UTC](https://discourse.julialang.org/t/rational-rational/91063/4 "2022-11-30T21:16:54Z")

</div>

> [@jar1](#):
>
> Why does this make a Float64 instead of a Rational?
> 
> ```julia
> julia> ((2//1)^(-4//1))
> 0.0625
> 
> ```

An important principle in Julia to ensure performance, is type stability. I.e. the type of a function’s return value should be a function of the input types. The return type should not vary with the input values, only with their types. Some powers with rational numbers will yield a rational result, but most often it does not. To ensure type stability the `Rational{Int64} ^ Rational{Int64}` operation returns a `Float64`. It’s a design choice (it could have been `BigFloat` or `Float32` or whatever, but it is `Float64`).

---

<div class="post-metadata">

### Author: ![mbauman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mbauman/32/31082_2.png) [@mbauman](https://discourse.julialang.org/u/mbauman)
#### Post date: [November 30, 2022, 9:28pm UTC](https://discourse.julialang.org/t/rational-rational/91063/5 "2022-11-30T21:28:58Z")

</div>

And note that `Rational ^ Integer` is a `Rational`.

```julia
julia> ((2//1)^(-4))
1//16

```

---

<div class="post-metadata">

### Author: ![Palli](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/palli/32/3380_2.png) [@Palli](https://discourse.julialang.org/u/Palli)
#### Post date: [November 30, 2022, 9:57pm UTC](https://discourse.julialang.org/t/rational-rational/91063/6 "2022-11-30T21:57:48Z")

</div>

Depending on types you get Float46 (reasonable) or BigFloat (also reasonable), and you can do (but it’s not reasonable):

```julia
julia> Rational(big(1//2)^big(1//2)) # scroll to right to see this is still a "Rational", type that is
81877371507464127617551201542979628307507432471243237061821853600756754782485//115792089237316195423570985008687907853269984665640564039457584007913129639936

```

`Rational` there implies you get a rational out, but that’s wrong, as the calculated value an irrational number (so I would avoid typing that in code), you only get the most accurate (considering default precision of BigFloat) rational of the resulting BigFloat number, which was already an approximation.

Since you know the result in in general, for a^b, irrational, i.e. none of the approximations are accurate, then this is more reasonable, if you only want some rational approximation:

```julia
julia> rationalize(big(1//2)^big(1//2))
4866752642924153522//6882627592338442563

```

Yes, the type you get is still a Rational, but look at the help string for rationalize, then it’s clear it’s an approximation. I would still just keep the Float64 (or even convert BigFloat to Float64 too).

Or maybe even:

```julia
julia> rationalize(Int16, big(1//2)^big(1//2)) # possibly rather Rational{Int64}(rationalize(Int16, big(1//2)^big(1//2)))
13860//19601

```
