answersLogoWhite

0

When dividing one integer with another, there's a high probability that the answer will not be an integer. To deal with this, you must cast the numerator or denominator to a floating point type (float or double). By way of an example, consider the following code snippet:

int x=5, y=2;

int z = x/y;

float f1 = x/y;

float f2 = ( float ) x/y;

float f3 = x/( float ) y;

printf( "x=%d, y=%d, z=%d, f1=%f, f2=%f, f3=%f\n", x, y, z, f1, f2, f3 );

Output:

x=5, y=2, z=2, f1=2.000000, f2=2.500000, f3=2.500000

Clearly the correct answer is shown by f2 and f3. This is because we explicitly cast the numerator or denominator in each case. So long as we cast one or the other (or both), the result will be correct. If we don't cast either, then we're effectively only converting the result of the integer division, which is effectively the same as casting z to a float, as shown by f1.

See the related links section for in-depth information on floating point arithmetic in C/C++.

User Avatar

Wiki User

12y ago

What else can I help you with?

Related Questions

Why do you make a remainder in division a fraction or decimal?

So that the answer from the division is a single quotient.


Why can you write a division problem as a fraction?

when you are writing a division problem as a fraction u take the remainder of your problem and make it the numerator and make the number you are dividing by the denominator


Can division make a number larger?

Only if you are dividing by a decimal or a fraction.


How do you do 36 divide by 7000?

To divide 36 by 7000, you would perform the division operation by setting up the problem as follows: 36 ÷ 7000. To solve this division problem, you would first place a decimal point after 36 to make it 36.0. Then, you would move the decimal point three places to the right in both the dividend and divisor to make the divisor a whole number. This gives you 0.00514 as the quotient when dividing 36 by 7000.


How do you make 4 over 24 a decimal?

Any fraction can be rearranged as a division problem. Divide the top number by the bottom number.In this case, divide 4 by 24 and get 0.166666... ■


How you make a ratio to a decimal?

Divide the numerator by the denominator using long division.


How do you make 4 over nine a decimal?

Divide 4.000.. by 9, as a long division, but make sure the decimal point for the answer is directly above the decimal point for 4.000... This should give you 0.444...


How do make a fraction to a decimal?

Do the division. 3/4 = 3 divided by 4 = 0.75


How do you you make 1 over 8 in to a decimal?

Do the long division. 1/8 = 0.125


How do you make a fraction inyo a decimal?

You divide the numerator by the denominator. It may need to be a long division.


How do you find a divisor?

make a division problem and find the lowest number


What direction does the decimal move in micrometers to millimeters?

When going from smaller to larger units, there will be fewer of them. That indicates division, which will make it appear as if the decimal point is going to the left.