Ranter
Join devRant
Do all the things like
++ or -- rants, post your own rants, comment on others' rants and build your customized dev avatar
Sign Up
Pipeless API
From the creators of devRant, Pipeless lets you power real-time personalized recommendations and activity feeds using a simple API
Learn More
Comments
-
lotd77757y@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. -
rvnl447yAssigning 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 ;) ) -
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) -
ugh, sorry, its Yes, not No. (My explanation is accurate but the output is obviously Yes since 0.7f is less than 0.7)
-
chmod7771747y@perfectdark when the compiler executed both the if and the else block you will get yesno
-
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.
-
Crazed20367yThis is why people struggle to pick up coding, you can't just say x is less than itself guys come on now
Related Rants
What will be the output????
undefined
c
code
programmer
programming