+ 4
Why my solution doesn’t produce correct output when I = 2?
It should work since the if statement is working for I = 1 and 2. Problem: https://www.urionlinejudge.com.br/judge/en/problems/view/1098 https://code.sololearn.com/ca22A08A9a21/?ref=app
17 odpowiedzi
+ 9
Alpha Caspian Because decimal is not precise in programming. A famous example is 0.1+0.2 != 0.3. It's not only in C++, but basically every languages which have the same decimal system.
Though float can correctly hold up to 7 decimals, doing calculation with it may produce an approximate or an accurate value. We can't tell which it will be unless output it. Same things happen to double.
By doing printf("%.7f", I), you can see that there is a 2 at the 7th decimal. I guess it's it so 2.0000002 != 2.
That being said, I don't know why in C++ it doesn't output 2.0000002 unless you explicitly set the precision.
If you want, you can use if(I==0 || I==1 || I==2.0000002f). I tested it and it works.
+ 4
Because this is C++, you can use cout and let it omit the zero decimals output for you.
Remove the if and else statements and add
cout << "I=" << I << " J=" << I+J << "\n";
+ 3
Alpha Caspian f tells the compiler to treat 2.0000002 as float, without it, the number would be treated as double and the variable I will implicitly convert to double temporarily when compare to equality.
Basically, it won't go well because float can only hold up to 7 decimals, while double is more precise and stores up to 15 decimals. The further decimal won't be zeros but a bunch of inaccurate numbers stored in the float.
There are also L and LL which can be placed after int, indicating long and long long respectively.
+ 3
Alpha Caspian I don't know, but you can check out the way C++ and most languages store decimal:
https://en.m.wikipedia.org/wiki/IEEE_754#Basic_and_interchange_formats
It's worth to check out, but honestly there isn't a practical use from knowing it in programming as you still cannot prevent the inaccuracy.
+ 2
.0f part makes it to output decimals
+ 1
I see. Thank you so much CarrieForle.
0
CarrieForle I got your point but it should work for printf also with this code. But why it isn’t working for 2? I don't get it.
0
Alpha Caspian, it does work for I = 2 it outputs 3.0 , 4.0 and 5.0
0
Aleksei Radchenkov Though it gives the right output but It's not matching the problem requirements here.
You it says
For I=2 j should be 3, 4,5 not 3.0, 4.0 5.0. That means no zero decimals
0
I is indeed different from 2, it's 2.0000002384...
Everytime you add 0.2 you're introducing in your result a bit of noise
Doing it 5 times is still fine, but 10 times is enough for the error to become noticeable
0
Alpha Caspian, but that's what you did by printf("I=%.0f J=%.0f\n", I,J+I); which adds 0.0 to output....
0
But .0f means it will Just print without 0decimals.
0
Thanks CarrieForle I got your point now.
0
CarrieForle in the condition u used I==2.0000002f. Is it necessary to put f in last decimay place? Why?
0
CarrieForle sorry to bother you again. Another question poped on my head. Why the value is increasing in this manner? Like before 2.0000002 it was 1.8000001. I mean at 7th decimal it's increasing. Why?
0
Ok CarrieForle Again Thanks a bunch.
- 2
It's worth to check out, but honestly there isn't a practical use from knowing it in programming as you still cannot prevent the inaccuracy.