Dividing Two Integers to Produce a Float Result

Dividing two integers to produce a float result

Cast the operands to floats:

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

Why dividing two integers doesn't get a float?

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;
}

How to make the division of 2 ints produce a float instead of another int?

Just cast one of the two operands to a float first.

v = (float)s / t;

The cast has higher precedence than the division, so happens before the division.

The other operand will be effectively automatically cast to a float by the compiler because the rules say that if either operand is of floating point type then the operation will be a floating point operation, even if the other operand is integral. Java Language Specification, §4.2.4 and §15.17

How to get a float result by dividing two integer values using T-SQL?

The suggestions from stb and xiowl are fine if you're looking for a constant. If you need to use existing fields or parameters which are integers, you can cast them to be floats first:

SELECT CAST(1 AS float) / CAST(3 AS float)

or

SELECT CAST(MyIntField1 AS float) / CAST(MyIntField2 AS float)

integer division to float result

When you divide two ints you perform integer division, which, in this case will result in 22/64 = 0. Only once this is done are you creating a float. And the float representation of 0 is 0.0. If you want to perform floating point division, you should cast before dividing:

ws = ((float) zahl1) / i;

How do I get a float value when dividing two integers? (PHP)

Under normal circumstances your code should return the floating value 0.923076...

The reason you get a rounded integer might be because you have your ini setting for "precision" set to 0, to fix this either edit your php.ini or use ini_set("precision", 3); in your code before the calculation.

Another way to workaround this is to use BCmath:

echo $value=bcdiv($a, $b, 3);

And yet another way without using any extension is to use a little math trick by multiplying the value you want to divide by 1000 to get 3 decimals.

This way you'll divide 12000 by 13 and the whole part will be 923, then since you multiplied by 1e3 insert a comma/dot before the last most 3 places.

function divideFloat($a, $b, $precision=3) {
$a*=pow(10, $precision);
$result=(int)($a / $b);
if (strlen($result)==$precision) return '0.' . $result;
else return preg_replace('/(\d{' . $precision . '})$/', '.\1', $result);
}

echo divideFloat($a, $b); // 0.923

How to get a floating point result from dividing two integers

Testing your code, it does indeed give 3.333, because the typecast takes precedence... did you execute some other code?


Another possible option is to typecast b.

rs = a / (float)b;

You could also typecast a, but you'd need an extra set of parenthesis.

Here's an ideone demo.

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.

What's the right way to divide two Int values to obtain a Float?

You have to convert the operands to floats first and then divide, otherwise you'll perform an integer division (no decimal places).

Laconic solution (requires Data.Function)

foo = (/) `on` fromIntegral

which is short for

foo a b = (fromIntegral a) / (fromIntegral b)

with

foo :: Int -> Int -> Float

Dividing two integers and rounding up the result, without using floating point

With help from DyP, came up with the following branchless formula:

int idiv_ceil ( int numerator, int denominator )
{
return numerator / denominator
+ (((numerator < 0) ^ (denominator > 0)) && (numerator%denominator));
}

It avoids floating-point conversions and passes a basic suite of unit tests, as shown here:

  • http://ideone.com/3OrviU

Here's another version that avoids the modulo operator.

int idiv_ceil ( int numerator, int denominator )
{
int truncated = numerator / denominator;
return truncated + (((numerator < 0) ^ (denominator > 0)) &&
(numerator - truncated*denominator));
}
  • http://ideone.com/Z41G5q

The first one will be faster on processors where IDIV returns both quotient and remainder (and the compiler is smart enough to use that).



Related Topics



Leave a reply



Submit