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

#1

Why are these returning a number?

$ 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

0 Likes

#2

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

1 Like

#3

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

0 Likes

#4

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: PSA: make it easier to help you

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:

3 Likes

#5

A Bool already is a Number, and true and false indeed represent the numbers 1 and 0 in 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.

5 Likes

#6

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

## 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> 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

0 Likes

#7

To convert the argument to Float64.

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

0 Likes

#8

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.

0 Likes

#9

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.

1 Like

#10

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

0 Likes