Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

float/int implicit conversion

I'm doing multiplication and division of floats and ints and I forget the implicit conversion rules (and the words in the question seem too vague to google more quickly than asking here).

If I have two ints, but I want to do floating-point division, do I need only to cast one or both of the operands? How about for multiplication — if I multiply a float and an int, is the answer a float?

like image 411
Rich Avatar asked Nov 04 '10 14:11

Rich


People also ask

What types can int be implicitly converted to?

Any integral numeric type is implicitly convertible to any floating-point numeric type. There are no implicit conversions to the byte and sbyte types. There are no implicit conversions from the double and decimal types. There are no implicit conversions between the decimal type and the float or double types.

Can we convert int to float in Java?

So, when you assign an int value to float variable, the conversion of int to float automatically happens in Java. This is also called widening casting or widening primitive conversion. So, to convert an in to float using widening casting, you can just assign a value of int to float.

What is a implicit conversion?

An implicit conversion sequence is the sequence of conversions required to convert an argument in a function call to the type of the corresponding parameter in a function declaration. The compiler tries to determine an implicit conversion sequence for each argument.

Can float convert to int C++?

Use Direct Assignment to Convert Float to Int The conversion between the float and int values can be done using the assignment operator. In this case, the float variable will be implicitly converted to the int type, and the value will be narrowed down to the second type, losing all digits after the decimal point.


5 Answers

You can’t assign to an int result from division of a float by an int or vice-versa.

So the answers are:

If I have two ints, but I want to do floating point division…?

One cast is enough.

If I multiply a float and an int, is the answer a float?

Yes it is.


float f = 1000f;
int i = 3; 

f = i; // Ok
i = f; // Error

f = i/f; //Ok 0.003
f = f/i; //Ok 333.3333(3)

i = i/f; //Error
i = f/i; //Error
like image 136
Damian Leszczyński - Vash Avatar answered Sep 26 '22 13:09

Damian Leszczyński - Vash


To demonstrate:

 int i1 = 5;
 float f = 0.5f;
 int i2 = 2;
 System.out.println(i1 * f);
 System.out.println(i1 / i2);
 System.out.println(((float) i1) / i2);

Result:

2.5
2 
2.5
like image 20
Bozho Avatar answered Sep 27 '22 13:09

Bozho


In order to perform any sort of floating-point arithmetic with integers, you need to convert (read: cast) at least one of the operands to a float type.

like image 39
Matt Ball Avatar answered Sep 26 '22 13:09

Matt Ball


If at least one of the operands to a binary operator is of floating-point type, then the operation is a floating-point operation, even if the other is integral.

(Source: Java language specifications - 4.2.4)

if I multiply a float and an int, is the answer a float?

System.out.println(((Object)(1f*1)).getClass());//class java.lang.Float

(If you use DrJava, you can simply type ((Object)(1f*1)).getClass() into the interactions panel. There's a plugin for DrJava for Eclipse too.)

like image 23
blizpasta Avatar answered Sep 26 '22 13:09

blizpasta


The simple answer is that Java will perform widening conversions automatically but not narrowing conversions. So for example int->float is automatic but float->int requires a cast.

like image 25
user207421 Avatar answered Sep 27 '22 13:09

user207421