186

In another Bruce Eckels exercise in calculating velocity, v = s / t where s and t are integers. How do I make it so the division cranks out a float?

class CalcV {
  float v;
  float calcV(int s, int t) {
    v = s / t;
    return v;
  } //end calcV
}

public class PassObject {

  public static void main (String[] args ) {
    int distance;
    distance = 4;

    int t;
    t = 3;

    float outV;

    CalcV v = new CalcV();
    outV = v.calcV(distance, t);

    System.out.println("velocity : " + outV);
  } //end main
}//end class
Ciro Santilli OurBigBook.com
  • 347,512
  • 102
  • 1,199
  • 985
phill
  • 13,434
  • 38
  • 105
  • 141

9 Answers9

371

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

2240
  • 1,547
  • 2
  • 12
  • 30
Alnitak
  • 334,560
  • 70
  • 407
  • 495
  • 9
    Grrr, this took me about 30 mins till i found this and figured it out. So simple. :D – Rihards Apr 16 '11 at 23:21
  • More specifically, this particular rule is mentioned here: [Multiplicative Operators](http://java.sun.com/docs/books/jls/third%5Fedition/html/expressions.html#239829), so let it stand here for future reference. – quantum May 02 '11 at 08:39
  • 5
    (For anyone coming across this question later, the given links are broken. The new ones are: http://docs.oracle.com/javase/specs/jls/se7/html/jls-4.html#jls-4.2.4 and http://docs.oracle.com/javase/specs/jls/se7/html/jls-15.html#jls-15.17) – Steve Haley Apr 21 '12 at 15:23
18

Try:

v = (float)s / (float)t;

Casting the ints to floats will allow floating-point division to take place.

You really only need to cast one, though.

anisoptera
  • 1,098
  • 7
  • 17
5

Cast one of the integers to a float to force the operation to be done with floating point math. Otherwise integer math is always preferred. So:

v = (float)s / t;
Jason Coco
  • 77,985
  • 20
  • 184
  • 180
4

To lessen the impact on code readabilty, I'd suggest:

v = 1d* s/t;
antyrat
  • 27,479
  • 9
  • 75
  • 76
3

Cast one of the integers/both of the integer to float to force the operation to be done with floating point Math. Otherwise integer Math is always preferred. So:

1. v = (float)s / t;
2. v = (float)s / (float)t;
Nikhil Kumar
  • 2,618
  • 3
  • 21
  • 24
  • I was lazy to figure out this all by myself, so I searched. I found out that I needed to cast both variables for it to work. So this one worked well – Noah Apr 25 '22 at 04:09
3

You can cast the numerator or the denominator to float...

int operations usually return int, so you have to change one of the operanding numbers.

Jhonny D. Cano -Leftware-
  • 17,663
  • 14
  • 81
  • 103
3

You can cast even just one of them, but for consistency you may want to explicitly cast both so something like v = (float)s / (float)t should work.

Uri
  • 88,451
  • 51
  • 221
  • 321
2

JLS Standard

JLS 7 15.17.2. Division Operator / says:

Integer division rounds toward 0. That is, the quotient produced for operands n and d that are integers after binary numeric promotion (§5.6.2) is an integer value q whose magnitude is as large as possible while satisfying |d · q| ≤ |n|. Moreover, q is positive when |n| ≥ |d| and n and d have the same sign, but q is negative when |n| ≥ |d| and n and d have opposite signs.

This is why 1/2 does not give a float.

Converting just either one to float as in (float)1/2 suffices because 15.17. Multiplicative Operators says:

Binary numeric promotion is performed on the operands

and 5.6.2. Binary Numeric Promotion says:

  • If either operand is of type double, the other is converted to double.
  • Otherwise, if either operand is of type float, the other is converted to float
Ciro Santilli OurBigBook.com
  • 347,512
  • 102
  • 1,199
  • 985
0

Try this:

class CalcV 
{
      float v;
      float calcV(int s, int t)
      {
          float value1=s;
          float value2=t;
          v = value1 / value2;
          return v;
      } //end calcV
}
Floern
  • 33,559
  • 24
  • 104
  • 119