+ 4
Toughest Challenge EVER! Guess The output! (DAILY CHALLENGE HOT)
Guess The Output. -------------------------------- No cheating! #include<stdio.h> int main() { float x=0.6,y=0.7,z=0.8; printf("%d%d%d",x<0.6?1:0,y<0.7?1:0,z<0.8?1:0); } A)000 B)010 C)101 D)111 Answer this Question with correct explanation. Please upvote if you like it. SHARE IT AS MUCH AS POSSIBLE! I BET MAXIMUM PEOPLE WILL GET IT WRONG!
16 Respuestas
+ 9
Guys, I realised why the answer is 010. (Thought of it while at school)
Firstly, all values are stored as binary for computers.
Now, try to represent 0.7 in binary; You cant. Its simply like 1divided by 3 in Base10, you get 0.33333 instead of the actual value.
So basically (if I did my conversion correct)
Base2's 0.1111111111.. is NOT Base10's 0.7
+ 8
float a = 0.6f; // .600000002
float b = 0.7f; // .699999999
float c = 0.8f; // .800000001
float d = 0.9f; // .899999998
bool x = a < 0.6 ? 1 : 0; // 0
bool y = b < 0.7 ? 1 : 0; // 1
bool z = c < 0.8 ? 1 : 0; // 0
bool s = d < 0.9 ? 1 : 0; // 1
I really don't know why the float values go up and down by epsilon.
+ 6
@Babak oh I see. So its floating point problum, thx for sharing.
@Saurabh nice challange. Got into the usual C++ trap.
+ 5
Hey friends, It is so strange. The Answer is Not D. it's actually B.
In a minute I'll tell you.
+ 5
Dear Saurabh,
That's truly a Hard one. Thanks.
+ 5
But I learned my lesson!
Don't compare two float or double numbers in a nomal way like integers. Rather, compare them within a range.
// E.G. Using
float f = .5f;
const float Epsilon = .0001;
if ( f < (.5 + Epsilon) || f > (.5 - Epsilon) )
// Instead of
if (f == .5)
Something like that.
+ 4
Dear wen,
Thank you. In fact last night I figured another challenging area in terms of floating point precision. When I want to add 2^59 to 2^0 by using a loop I noticed that I get an incorrect output.
long long num = 0;
for (int x = 0; x < 60; ++x)
num += powl(2, x);
cout << num; // 115292150460684697 6
but the correct answer was 115292150460684697 5
After doing a little research, I figured that because my sum made a huge number, POOR float couldn't handle that and an overflow happened( exactly because of precision issue ). So I decided to cast the result of powl() in each cycle to long long type.
num += (long long) powl(2, x);
And I got what I want.
I can now conclude my perception of these guys called float and double.
+ 3
@Babak I guess it has something to do with binary but my brain fails me. 😲
+ 1
The default data type for decimal numbers in C is *double*. Since double have higher precision (more accurate due to more bytes for storage) than float, ALL the conditionals are true and thus will return 1.
My vote goes into D as well. ✌
P/S: Don't trust me, I fell into the trap as well.
+ 1
@Sayan Chandra, your answer is wrong. Btw I m Saurabh, not Sourabh.
0
I will comment the RIGHT answer after sometime when this question gets some specific amount of comments.
I BET MAXIMUM PEOPLE WILL GET IT WRONG.
Please READ the code AGAIN!
0
then it must be 010
explaination
x=0.digit---> if digit is odd and greater than 5
then x<0.digit gives 1
if digit less than 5 it gives 0
PURE ROUNDING OFF RULE
0.67=0.7
0.65=0.6
0.64=0.6
0.75=0.8
0.94=0.9
- 1
@ SOURABH @
the ans is looking like 111
x y z will be taken as 0 as operated with%d
- 1
different challenge on the way
TUESDAY TALES
https://www.sololearn.com/discuss/646010/?ref=app
- 1
heres my new dose
check it out
https://www.sololearn.com/discuss/658245/?ref=app