I can't seem to figure this one out but I'm sure it's easy. My program seems to be truncating decimal values even though stored in a double
Both sets of code below output the number 0 when I need them to output .5, what am I doing wrong?

cout << (1/2);

double value;
value = (1/2);
cout << value;

Dani AI

Generated

As found, getting 0 instead of 0.5 is the classic integer-division trap. correctly pointed out the cause and showed casting as a remedy. A few clarifications and extra tips that often save time when this comes up later:

Integer division discards the fractional part (since C++11 the quotient is defined to be truncated toward zero), so any division where both operands have an integral type yields an integral result. Mixing signed and unsigned integers can produce surprising results because the signed operand is converted to unsigned before the operation. See the language rules for built-in arithmetic on cppreference for the precise behavior: Built-in arithmetic operators.

Practical fixes and gotchas:

  • Make sure at least one operand is a floating-point type before dividing (literal with a decimal or a variable already typed as float/double), or convert explicitly. Prefer C++-style casts for clarity when you must convert.
  • Watch library usage: e.g., using an integer initial value in algorithms that accumulate (like std::accumulate) can force integer accumulation and lose fractions; supplying a floating-point initial value keeps the accumulation in floating point.
  • For output, remember cout’s default formatting may hide precision concerns. To show a fixed number of decimal places, use the iomanip manipulators.

Example (formatting output) — this shows using fixed output with set precision:

#include <iostream>
#include <iomanip>

int main() {
    double numerator = 5.0;
    double denominator = 2.0;
    double result = numerator / denominator;
    std::cout << std::fixed << std::setprecision(2) << result << '\n'; // prints 2.50
}

For deeper reading, the I/O manipulators documentation is helpful: std::setprecision.

Recommended Answers

All 2 Replies

Even though you store it in a double, you are still doing integer division because both operands to the slash are integers. Add a decimal point to at least one of them to get decimals. value = 1.0 / 2;

Yeah, Nucleon is right.
What nucleon suggested is called implicit type conversion. The compiler in this case will convert the type of 2 from int to double. Hence the division performed will be on decimals(rather than integers).
To do a explicit type conversion, use the static_cast<type_name>(operand)
For eg:

int a=1,b=2;
//cast promotes the type of int a to double
double value = static_cast<double>(a)/static_cast<double>(b);
//you can even use cast on one operand like:
//value=static_cast<double>(a)/b; 
cout << value;

Learn about casts. Although they are evil, but you will need them in some cases.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.