25

What will be the output????

Comments
  • 4
    No I guess
  • 7
    [B] No
  • 4
    Trick question?
  • 8
    God damned floating point!
  • 13
    wtf is a YesNo
  • 3
    No. An equal value is not greater than.
  • 3
    @perfectdark I liberally interpret it as "I don't know".
  • 8
  • 3
    @Jonnyforgotten Mmh, yeah I was wondering if it was some sort of precision or a compiler trick question..

    But I didn't bother to look it up, so I assumed the logical assumption of those two values are exactly equal.
  • 13
    Assigning a as 0.7 will get it "optimized" to a double, which is a little less then 0.7 (0.69....), therefore the program will output 'yes'.
    To prevent this "optimizing", define the variable like so: float a = 0.7f. (Note the 'f')
    The same works for others (like double, add a 'd')
    (In case someone was wondering and does not want to read the link ;) )
  • 4
    It will output No due to the implicit typecast from double to float in the assignment and then from float to double in the comparison since 0.7 can't be accurately represented as a floating point number.

    The lower precision float type will almost always truncate more of the value than the higher precision double. (there are a few values where that isn't the case, i.e 1.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001
    will always be stored as 1.0)
  • 2
    I suppose 0.7 value is 0.6999999999999999999999999999
  • 4
    ugh, sorry, its Yes, not No. (My explanation is accurate but the output is obviously Yes since 0.7f is less than 0.7)
  • 2
    I like how many devs here hadn't been bitten by floats yet :)
  • 1
    @perfectdark when the compiler executed both the if and the else block you will get yesno
  • 3
    Machine epsilon isthe unavoidable evil here
  • 1
  • 3
    Hmm, checking the specification it is actually a bit less clear: the default rounding mode is implementation defined so it can round down(towards -infinity), up(towards infinity), truncate (towards zero) or to the nearest representable number so either Yes or No could be valid outputs depending on the compiler and cpu.
  • 1
    Error? Wrong indentation on line 2? Or is this just my bad?
  • 1
    @BixelPitch still better than js
  • 1
    This is why people struggle to pick up coding, you can't just say x is less than itself guys come on now
  • 1
    @Wytchley C doesn't regard whitespace as part of the logic
  • 1
  • 1
    Either yes or no. Floats lol
  • 1
    @Hedgepig ah okay, cheers my bad :)
  • 2
    It's quantum. Both yes and no until the code is run.
Add Comment