Greedo
Greedo

Reputation: 5523

Force integer division on a double?

I have

x /= y;

Where x & y are both double

I would like x to be the integer part of x/y , how do I do this?

I have tried

x /= y;
x = x.intValue();

But am receiving a double cannot be dereferenced error in TIO which I presume means the double x does not have that method

IO x = x\y: Carry out float division then round towards -∞

Sample table

NB all I'm after is to change this code to add in floor division with \

Upvotes: 0

Views: 3253

Answers (3)

Dorukhan Arslan
Dorukhan Arslan

Reputation: 2754

If a smaller data type is assigned to a bigger data type, there'll be no error. But the assignment of bigger to smaller gives error. In this case, you need to make compatible these data types with each other using type conversion ('x = (Type) y'). Converting a double to int is an example of assigning a bigger data type (double) to smaller (int). When we perform this operation, the double variable lost its precision and its "integer part" is assigned to the int variable.

double x = 3, y = 2;
x /= y;
int integerPart = (int) x;
System.out.println(integerPart); // Prints 1

From small to big, the numeric data types are as follows btw:

byte < short < int < long < float < double

Edit: After your last edit I realized what you actually ask. Your first expression was wrong. You don't want to find integer part of the double result of division, you want its floor. Just use java.lang.Math.floor:

double[] x = {-10, -7, 1, 3, 7.1, 9.5};
double[] y = {-10, -7, -1.7, 0.5, 7.1, 9.5};
for (int i = 0; i < y.length; i++) {
  for (int j = 0; j < x.length; j++)
    System.out.print(Math.floor(x[j] / y[i]) + " ");
    System.out.println();
}

Upvotes: 3

T.J. Crowder
T.J. Crowder

Reputation: 1073968

Force integer division on a double?

To force integer division, use int or long (for the calculation part); long would probably be the better choice:

x = (double)((long)x / (long)y);

That uses an explicit cast back to double for emphasis; you can just write it with the implicit cast back to double if you prefer:

x = (long)x / (long)y;

Do note that (long)y on a non-zero y can result in 0 (for instance, if y is 0.3), which then ends up being division-by-zero and thus a runtime exception.

I would like x to be the integer part of x/y

That's a different question than the title; that's not integer division, that's getting the integer part of the result of floating point division. If that's what you want, just cast the result:

x = (long)(x / y);

...(and of course the long is then implicitly cast back to double) or use Math.floor on it.


I have tried

x /= y;
x = x.intValue();

But am receiving a double cannot be dereferenced error

Right. x is a double, not Double. Primitives (like double) don't have methods, only reference types (like Double) do.

Upvotes: 4

laune
laune

Reputation: 31290

x = java.lang.Math.floor(x/y);

Relying on some Math function is arguably the best choice. "Returns the largest (closest to positive infinity) double value that is less than or equal to the argument and is equal to a mathematical integer."

If you need the symmetric version (truncation towards zero), you'll have to handle negative quotients:

 floor(abs(x/y))*signum(x/y)

Upvotes: 4

Related Questions