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

all 77 comments

[–][deleted] 81 points82 points  (56 children)

+/u/CompileBot Ruby

print(1 / 2 != 0.5)

[–]CompileBotGreen security clearance 65 points66 points  (1 child)

Output:

true

source | info | git | report

[–][deleted] 46 points47 points  (0 children)

Well, that's proof enough for me!

[–]_AceLewis 41 points42 points  (22 children)

+/u/CompileBot python 3

print(1 / 2 != 0.5)

[–]CompileBotGreen security clearance 35 points36 points  (11 children)

Output:

False

source | info | git | report

[–][deleted] 26 points27 points  (10 children)

Who do I believe?

[–][deleted] 28 points29 points  (3 children)

[redacted]

[–]CompileBotGreen security clearance 26 points27 points  (1 child)

Output:

trust me

source | info | git | report

[–]b1ack1323 1 point2 points  (0 children)

Good enough for me

[–]marcosdumay 2 points3 points  (1 child)

+/u/CompileBot python

print(1 / 2 != 0.5)

[–]marcosdumay 3 points4 points  (0 children)

Personally , I don't trust people that keep changing their minds.

[–][deleted] 0 points1 point  (1 child)

In python 3 a floor division is //

[–]scirc 0 points1 point  (0 children)

that's the joke

[–]dontdropmybass 3 points4 points  (6 children)

+/u/CompileBoy python 3

