How to convert numbers into boolean in Julia?
Asked Answered
U

1

6

I want to convert numbers into equivalent boolean. based on the official doc:

"false is numerically equal to 0 and true is numerically equal to 1."

So I would like to convert the numeric values to the equivalent boolean. Example of expected behavior:

[1] Bool(5)
> true

But, Julia gives me:

ERROR: InexactError: Bool(5)
Stacktrace:
 [1] Bool(x::Int64)
   @ Base .\float.jl:158
 [2] top-level scope
   @ REPL[26]:1

But if I say:

[1] Bool(0.0)

# result
> false

It looks good on 0 and 0.0! But it doesn't work on numbers like 5 or 2.1, etc.

Unpracticed answered 27/5, 2022 at 13:47 Comment(0)
M
7

The quick answer is that it seems like you want to test whether a value is non-zero or not and you can test if a number x is non-zero like this: x != 0. Whether a number is considered true or false depends on what language you're in, however; more on that below.

Having gotten the quick answer out of the way, we can talk about why. The docs say that false and true are numerically equal to zero and one. We can test this out ourselves:

julia> false == 0
true

julia> true == 1.0
true

You can see that the equality holds regardless of numerical type—integers or floats or even more esoteric number types like complexes or rationals. We can also convert booleans to other types of numbers:

julia> Int(false)
0

julia> Float64(true)
1.0

We can convert those values back to booleans:

julia> Bool(0)
false

julia> Bool(1.0)
true

What you can't do is convert a number that isn't equal to zero or one to boolean:

julia> Bool(5)
ERROR: InexactError: Bool(5)

That is because the Bool type can only represent the values zero and one exactly and trying to convert any other numeric value to Bool will give an InexactError. This is the same error you get if you try to convert a float that isn't integer-valued to an integer type:

julia> Int(5.0)
5

julia> Int(5.5)
ERROR: InexactError: Int64(5.5)

Or an integer to a smaller type that isn't big enough to represent that value:

julia> Int8(123)
123

julia> Int8(1234)
ERROR: InexactError: trunc(Int8, 1234)

The exact same thing is happening here: Bool is not big enough to represent the value 5, so you get an error if you try to convert the value 5 to Bool.

The convention that many languages use for truthiness of numbers is that values that represent zero are falsey and values that represent no-zeros are truthy. Note that there is no sound mathematical reason for this: zero is not false and non-zero numbers are not true; it's just a convention that comes from the C programming language, which doesn't have a boolean type and uses this convention for treating integers as true/false in conditionals. This convention is far from universal, however, as there are popular languages that don't follow it: in Lisps and Ruby, for example, all numbers are truthy. I wrote a post recently on the Julia discourse forum about differing notions of truthiness in different languages and why Julia rejects truthiness and instead requires you to write out an explicit condition like comparison with zero for numbers, or non-emptiness for collections.

Since the condition you actually want to check is whether a number is equal to zero or not, that's just what you should do: explicitly compare the number to zero by doing x != 0. This will evaluate to a boolean value: true if x is non-zero and false if x is equal to zero. There's a predicate function that does this as well: iszero(x) checks if x is zero, which can be handy if you want to e.g. count how many zero or non-zero values there are in a collection:

julia> v = rand(-2:2, 100);

julia> count(iszero, v)
18

julia> count(!iszero, v)
82
Muddleheaded answered 27/5, 2022 at 14:16 Comment(2)
Thank you for these great explanations! I got your point about the concept and my mistake! Thanks.Unpracticed
You're very welcome! Glad you found the explanation helpful.Muddleheaded

© 2022 - 2024 — McMap. All rights reserved.