double doesnt show behind ,

Solved: one has have a decimal or be a double.

1
2
     double x= 45/10;
    cout << x;


output is 4. not 4.5, but 4.

These are the includes:
#include<iostream>
#include<time.h>
#include<stdlib.h>
#include<vector>
#include<windows.h>
#include<cstdio>

Anything else that could be causing this?
Last edited on
Anything else that could be causing this?

The lack of a decimal point.

Try 45.0 / 10;
Last edited on
Got it

Last edited on
It evaluates 45/10 first.

Since neither number has a decimal point they are taken to be integers, implying that the result has to be an integer ... and so it does integer division, truncating, where necessary, toward zero. Thus 45/10 becomes the integer 4.

This is then assigned to the double x, becoming, at this point, 4.0.

As I said, if you want the more accurate answer, at least one of 45 and 10 must either be given a decimal point or explicitly cast to a double.

Remember that the RHS is worked out first, before your code starts worrying what it will assign it to subsequently.
Last edited on
Topic archived. No new replies allowed.