Why Does Dividing Two Int Not Yield the Right Value When Assigned to Double

Why does dividing two int not yield the right value when assigned to double?

This is because you are using the integer division version of operator/, which takes 2 ints and returns an int. In order to use the double version, which returns a double, at least one of the ints must be explicitly casted to a double.

c = a/(double)b;

Why dividing two integers doesn't get a float? [duplicate]

This is because of implicit conversion. The variables b, c, d are of float type. But the / operator sees two integers it has to divide and hence returns an integer in the result which gets implicitly converted to a float by the addition of a decimal point. If you want float divisions, try making the two operands to the / floats. Like follows.

#include <stdio.h>

int main() {
int a;
float b, c, d;
a = 750;
b = a / 350.0f;
c = 750;
d = c / 350;
printf("%.2f %.2f", b, d);
// output: 2.14 2.14
return 0;
}

Integer division: How do you produce a double?

double num = 5;

That avoids a cast. But you'll find that the cast conversions are well-defined. You don't have to guess, just check the JLS. int to double is a widening conversion. From §5.1.2:

Widening primitive conversions do not
lose information about the overall
magnitude of a numeric value.

[...]

Conversion of an int or a long value
to float, or of a long value to
double, may result in loss of
precision-that is, the result may lose
some of the least significant bits of
the value. In this case, the resulting
floating-point value will be a
correctly rounded version of the
integer value, using IEEE 754
round-to-nearest mode (§4.2.4).

5 can be expressed exactly as a double.

Type casting does not produce expected behavior [duplicate]

The important thing is at least one element of calculation be a float-double. To get a double result you need to cast one of them as shown below:

    int result = 40;
cout << "Result casted to a double " << (static_cast<double>(result)/60) << endl;

Or you can create it directly as a float-double.

    double result2 = 40.0 / 60;
cout << "Result casted to a double " << result2 << endl;

Note that one of elements must have the '.0' to indicate a division of a float-double type by an integer in the case above. Otherwise, despite the result2 be a double, the result will be zero too.

Why does integer division in C# return an integer and not a float?

While it is common for new programmer to make this mistake of performing integer division when they actually meant to use floating point division, in actual practice integer division is a very common operation. If you are assuming that people rarely use it, and that every time you do division you'll always need to remember to cast to floating points, you are mistaken.

First off, integer division is quite a bit faster, so if you only need a whole number result, one would want to use the more efficient algorithm.

Secondly, there are a number of algorithms that use integer division, and if the result of division was always a floating point number you would be forced to round the result every time. One example off of the top of my head is changing the base of a number. Calculating each digit involves the integer division of a number along with the remainder, rather than the floating point division of the number.

Because of these (and other related) reasons, integer division results in an integer. If you want to get the floating point division of two integers you'll just need to remember to cast one to a double/float/decimal.

Why is my double or int value is always 0 after division?

Your current calculation only involves integers and so will be affected by integer division (which truncates the result to the nearest integer value).

(currPixel / modifier) * newValue
| |
---------------integer division e.g. 10/3 = 3, not 3.333

The result is then cast to double, but the accuracy is lost before this point.

Consider the following:

#include <iostream>
using namespace std;

int main() {
int val1 = 10;
int val2 = 7;
int val3 = 9;

double outval1 = (val1 / val2) * val3;
double outval2 = ((double)val1 / val2) * val3;
cout << "without cast: " << outval1 << "\nwith cast: "<< outval2 << std::endl;

return 0;
}

The output of this is:

without cast: 9
with cast: 12.8571

See it here

Note that the cast has to be applied in the right place:

(double)(val1 / val2) * val3 == 9.0      //casts result of (val1/val2) after integer division
(val1 / val2) * (double)val3 == 9.0 //promotes result of (val1/val2) after integer division
((double)val1 / val2) * val3 == 12.8571 //promotes val2 before division
(val1 / (double)val2) * val3 == 12.8571 //promotes val1 before division

Due to promotion of the other operands, if in doubt you can just cast everything and the resulting code will be the same:

((double)val1 / (double)val2) * (double)val3 == 12.8571  

It is a little more verbose though.

Dividing two integers to produce a float result [duplicate]

Cast the operands to floats:

float ans = (float)a / (float)b;


Related Topics



Leave a reply



Submit