# Why is a type when used as a function given a boolean true return a number?

**URL:** <https://discourse.julialang.org/t/why-is-a-type-when-used-as-a-function-given-a-boolean-true-return-a-number/21333>\
**Category:** New to Julia\
**Created:** [March 1, 2019, 7:54am UTC](https://discourse.julialang.org/t/why-is-a-type-when-used-as-a-function-given-a-boolean-true-return-a-number/21333 "2019-03-01T07:54:00Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![StevenSiew](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevensiew/32/218393_2.png) [@StevenSiew](https://discourse.julialang.org/u/StevenSiew)\
**Post date:** [March 1, 2019, 7:54am UTC](https://discourse.julialang.org/t/why-is-a-type-when-used-as-a-function-given-a-boolean-true-return-a-number/21333/1 "2019-03-01T07:54:01Z")

</div>

Why are these returning a number?

```julia
$ julia
               _
   _ _ _(_)_ | Documentation: https://docs.julialang.org
  (_) | (_) (_) |
   _ _ _| |_ __ _ | Type "?" for help, "]?" for Pkg help.
  | | | | | | |/ _ | |
  | | |_| | | | (_| | | Version 1.0.3 (2018-12-18)
 _/ |\ ___|_|_|\__ _| | Official https://julialang.org/ release
|__/ |

julia> Float64(true)
1.0

julia> Float32(true)
1.0f0

julia> Int64(true)
1

julia> Int32(true)
1

julia> Float64(false)
0.0

julia> Float32(false)
0.0f0

julia> Int64(false)
0

julia> Int32(false)
0

```

---

<div class="post-metadata">

**Author:** ![carstenbauer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/carstenbauer/32/4981_2.png) [@carstenbauer](https://discourse.julialang.org/u/carstenbauer)\
**Post date:** [March 1, 2019, 7:56am UTC](https://discourse.julialang.org/t/why-is-a-type-when-used-as-a-function-given-a-boolean-true-return-a-number/21333/2 "2019-03-01T07:56:07Z")

</div>

Maybe I misunderstand but it’s because you are explicitly converting to numbers by calling the constructors? What did you expect?

---

<div class="post-metadata">

**Author:** ![StevenSiew](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevensiew/32/218393_2.png) [@StevenSiew](https://discourse.julialang.org/u/StevenSiew)\
**Post date:** [March 1, 2019, 8:20am UTC](https://discourse.julialang.org/t/why-is-a-type-when-used-as-a-function-given-a-boolean-true-return-a-number/21333/3 "2019-03-01T08:20:52Z")

</div>

Please refer to this  
[https://discourse.julialang.org/t/how-to-get-a-square-matrix-of-mynum-struct-to-inverse/21247](https://discourse.julialang.org/t/how-to-get-a-square-matrix-of-mynum-struct-to-inverse/21247)

---

<div class="post-metadata">

**Author:** ![kevbonham](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kevbonham/32/216165_2.png) [@kevbonham](https://discourse.julialang.org/u/kevbonham)\
**Post date:** [March 1, 2019, 10:10am UTC](https://discourse.julialang.org/t/why-is-a-type-when-used-as-a-function-given-a-boolean-true-return-a-number/21333/4 "2019-03-01T10:10:44Z")

</div>

It’s generally not good form to refer people to other unresolved questions in order to answer your question. Don’t make people do work in order to understand what you’re asking. See also: [Please read: make it easier to help you](https://discourse.julialang.org/t/psa-make-it-easier-to-help-you/14757)

It’s pretty common in programming languages for booleans true and false to be represented by 1 and 0 respectively. In python, they are literally the same (`1 == True`). ~~In Julia, they’re not the same thing~~ , but you can convert a Bool into a number as you’ve shown. To reiterate:

> [@carstenbauer](#):
>
> What did you expect?

---

<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:** [March 1, 2019, 12:22pm UTC](https://discourse.julialang.org/t/why-is-a-type-when-used-as-a-function-given-a-boolean-true-return-a-number/21333/5 "2019-03-01T12:22:00Z")

</div>

> [@kevbonham](#):
>
> In Julia, they’re not the same thing, but you can convert a Bool into a number as you’ve shown.

A `Bool` already is a `Number`, and `true` and `false` indeed represent the numbers 1 and 0 in Julia:

```julia
julia> Bool <: Number
true

julia> true == 1
true

julia> false == 0
true

```

However, if you are in a context expecting a boolean type, e.g. an `if` statement, you must use a `Bool` and not some other number type for 0 and 1.

---

<div class="post-metadata">

**Author:** ![StevenSiew](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevensiew/32/218393_2.png) [@StevenSiew](https://discourse.julialang.org/u/StevenSiew)\
**Post date:** [March 1, 2019, 2:30pm UTC](https://discourse.julialang.org/t/why-is-a-type-when-used-as-a-function-given-a-boolean-true-return-a-number/21333/6 "2019-03-01T14:30:45Z")

</div>

Prehaps I should explain myself.

I could not get a struct called mynum to have it’s inverse calculated properly as a square matrix

When I traced how a square matrix of type Float64 is transformed into its inverse

there is a strange call Float64(true)

at LinearAlgebra.jl/blob/master/src/uniformscaling.jl

```julia
## Matrix construction from UniformScaling
function Matrix{T}(s::UniformScaling, dims::Dims{2}) where {T}
    A = zeros(T, dims)
    v = T(s.λ)
    for i in diagind(dims...)
        @inbounds A[i] = v
    end
    return A
end

```

Where " v = T(s.λ) " becomes v = Float64(true)

```julia
julia> Float64(I.λ)
1.0

julia> I.λ
true

```

and subsequently I need to create a mynum(x::Bool) before my struct mynum can have its inverse calculated.

So my question is what is the purpose of Float64(x::Bool)

Steven Siew

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [March 1, 2019, 2:44pm UTC](https://discourse.julialang.org/t/why-is-a-type-when-used-as-a-function-given-a-boolean-true-return-a-number/21333/7 "2019-03-01T14:44:43Z")

</div>

> [@StevenSiew](#):
>
> what is the purpose of Float64(x::Bool)

To convert the argument to `Float64`.

Please do look at the documentation, eg `?Float64` in the REPL.

---

<div class="post-metadata">

**Author:** ![GunnarFarneback](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gunnarfarneback/32/1827_2.png) [@GunnarFarneback](https://discourse.julialang.org/u/GunnarFarneback)\
**Post date:** [March 1, 2019, 4:24pm UTC](https://discourse.julialang.org/t/why-is-a-type-when-used-as-a-function-given-a-boolean-true-return-a-number/21333/8 "2019-03-01T16:24:44Z")

</div>

> [@StevenSiew](#):
>
> So my question is what is the purpose of Float64(x::Bool)

As others have explained it’s an established (and quite useful) convention in programming languages, but also in mathematics, to associate `true` and `false` with the values `1` and `0`. I believe what you rather want to ask is why the implementation of `inv` chooses to make use of this convention.

---

<div class="post-metadata">

**Author:** ![foobar\_lv2](https://avatars.discourse-cdn.com/v4/letter/f/ee59a6/32.png) [@foobar\_lv2](https://discourse.julialang.org/u/foobar_lv2)\
**Post date:** [March 1, 2019, 4:40pm UTC](https://discourse.julialang.org/t/why-is-a-type-when-used-as-a-function-given-a-boolean-true-return-a-number/21333/9 "2019-03-01T16:40:42Z")

</div>

Afaik, the general convention for number types is that `T(true) == one(T)` and `T(false) == zero(T)`, i.e.~you should get the neutral elements for addition and multiplication in your field / ring / approximate field, and `true * x == x`, and `false * x == zero(typeof(x))`.

Note the hard zero: E.g. `0*Inf` is `NaN`, but `false*Inf` is `0.0`.

---

<div class="post-metadata">

**Author:** ![kevbonham](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kevbonham/32/216165_2.png) [@kevbonham](https://discourse.julialang.org/u/kevbonham)\
**Post date:** [March 1, 2019, 5:54pm UTC](https://discourse.julialang.org/t/why-is-a-type-when-used-as-a-function-given-a-boolean-true-return-a-number/21333/10 "2019-03-01T17:54:50Z")

</div>

> [@stevengj](#):
>
> However, if you are in a context expecting a boolean type, e.g. an `if` statement, you must use a `Bool` and not some other number type for 0 and 1.

I guess that shows me for running my mouth without checking 😄. I knew there was a way they were different from python, and this is definitely what I was thinking. Thanks for the clarification!