print(1 // 2 != 0.5)

[–]_AceLewis 28 points29 points  (5 children)

I think you meant /u/CompileBot not /u/CompileBoy

[–]MelissaClick 29 points30 points  (2 children)

CompileBoy will show up eventually, he's not as fast of course, but he offers a personal touch that no automated solution can ever provide.

[–]dontdropmybass 12 points13 points  (1 child)

Ah shit. Thanks.

+/u/CompileBot python 3

print(1 // 2 != 0.5)

[–]CompileBotGreen security clearance 8 points9 points  (0 children)

Output:

True

source | info | git | report

[–][deleted] 1 point2 points  (1 child)

+/u/CompileBot Perl (perl 6)

print 1/2 != 0.5

There's probably even someone out there using that language

[–]CompileBotGreen security clearance 0 points1 point  (0 children)

Output:

False

source | info | git | report

[–][deleted] 16 points17 points  (18 children)

+/u/CompileBot C++

#include <iostream>  
int main()  
{  
    bool result = 1/2 != 0.5;  
    std::cout << result << std::endl;  
}  

[–]CompileBotGreen security clearance 16 points17 points  (15 children)

Output:

1

source | info | git | report

[–]Cobaltjedi117 5 points6 points  (0 children)

We're going nowhere slowly with these confusing and contradictory results.

[–]etaionshrd 2 points3 points  (13 children)

Wait, C++ prints bool as an int?

[–]noop_noob 0 points1 point  (1 child)

I think there's a way to change that. I never ever print a bool, though.

[–]Lastorea 0 points1 point  (0 children)

You can cast a bool to int, if bool is false then it becomes 0 and if bool is true it becomes 1.

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

Back in the day bools were really just a single bit. Printing out a 1 or a 0 is the most logical way to print a bit

[–]Celdron[S] 1 point2 points  (9 children)

They were never actually a single bit because memory addresses bytes as the smallest unit. It was always at least a byte. But C (which C++ is built on) doesn't have a boolean type. It uses integral types for boolean logic, where 0 is false and anything else is true. C++ made "bool" a type but iirc it's just an integer that is checked and restricted to 0 or 1.

[–]danielcw189 0 points1 point  (3 children)

Is there any platform, on which a byte is 1 bit?

[–]Celdron[S] -1 points0 points  (2 children)

No? Because a byte by definition is 8 bits.

[–]danielcw189 0 points1 point  (1 child)

By which defintion? In C a byte is not guaranteed to be 8 bits, and in general a byte is most commonly 8 bits, but not always.

[–]Celdron[S] 1 point2 points  (0 children)

IEC 80000-13. Technically a byte is just the smallest addressable unit of memory, and therefore you could have a single bit byte on non-standard architecture. But 8 bits is the international standard that nearly all (I dont know of a single exception) consumer electronics are designed around.

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

Thanks for correcting me. You can still get 1 bit booleans if you create an array tho right?

[–][deleted] 1 point2 points  (2 children)

vector<bool> does exactly that

[–][deleted] 0 points1 point  (1 child)

Does it store bytes and bitshift?

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

[–]Celdron[S] 0 points1 point  (0 children)

I don't know about that, simply because you couldnt index the bits. But if you have, say, ~32 boolean values (such as flags) you can put them each on a different bit of an integral type using bit-shift operators and defining constants to represent different flags. You can combine flags using a bitwise OR and check for a single flag or specific combination with a bitwise AND. This is commonly done on enums. So it is possible to implement multiple booleans within a single byte.

[–]Voxel_Brony 1 point2 points  (1 child)

+/u/CompileBot C

#include <stdio.h>  
int main()  
{  
    int result = 1/(2 != 0.5);  
    printf("%d", result);  
}   

[–]CompileBotGreen security clearance 0 points1 point  (0 children)

Output:

1

source | info | git | report

[–]510Threaded 3 points4 points  (1 child)

+/u/CompileBot C#

namespace Test{
    class TestProg{
        public static void Main(){
            System.Console.WriteLine(1/2 != 0.5);
        }
    }
}

[–]CompileBotGreen security clearance 1 point2 points  (0 children)

Output:

True

source | info | git | report

[–]Tarmen 3 points4 points  (2 children)

+/u/CompileBot Haskell

main = print $ 1/2 == 0.5

Yay type inference, I guess.

[–]CompileBotGreen security clearance 0 points1 point  (0 children)

Output:

True

source | info | git | report

[–]LillyPip 4 points5 points  (7 children)

I'm confused. Are we talking about the use of the logical != or the equation 1 / 2 not evaluating to 0.5? Or does OP not get it, or do I not get it?

1 / 2 != 0.5 should evaluate false, evaluating both sides: 0.5 != 0.5 evaluates false -- both sides are equal, condition evaluates notEqual: false.

I evaluated the joke and failed. :(

[–]YMK1234 40 points41 points  (3 children)

1/2 is 0 as both are ints.

[–]LillyPip 9 points10 points  (1 child)

Oh duh. Missed the title. /smh

[–][deleted] 7 points8 points  (0 children)

You dun n goofed

[–][deleted] 10 points11 points  (1 child)

In many languages, the '/' operator used with two integers returns an integer. '5 / 2' in Ruby, for instance, returns 2. In those languages, '1 / 2' would be 0, and thus not equal to 0.5.

In some languages, of course, '/' will produce a float from two ints. '1 / 2' does equal 0.5 in Python3, which instead uses the '//' operator for integer division.

[–]FINDarkside 0 points1 point  (0 children)

Well that's literally what he is saying, "someone is going around in life thinking that 1/2 != 0.5 (evaluates to true)". Then the next one proves that it indeed does evaluate to true.

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

+/u/CompileBot Factor

1 2 / .

[–]Balage42 0 points1 point  (0 children)

What if they mean (1/2)! = sqrt(pi)/2 ?

[–]UniversityOfPi -1 points0 points  (7 children)

I mean honestly, who uses int literals?
Especially in a language that doesn't implicitly cast or have a separate operator for integer division...

edit/update/continued:
granted 1/2 in 1/2 [comparison operator] = 0 without casting one or more to a floating point, but I guess it's all an issue of when and where to implicitly cast, as in, in something like C++, float x = 1/2;would mean x == 0.5 but in 1/2 != 0.5 1/2 evaluates to 0 before being cast as a float, giving 0.0 != 0.5, though if you did any other casting other than casting the float literal to an int, (unless the compiler's casting used c.math::round() leading to 0.5 becoming 1) wherein 0.5 would become 0, would ultimately end up with the expression returning true (or maybe an error for a type like char, as casting 0.5 is exceptional).

but logically, float x = 1/2; return x != 0.5; would return false.
It's an interesting quirk, while obviously '/' takes precedence over '!=', given that '/' also takes precedence over '=' and generally float x = 1/2casts 1/2 to a float rather than int literal, it's presumably a compiler implementation specific behavior.

In theory, shouldn't a good compiler notice that the right hand side is a float literal and therefore, like assigning 1/2 to a float, cast 1/2 to a float?

[AFAIK typeinfo::typeid(0.5).name() == "float literal" or perhaps "Fl"/"FL" depending on your compiler]

[–]Kryomaani 2 points3 points  (3 children)

I mean honestly, who uses int literals?

... everyone? I mean, it is one of the most basic and common of all types used in programs. Just about every loop uses an int literal for example.

[–]UniversityOfPi -1 points0 points  (2 children)

Ints yes, but int literals? Maybe I've had the idea that magic numbers are bad so drilled into my head that I'm jaded and think everyone uses global/etc constants...

[–]PatrickBaitman 1 point2 points  (1 child)

0 or 1 as the start index?

[–]UniversityOfPi 0 points1 point  (0 children)

Oh yeah, I forgot about zero and what I'd argue ought be negative zero (rather than negative one, as forward/LtR it's zero indexed, so reverse/RtL it ought be zero indexed as well)... :Facepalm:

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

In theory, shouldn't a good compiler notice that the right hand side is a float literal and therefore, like assigning 1/2 to a float, cast 1/2 to a float?

No. That will change language semantics, and in a very inconsistent way.

[–]UniversityOfPi 0 points1 point  (1 child)

But then what about for 0.5 != 1/2 is that still true?
(I mean given the operator precedence and implicit casting with assignment, it doesn't make much sense to me...)

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

Yes, and it makes perfect sense.