This question already has answers here:
Division of integers in Java [duplicate]
(7 answers)
Closed 7 years ago.
if 123/33 prints out 3 and 3 is an integer if we cast it to float ( (float)123/33 )how do we get the decimal places from the integer 3. does 3 contains floating points internally what ?
public static void test() {
System.out.println("==========");
System.out.println(123/33); // prints 3
System.out.println((float)123/33); // prints 3.7272727
//so if we cast it to float we get the decimal points also (.7272727)
}
The cast does not apply to the entire expression 123/33. You're casting the value 123 to a float, which causes any further operations to use floating-point math.
123 will be casted(converted) to float and then the division happens, so the result will be float value.
System.out.println("==========");
System.out.println(123/33); // prints 3
System.out.println((float)123); // prints 123.0
System.out.println((float)123/33); // prints 3.7272727
Related
This question already has answers here:
Integer division: How do you produce a double?
(11 answers)
Closed 2 years ago.
I'm trying to get a number when I divide 2 numbers together:
int wins = 3070;
int n = 10000;
double probability = wins/n;
System.out.println(probability);
All it prints is: 0.0
But I'm expecting it to print: 0.307
Atleast one of the values (numerator or denominator) should be type casted with double. Integer divided by integer would result integer. If one of them would be double then result would be upcasted to double. Try it! It should work!
This question already has answers here:
Java division by zero doesnt throw an ArithmeticException - why?
(8 answers)
Double divide by zero: Why is the result inconsistent?
(3 answers)
why there is no ArithmeticException( divide by zero) when both values are double? [duplicate]
(3 answers)
Why is number divided by zero infinity in Java?
(5 answers)
Why does integer division by zero 1/0 give error but floating point 1/0.0 returns "Inf"?
(5 answers)
Closed 3 years ago.
I am trying to run below program and expecting Arithmetic Exception because we are dividing double value by zero but getting Infinity as output.
This is below program. But if we are using int instead of double then getting Arithmetic Exception
public static void main(String z[]) {
Double a = 10.0;
Double b = 0.0;
System.out.println(a/b);
}
Output: Infinity
public static void main(String z[]) {
int a = 10;
int b = 0;
System.out.println(a/b);
}
Output:
Exception in thread "main" java.lang.ArithmeticException: / by zero
at (Test.java:94)
Can someone please explain why we are getting Infinity for Double
value?
Refer to the language spec:
if the value of the divisor in an integer division is 0, then an ArithmeticException is thrown.
...
The result of a floating-point division is determined by the rules of IEEE 754 arithmetic:
Division of a nonzero finite value by a zero results in a signed infinity.
Floating point division can have a non-exceptional result because there is a special value to indicate it. There is no such special value for integer division, so returning any integer result would be incorrect; the only option is to throw an exception.
In IEEE 754 floating point operations, dividing a value by 0 (resp. 0.0) is well-defined and results in an "infinite" value, either positive or negative, depending on the involved values.
In integer domain, no such thing is defined and thus you get an ArithmeticException.
This question already has answers here:
Why does division by zero with floating point (or double precision) numbers not throw java.lang.ArithmeticException: / by zero in Java
(6 answers)
Closed 7 years ago.
Why is it that division of an integer by 0 gives ArithmeticException whereas division of a non-zero double or float by 0,prints Infinity.
Also division of an int 0 by 0 gives ArithmeticException whereas division of a double or float 0 by 0 gives NaN(Not a number).
public class First
{
public static void main(String[] args)
{
System.out.println(10/0); //Arithmetic Exception
System.out.println(10.0/0); //Prints Infinity
}
}
java follows IEEE floating point standards/practices, and those incorporate infinity [+ and -] as a value that can be calculated with, and NaN as a representation for results that are not a number.
(And obviously, those floating point standards do not have a counterpart for integer arithmetic.)
Division by a floating point 0.0 yields NaN or +/-Inf.
Division by an integer 0 is not covered by IEEE 754, and generates an exception - (i.e because an int can't represent NaN or Inf).
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Why does (360 / 24) / 60 = 0 … in Java
Having problems with floats and longs in Java
float f = 0.100f;
f = 3/180;
At the minute i am trying to do something like this with object and their attributes, but even to this simplest form my program returns 0.0.
I have tried this with Longs as well as still the same result. It's been a long day and maybe it's something simple but I'm at a brick wall.
Your expression 3/180 is performing an integer division, and it is then casting that into the float f. In integer division, 3/180 will return 0, and this is what you are seeing.
What you probably want to do is just add a decimal point to your numbers: f = 3.0/180.0;
3/180 is integer division.
Therefore, the result is truncated to an integer.
You need to perform floating-point division: 3/180f
You do an integer division. So you get an int which is casted back to a float. Try this:
f = 3/180.0;
or
f = 3/180f;
Try 3.0/180. Otherwise, you are dividing two integers and you run into integer truncation. When you do integer division the result is also an integer, not a floating point number.
This question already has answers here:
Integer division: How do you produce a double?
(11 answers)
Closed 7 years ago.
Here's the code:
class testsum
{
public static void main(String arg[])
{
double sum=0;
double fraction;
fraction=-1/9;
System.out.println("fraction: "+fraction);
fraction=-1;
fraction=fraction/9;
System.out.println("fraction: "+fraction);
}
}
the outputs are 0 and then -0.11111111
why was the first output 0 and not -0.11111111111?
It's doing integer division in the first example as this is the default type for a numeric literal. Try changing it to -1.0/9 (or 1d/9d - the d suffix indicates a double) and you should get the same answer.
1 and 9 are both integer. Try
1.0/9
This is why it works for fraction/9, since fraction is a double.
When you do -1/9, it says "-1, that's an int. 9, that's an int. -1 / 9 in integer division is 0. Oh, now I need to cast to double."
Changing it to -1.0 / 9 should solve the problem.
Try wrapping the "-1/9" in brackets.
The first one is 0 because it is doing integer division. -1 and 9 and integers and when divided equal 0. The result is then converted into a double so it can be stored in fraction. The easiest solution is this:
fraction = -1.0/9;
Because -1 and 9 are integers, so -1/9 is an integer division (with the result 0, which when cast to double is 0.0).
To do a floating point division, you should convert one of the numbers to double, (double) 9, 9d or simply 9.0.
In the latter case, fraction is already double (-1.0) so fraction/9 is a floating point division.