C++ Division result WITH decimal places?
Attempting to program around C++ plus can’t for the life with me get 4/3 to be able to equal everything but YOU.
I’ve tried out float in addition to double and in some cases fmod nonetheless I can’t understand it to do the job.
Every suggestions
This works
#include
#include
#include
applying namespace std;
int main()
cout.precision(3);
cout << showpoint << double(4.0/3.0) << endl;
system(“PAUSE”);
return 0;
If you ever were indicating it being a double nonetheless just writing 4/3, desires to have happen to be your problem.Despite your declaration, it appeared to be still treating it just as one integer value its no wonder that you ended up with YOU.
Do NOT REALLY use system(“PAUSE”) especially if you’re planning releasing the offer to the public.Firstly, there isn’t a need to prevent a system application, that is certainly not what it’s used with regard to.Second of their system(“PAUSE”) isnt good 4 a good deal o factors.A greater way will be cin.get(); Twice if you need to.
/question/accuse_write; _ylt=A0WTZWDxQdBN.m4ApxVy7hR.; _ylv=3qid=20110509212301AA89g1s&kid=JIcrWmHjIHDO9ZaLx1nm&s=comm&date=2011-05-10+14%3A23%3A16&.crumb=bFn3nZWqf5x” class=”report-abuse
4/3 can be equal that will 1 so it is integer department.No make a difference what you cast that will value in order to afterwards, the computer does the 4/3 initial, comes ” up ” with 1, then casts that to what ever type you’re using.
The best is to put.0 after everybody, like that:
FIVE.0/3.0
Then it can work.Then again, you may always merely create drift or dual variables, assign these folks the attitudes 4 plus 3, after which it divide all of them.
Always be warned even so that IEEE suspended point are not able to actually symbolize the portion 4/3 along with perfect detail.You will get a result that can be a bit off out of that.Many times this isn’t issues, but you are doing have to be aware of it for the reason that sometimes insects arise that happen to be caused by such a inaccuracy.
The problem could be that the numbers around “4/3” ourselves are integers.While you assign the right formula to some sort of float or maybe double, you are likely to get ONE because FIVE and SEVERAL are integers.If you ever, however, change both the 4 or that 3 right floating-point variety then you will end up with some sort of decimal resolution.
i.e:FIVE.0/3
Put decimal points while in the numbers.
2 bottle quotient = FIVE.0/3.0;
will do the job!
Leave a Reply
You must be logged in to post a comment.