0

I have studied that when division is performed then if there are 2 datatypes like there is a float value which is divided by an int value then it's converted to the much higher precision i.e. float.

So is it always the case with arithmetic operations that compiler will do an implicit type conversion or do we have to perform this type casting explicitly somewhere or else it will give error.

  • 1
    https://stackoverflow.com/questions/16221776/why-dividing-two-integers-doesnt-get-a-float – Michael Rovinsky May 11 '21 at 16:36
  • Does this answer your question? [Why dividing two integers doesn't get a float?](https://stackoverflow.com/questions/16221776/why-dividing-two-integers-doesnt-get-a-float) – DeBARtha May 11 '21 at 16:45

1 Answers1

4

In general, you are not required to cast operands when performing arithmetic. C has a number of rules for automatically converting operands, and they serve well in many situations.

In many current C implementations, float is not more precise than int. int is commonly 32 bits, and float has 24 bits for the significand (fraction portion of the floating-point number), along with about eight for the exponent and one for the sign. This gives float wider range but less precision. The conversion rules give a ranking of types used for the conversions, but it is not strictly from less precise to more precise.

The automatic conversions do not serve all situations, and C programmers need to become familiar with the rules so they know when to add casts. These include:

  1. When the result is not representable, or is not representable with desired accuracy, in the default type.
  2. When the automatic conversions would cause errors in one of the operands.

An example of 1 is when we want to divide two integers and get a floating-point result:

float x = 1/3; // Wrong, integer division is performed, yielding zero, but we want (approximately) ⅓.
float x = (float) 1 / 3; // Right, convert at least one operand to float (or use 1.f, a float constant).

Another example is when two integers might overflow:

int x = Some large integer;
int y = Some large integer;
long z = x*y; // Wrong, result may overflow.
long z = (long) x * y; // Possibly right, long may be wide enough to represent product.

An example of 2 is when conversion from int to float may lose precision:

float x = 2;
int y = 123456789;
double z = x*y; // Wrong, converting 123456789 to float loses precision and produces 123456792 in many C implementations.
double z = x * (double) y; // Right, double has enough precision in many C implementations.
Eric Postpischil
  • 195,579
  • 13
  • 168
  • 312
  • Thanks it was very useful – Ananya Nayak May 11 '21 at 17:14
  • You say `int z = (long) x * y` might be right, but wouldn't it overflow in `z` even if the rvalue is a `long`? Also, to check before assignment, what's best practice? My guess is you might want to divide `LONG_MAX` by `x` and check to see if `y` is smaller than the result. However, division is slow. There has to be a better way? – torstenvl May 11 '21 at 18:37
  • @torstenvl: Thanks, fixed. – Eric Postpischil May 11 '21 at 18:39