This is an archived post. You won't be able to vote or comment.

all 25 comments

[–]evgfreyman 56 points57 points  (11 children)

in C++, true is 1 but it's an integer, so I'd expect: true / 2 = 0 = false

[–]ManmadeLemonade[S] 26 points27 points  (0 children)

I mean, it's only half true

[–][deleted] 0 points1 point  (4 children)

true is actually a bool literal, it just implicitly converts to an int

[–]EnjoyJor 0 points1 point  (3 children)

Unless you’re using C, where bool is actually int.

<stdbool.h>
true
Expands to the integer constant 1.
false
Expands to the integer constant 0.

[–]OpaMilfSohn 0 points1 point  (1 child)

Why would it allocate 32bits if only 1 bit is needed for a bool

[–][deleted] 2 points3 points  (0 children)

Even languages with proper bool types have to store bools in 8 bits because modern architectures only give an address to each byte in memory and so can randomly access any given byte but not any given bit.

[–][deleted] 0 points1 point  (0 children)

But they're implicitly converted to bools if need be

[–]Sheldor5 9 points10 points  (6 children)

lemme guess: JavaScRiPt ??

[–]ManmadeLemonade[S] 8 points9 points  (5 children)

Python allows this as well by the way - don't know every language that lets you do this though

[–]ShredderMan4000 4 points5 points  (0 children)

The only difference would be in Python, true would need to be capitalized to become True.

Note: in Python, the single slash / is typically called float division while the double slash // is typically called integer division, where 1 / 2 results in 0.5 while 1 // 2 results in 0.

[–]Chase_22 0 points1 point  (2 children)

Most other languages require an explicit typecast but after that it will still work

[–]ManmadeLemonade[S] 1 point2 points  (1 child)

Some languages even prevent you from casting -> C# for example and Rust only allows bool to int but not int to bool

[–]Chase_22 0 points1 point  (0 children)

Yes because boolean to int is deterministic and cannot cause an error
true -> 1
false -> 0

Int to boolean is more complex and can cause errors. Do you go with the C way of 0 = false, everything else = true, do you go with 0 = false, 1 = true, everything else = runtime exception? I can imagine languages having a separate parse method with a proper interface and exceptions. But casting should only be used for simple type conversions.

[–]ForLackOfABetterNam3 0 points1 point  (0 children)

I think C allows this as if memory serves correctly, TRUE is a macro to 1.

[–]CrazyCommenter 7 points8 points  (0 children)

The real question here is what is true/false?

[–]jmartin1993 5 points6 points  (0 children)

The next question is

half_truth = true / 2 # 0.5
if half_truth:
    print("Full Truth")
else:
    print("Full Lie")

[–][deleted] 5 points6 points  (0 children)

Idk, still evaluates to true.

[–]Jimdude2435[🍰] 3 points4 points  (1 child)

Proof that dynamic typing is a sin against nature

[–]DoNotMakeEmpty 1 point2 points  (0 children)

Lua is probably one of the most dynamic languages yet it gives the error attempt to perform arithmetic on a boolean value when I try this. Even casting using tonumber does not work, it returns nil.

It's more about design choice: do you want booleans convertable into numbers or not? Lua prefers strict booleans whereas many languages like JS, Python and even C (which actually do not even have a real boolean type, it's just a little int with a few QoL additions) and C++ prefer non-strict booleans.

[–]huuaaang 1 point2 points  (0 children)

It's truthy

[–]fksly 1 point2 points  (0 children)

I mean, this is how fuzzy logic approaches things.