Division between integers in Java - java

I need to perform division between integers in Java, and the result should be a float.
Could I just use / symbol for it? As in:
int integer1 = 1;
int integer2 = 2;
float quotient = integer1 / integer2; // Could I do this?

Cast one of the integers to float to ensure a floating point division:
float result = integer1 / (float) integer2

Related

How to generate a random double number in Java? (not necessarily in the range [0,1]) [duplicate]

Anyone got an idea of how to achieve this. I've tried the usual formula but I'm only getting positive numbers <= 10:
Double.MIN_VALUE + Math.random() * ((Double.MAX_VALUE - Double.MIN_VALUE) + 1)
You could do this
private static final Random rand = new Random();
public static double getRandomDouble() {
while(true) {
double d = Double.longBitsToDouble(rand.nextLong());
if (d < Double.POSITIVE_INFINITY && d > Double.NEGATIVE_INFINITY)
return d;
}
}
This will return any finite double with equal probability.
You can't just the the formula above as the (Double.MAX_VALUE - (-Double.MAX_VALUE)) overflows to infinity. i.e. the range for all positive and negative double values is too large to store in a double.
double d = Math.random() * Double.MAX_VALUE;
return Math.random() < 0.5 ? d : 0-d;

integer division to float result [duplicate]

This question already has answers here:
Fahrenheit to Celsius conversion
(3 answers)
Closed 7 years ago.
I have to divide two integers and get a float as result
My Code:
Float ws;
int i = Client.getInstance().getUser().getGespielteSpiele() -Client.getInstance().getUser().getGewonneneSpiele();
int zahl1 = Client.getInstance().getUser().getGewonneneSpiele();
ws = new Float((zahl1 / i));
I check the values with the debugger
i = 64
zahl1 = 22
ws = 0.0
Why is the result of ws 0.0? What I should do to get the correct float?
When you divide two ints you perform integer division, which, in this case will result in 22/64 = 0. Only once this is done are you creating a float. And the float representation of 0 is 0.0. If you want to perform floating point division, you should cast before dividing:
ws = ((float) zahl1) / i;
zahl1 / i is computed as int division and then converted to Float by the Float constructor. For floating point division, cast one of the operands to float or double:
ws = new Float((float)zahl1 / i);
It's because you're doing integer division,you need to change the int value as float at first.
float ws = (float) zahl1 / i;
try:
float ws = (float)zahl1/i;

JAVA Round to closest number

How to round a decimal number to closest "whole" number?
You can try using
float x = (float)Math.ceil(x);
or
float y = (float)Math.round(y);
Also note that I have converted them back to float as there may be a loss of precision when you are converting double to float.
One line answer!
public static long roundToClosestLong(long num, long div) {
return (long) num / div + ((double) (num % div) / div < 0.5 ? 0 : 1);
}
You can use java.lang.Math class for that.
Math#round(double) Will accecpt double as argument and returns long
Math#round(float) Will accecpt float as argument and returns int
You could do:
int result = (int) (yourValue + 0.5);
if you just need simple rounding this is maybe a more performant solution than Math.round(yourValue)

Error in initializing a float value

Why this is not allowed?
float n;
n = 1234567.89;
but this is.
float n;
n = (float) 12.3456789;
I'm using JAVA 8 and Netbeans 8.0.1.
IDE snapshot below.
By default, Java interprets literal decimals as double, so to enter a float you need to do one of the following:
float n = 1234567.89f;
float n = (float) 1234567.89;
Note that doing so may result in a loss of precision.
java treat it as double in first line. n = 1234567.89 not as float. double is 8 byte and float is 4 byte.
solution is
float n;
n = 1234567.89f;
Use
float n;
n = 1234567.89f;
This declares your number as being a float value instead of a double value by default.
When you define a decimal number as 1234567.89, its interpreted as a double.
Float numbers end with a letter f. You can define it as below.
float n;
n = 1234567.89f;
Float is a 32 bit IEEE 754 floating point. Double is a 64 bit IEEE 754 floating point.

Java ((4-2) / 4) = 0.0 not 0.5 after casting as double

This method has myNumSides equal to 4, myNumSides is a user-inputted. After myAngle being casted as a double it still returns 0.0 instead of 0.5. Why is this?
public double getMyAngle()
{
int n;
n = myNumSides;
double myAngle = (double) ((n - 2) / n);
return myAngle;
}
Output
Please enter number of sides => 4
Please enter length of each side => 100
Your polygon has 4 sides.
Each side of your polygon has a length of 100.0.
The angle of each vertex is 0.0.
In order to get the correct answer, you would have to do the following:
double myAngle = ((n - 2.0) / n);
OR
double myAngle = ((n - 2) / (double)n);
When java executes double myAngle = (double) ((n - 2) / n);, It will first do the division operation and then do the casting operation. In the division operation you are dividing an Integer by an another Integer. So the result of this division also will be an Integer. In your case case, 2/4 = 0.
Correct code would be do the casting first and then division.
double myAngle = ((double)(n - 2) / n);
If you cast a zero to a double, it's still a zero. What you do with the result has no effect on how it's computed.
You have already performed the math as an int, you then cast that int to a double. This widens the value but does not restore the lost precision to the previously computed value, I suggest you just make n a double like
public double getMyAngle() {
double n = myNumSides;
return ((n - 2) / n);
}
You can change your code to:
public double getMyAngle()
{
int n;
n = myNumSides;
double myAngle = (double)(n - 2)/n;
return myAngle;
}
==================
then the answer should be correct!

Categories