# \`x==y\` is true but \`π^x == π^y\` is false

**URL:** <https://discourse.julialang.org/t/x-y-is-true-but-x-y-is-false/59976>\
**Category:** Numerics\
**Created:** [April 25, 2021, 8:40am UTC](https://discourse.julialang.org/t/x-y-is-true-but-x-y-is-false/59976 "2021-04-25T08:40:33Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![HJW019](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/hjw019/32/22102_2.png) [@HJW019](https://discourse.julialang.org/u/HJW019)\
**Post date:** [April 25, 2021, 8:40am UTC](https://discourse.julialang.org/t/x-y-is-true-but-x-y-is-false/59976/1 "2021-04-25T08:40:33Z")

</div>

A simple question for you CS experts:

```julia
function foo()
    x::Float32 = 0.5
    y::Float64 = 0.5
    return x==y, π^x == π^y
end

julia> foo()
(true, false)

```

Why `x==y` is true given the different numeric types? And if `x==y` is true, why `π^x == π^y` is false? The example shows the importance of numerical details, and I want to better understand the issue.

Thanks in advance.

---

<div class="post-metadata">

**Author:** ![Vasily\_Pisarev](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/vasily_pisarev/32/7929_2.png) [@Vasily\_Pisarev](https://discourse.julialang.org/u/Vasily_Pisarev)\
**Post date:** [April 25, 2021, 9:22am UTC](https://discourse.julialang.org/t/x-y-is-true-but-x-y-is-false/59976/2 "2021-04-25T09:22:38Z")

</div>

\pi is of special `Irrational` type in Julia, so that it’s converted to different floating-point types for `π^x` and `π^y`. Since those `Float32(π)` and `Float64(π)` are different, the exponentiation gives different results.

You may check `Float64(π)^x` vs `Float64(π)^y`, those must give the same result.

---

<div class="post-metadata">

**Author:** ![BLI](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bli/32/37206_2.png) [@BLI](https://discourse.julialang.org/u/BLI)\
**Post date:** [April 25, 2021, 9:26am UTC](https://discourse.julialang.org/t/x-y-is-true-but-x-y-is-false/59976/3 "2021-04-25T09:26:51Z")

</div>

Also, because `0.5` can be represented exactly in binary numbers, `x` and `y` are identical.

If you instead do:

```julia
x = Float64(1/3)
y = Float32(1/3)

```

you will find that `x == y` leads to `false`, since `1/3` can _not_ be represented exactly in binary numbers.

---

<div class="post-metadata">

**Author:** ![HJW019](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/hjw019/32/22102_2.png) [@HJW019](https://discourse.julialang.org/u/HJW019)\
**Post date:** [April 25, 2021, 10:29am UTC](https://discourse.julialang.org/t/x-y-is-true-but-x-y-is-false/59976/4 "2021-04-25T10:29:29Z")

</div>

Thanks to @Vasily_Pisarev and @BLI for the quick responses and informative answers!

---

<div class="post-metadata">

**Author:** ![HJW019](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/hjw019/32/22102_2.png) [@HJW019](https://discourse.julialang.org/u/HJW019)\
**Post date:** [April 25, 2021, 2:17pm UTC](https://discourse.julialang.org/t/x-y-is-true-but-x-y-is-false/59976/5 "2021-04-25T14:17:55Z")

</div>

I wish to follow up with another example which confused me again.

```julia
function bar()
    x::Float32 = 1/3
    y::Float64 = 1/3
    a::Float32 = 1/2
    b::Float64 = 1/2
    return x^a == x^b, y^a == y^b
end
julia> bar()
(false, true)

```

In this example I use 1/3 as the base instead of \pi, thinking that it does not getting the special treatment of Julia mentioned by @Vasily_Pisarev . Per @BLI 's post I understand that `a` and `b` are the same. Then I am confused why `x^a == x^b` is false?  
Why when the base becomes Float64, the evaluation `y^a == y^b` becomes true?

Waiting to be enlightened again.

---

<div class="post-metadata">

**Author:** ![tbeason](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tbeason/32/15898_2.png) [@tbeason](https://discourse.julialang.org/u/tbeason)\
**Post date:** [April 25, 2021, 2:26pm UTC](https://discourse.julialang.org/t/x-y-is-true-but-x-y-is-false/59976/6 "2021-04-25T14:26:13Z")

</div>

They are not the same. Just look at the output

```julia
julia> (Float32(1/2)^Float32(0.5),Float32(1/2)^Float64(0.5))
(0.70710677f0, 0.7071067811865476)

```

The second is being promoted to `Float64` because it is the same as `0.5^0.5`

---

<div class="post-metadata">

**Author:** ![giordano](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/giordano/32/2166_2.png) [@giordano](https://discourse.julialang.org/u/giordano)\
**Post date:** [April 25, 2021, 2:33pm UTC](https://discourse.julialang.org/t/x-y-is-true-but-x-y-is-false/59976/7 "2021-04-25T14:33:35Z")

</div>

Generally speaking, when you deal with floating point numbers it’s more likely you want to do approximate comparison, for example with [`isapprox`](https://docs.julialang.org/en/v1/base/math/#Base.isapprox), than exact comparison with `==`, precisely because most real numbers don’t have exact representation as floating point numbers, and when doing operations with these numbers you can accumulate multiple rounding errors.

---

<div class="post-metadata">

**Author:** ![Vasily\_Pisarev](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/vasily_pisarev/32/7929_2.png) [@Vasily\_Pisarev](https://discourse.julialang.org/u/Vasily_Pisarev)\
**Post date:** [April 25, 2021, 5:33pm UTC](https://discourse.julialang.org/t/x-y-is-true-but-x-y-is-false/59976/8 "2021-04-25T17:33:03Z")

</div>

Arguments in `x^y` are promoted to a common type. In case of `x::Float32` and `y::Float64`, that’s `Float64`. So, `Float32(1/3)^Float32(0.5)` is computed in single precision, and `Float32(1/3)^Float64(0.5)` gets converted to `Float64(Float32(1/3))^Float64(0.5)` and computed in double precision.

---

<div class="post-metadata">

**Author:** ![HJW019](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/hjw019/32/22102_2.png) [@HJW019](https://discourse.julialang.org/u/HJW019)\
**Post date:** [April 25, 2021, 5:58pm UTC](https://discourse.julialang.org/t/x-y-is-true-but-x-y-is-false/59976/9 "2021-04-25T17:58:25Z")

</div>

Thanks again to everyone for the quick and informative response. Highly appreciated!
