in this program i'm writing, i seem to keep getting rounded down values. At first i though it was because i was declaring my return variable as an integer so i made sure i wasnt doing that and changed all number variables in the class to doubles to ensure i wasnt going wrong there and still i divide 3 into 4 and get 0.

double fraction::percent()
{
  double _percent=_top/_bottom*100+(_whole*100);
   cout<<_bottom<<endl<<endl<<"percent "<<3/4/**100+(_whole*100)*/<<endl;
 return(_percent);
}

the calculation was 3/4*100+_whole*100, i commented out all but the first part to see what 3/4 returns and i get 0 instead of .75.

Dani AI

Generated

Short answer: the division is happening with integer operands so the fractional part is lost before the multiplication. identified the root cause; 's naming warning is worth heeding too — verify the actual types declared in the class header (changing a local variable to double doesn't help if the members are still int).

A clear, robust calculation that avoids integer truncation and makes intent explicit:

double percent = (whole + static_cast<double>(top) / bottom) * 100.0;

Converting one operand with static_cast<double> forces floating-point division; grouping with parentheses computes the mixed number first, then scales. Add a defensive check for bottom != 0 before dividing.

Naming and style notes (echoing ): avoid leading underscores. Identifiers that begin with an underscore followed by an uppercase letter or another underscore are reserved to the implementation, and names that begin with an underscore are reserved in the global namespace. Prefer top_, m_top or similar for member names to avoid subtle collisions.

Formatting and verification: to display the result with a fixed number of decimals, use std::fixed and std::setprecision. Also, confirm at compile-time that the intended variables are floating-point (or inspect declarations in the class header) rather than relying on incidental promotion at expression time. These small changes remove the truncation, make the calculation explicit, and reduce future surprises.

Recommended Answers

All 2 Replies

1) Don't create variables with _ as the first character. That's used for internal values
2) top, bottom, whole are probably integers.

The issue is integer division vs. floating-point division.

Integer division divides an integer divisor into an integer dividend and produces an integer quotient. For example, if you have 11/3, integer division will produce a truncated integer result of 3 instead of the actual correct value of 3.6667, which is a floating-point value.

In your code, you have this:

3/4 /**100+(_whole*100)*/ << endl;

notice how the 3 and the 4 do not have decimal points. This causes C++ to interpret them as integers, which results in int / int. This combination results in integer division.

Can you think of a way to change this code so that it results in double / double which will give you floating-point division instead?

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.