C++: unexpected outcome from division of integers, any idea why?
Without doubt all.
We are using this code::blocks IDE having mingw.
ok the issue here is actually:
say u create this( IOstream & an std of course):
cout<<1/10.0;
you’ll naturally receive 0.ONE right
ok in order that is totally fine.
but what if u do such as this instead:
cout.precision(30);
cout<<1/10.0;
and also u could try 2/10.0 and also 3/10.0
you’ll identify that u purchase a different final result.U’d acquire 0.100000000000451 or similar to that.
that is my dilemma.I mean while i am doing various other calculations, that interferes and ruins anything.what must i do people
many thanks all in advance.:)
Say you’re going to do math to five decimal locations.What comes about when you 1/3 situations 3 To 10 decimal destinations, 1/3 will be.3333333333 so when you boost it through 3, you become.9999999999 that is not just 1.That’s this nature associated with math by using fixed detail — if you manipulate volumes that can not be represented accurately, you cannot get exactly the right response.
1/10 provides no correct binary representation exactly like 1/3 provides no precise decimal rendering.So you’re going to get errors.
Your expectation was in error.You’ll never receive exact responses from fixed-precision, flying point maths.
The treatment for your base problem is usually to code what you long for.For example, if you want a school that precisely handles realistic numbers to be a ratio of two integers, code one (or find one).If you’d like an arbitrary precision flying point school, code you (or find one).Depending on which you desperately want, GNU MPFR would be the answer.
Everything is definitely working since it should.The use of 30 throughout cout.precision(30); you’re going to get the response upto THIRTY places from your decimal issue.Simply decrease the count from the precision perform and almost everything should go when you want.
i still have no idea what a person’s problem is definitely…what result do you think you’re getting could be the problem your precision belongs afterwards reset it.
Leave a Reply
You must be logged in to post a comment.