+ 3

Modulus operation on floating numbers???

public class MyClass { public static int main(String[] args) { float value=23; float res=value%6; System.out.println(res); } } Output: 5.0 public class MyClass { public static int main(String[] args) { float value=23.0; float res=value%6; System.out.println(res); } } Results in Error :( Can anyone please explain the reason behind this?

29th Sep 2017, 5:48 PM
Priyanka P
Priyanka P - avatar
13 Respostas
+ 3
23.0 is a double, not a float. By default, decimal values are of type double. The reason this causes an error is because a float cannot be given the value of a double. This is to avoid data loss because a double is larger than a float. In order to explicity say we are talking about a float instead of a double, you must add an 'f' at the end of the decimal. Example/ float a = 23.0f; The reason float a = 21; Does not cause an error, is because 21 is an int. A float is larger than an int, and therefore the float can be assigned the value of an int without data loss.
29th Sep 2017, 5:57 PM
Rrestoring faith
Rrestoring faith - avatar
+ 3
A float is still a decimal number, just like a double. But a float is smaller (32bits) than a double (64 bits). This means a double can hold larger numbers / more decimals. The important thing is that decimal numbers are by default doubles in Java. So, 23.0 will be a double. 23.0f will be a float.
29th Sep 2017, 6:14 PM
Rrestoring faith
Rrestoring faith - avatar
+ 3
Yes it can be held into 32 bits. However, the compiler is not going to test whether or not it can be placed into a float every time you assign something. This is an extra step that is more expensive than just avoiding potential loss of data all together. (You might also need special cases for things like Double.Infinity or Double.NaN) There is also the issue of floating point precision. Decimals are not always fully precise, so a simple math calculation may not be the exact '5.5' you were looking for. It may be read as '5.4444444447' in a float, and '5.44444444444444489' on a double. In this case, you would have data loss trying to assign the expected 5.5 as a double to a float. Even without realizing it. Java chose to avoid any of these issues by just forcing the programmer to have to cast the type so he knows data-loss won't be an issue. In something like C++, they would assign the double to a float regardless of whether or not data-loss occurs.
29th Sep 2017, 6:30 PM
Rrestoring faith
Rrestoring faith - avatar
+ 3
By default decimals are of type double. 23.0 is a double. float x; Is creating a float. But you cant assign a float the value of a double. float x = 55.0; // ERROR So, you must cast the double to a float or indicate its type. float x = 55.0f; // OK
30th Sep 2017, 8:27 PM
Rrestoring faith
Rrestoring faith - avatar
+ 3
😀
1st Oct 2017, 5:17 PM
Rrestoring faith
Rrestoring faith - avatar
+ 2
Thanks, but if 23.0 is not a float, then what is float??
29th Sep 2017, 6:06 PM
Priyanka P
Priyanka P - avatar
+ 2
But 23.0 is still a small number that can be held within 32 bites right? Even though we explicitly declare it as float (float value=23.0), why will it become double?
29th Sep 2017, 6:20 PM
Priyanka P
Priyanka P - avatar
+ 2
Er. It's not that "it is taken as a double", it's that it IS a double. Animal b = new Animal(); Look at b. Here we assign a new Animal to b. But why would this not work: Animal b = 56.0; Well, it wouldn't work because 56.0 is not an Animal. So, what is 56.0?? Well, it is a double. By default decimal numbers are doubles. Every VALUE you write is a data-type. int a = "some word"; This doesn't work because "some word" is a String. This means you created a String with the value of "some word", then made the variable 'a' to be equal to that String. An int cannot be given the value of a String! So, this is an error. So, float a = 56.0; Is assigning the value of the double 56.0 to the float a. But 56.0 is a double! Floats cannot be given the values of doubles. So, it is an error. You need to do: float a = 56.0f; or float a = (float) 56.0;
1st Oct 2017, 4:32 PM
Rrestoring faith
Rrestoring faith - avatar
+ 1
So then what is the difference between: float x; double x;
30th Sep 2017, 7:10 PM
Priyanka P
Priyanka P - avatar
+ 1
One statement creates a variable of type float. The other of type double. floats and doubles are still 2 different data-types. They are just both used for decimal numbers. Doubles can hold more data than floats.
30th Sep 2017, 7:57 PM
Rrestoring faith
Rrestoring faith - avatar
+ 1
But then you said that 23.0 is a double, not float... (float value=23.0) Now you just said that float and double 2 are different data types. I am really sorry, but it is all so confusing..
30th Sep 2017, 8:02 PM
Priyanka P
Priyanka P - avatar
+ 1
So basically even a variable is declared as float, it is taken as double in java?
1st Oct 2017, 4:22 PM
Priyanka P
Priyanka P - avatar
+ 1
Thank you so much for helping me. I finally understand what you want to say. :) :) ***case closed***
1st Oct 2017, 4:52 PM
Priyanka P
Priyanka P - avatar