integer division to float result [duplicate] - java

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;

Related

Java pow without rounding up the final value [duplicate]

This question already has answers here:
Integer division: How do you produce a double?
(11 answers)
Closed 3 years ago.
I am trying to write a formula for one of my functions where I need to raise my X to power of Y. The values I am working with are really small and will get rounded up as soon as I use pow function of Math, BigInteger and BigDecimal.
For example if I use the following values it will return 1000 whereas it should return 1006.931669!
T0 = 1000, TN = 1, k = 1, N = 1000
double X = (finalTemp/initialTemp);
double A = Math.pow(X, (k/n));
double tk = initialTemp * A;
They are being divided in integer arithmetics. So dividing integer a by integer b you get how many times b fits into a, If the types of the operands are double, then "real" division is performed.
double X = (finalTemp/(double)initialTemp);
double A = Math.pow(X, (k/(double)n));
double tk = initialTemp * A;
the output is correct according to calculator
public class Main
{
public static void main(String[] args) {
double finalTemp = 1.0;
double initialTemp = 1000.0;
double k = 1.0;
double n = 1000.0;
double X = (finalTemp/initialTemp);
double A = Math.pow(X, (k/n));
double tk = initialTemp * A;
System.out.println(tk);
}
}
output is 993.1160484209338

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.

How to round double to nearest whole number and return it as Integer

Let suppose that I have double x. I would return nearest whole number of x. For example:
if x = 6.001 I would return 6
if x = 5.999 I would return 6
I suppose that I should use Math.ceil and Math.floor functions. But I don't know how return nearest whole number...
For your example, it seems that you want to use Math.rint(). It will return the closest integer value given a double.
int valueX = (int) Math.rint(x);
int valueY = (int) Math.rint(y);
public static void main(String[] args) {
double x = 6.001;
double y = 5.999;
System.out.println(Math.round(x)); //outputs 6
System.out.println(Math.round(y)); //outputs 6
}
The simplest method you get taught in most basic computer science classes is probably to add 0.5 (or subtract it, if your double is below 0) and simply cast it to int.
// for the simple case
double someDouble = 6.0001;
int someInt = (int) (someDouble + 0.5);
// negative case
double negativeDouble = -5.6;
int negativeInt = (int) (negativeDouble - 0.5);
// general case
double unknownDouble = (Math.random() - 0.5) * 10;
int unknownInt = (int) (unknownDouble + (unknownDouble < 0? -0.5 : 0.5));
int a = (int) Math.round(doubleVar);
This will round it and cast it to an int.
System.out.print(Math.round(totalCost));
Simple
Math.round() method in Java returns the closed int or long as per the argument
Math.round(0.48) = 0
Math.round(85.6) = 86
Similarly,
Math.ceil gives the smallest integer as per the argument.
Math.round(0.48) = 0
Math.round(85.6) = 85
Math.floor gives the largest integer as per the argument.
Math.round(0.48) = 1
Math.round(85.6) = 86

Division between integers in 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

Categories