Java Long Min and Max not exact as mentioned in docs [duplicate] - java

This question already has answers here:
Java Math.pow() Rounding Error
(4 answers)
Squaring Numbers in java using Math.pow getting error of precision
(5 answers)
why java Math.pow arguments double?
(1 answer)
Java always give me wrong result with huge numbers [duplicate]
(3 answers)
Closed 5 years ago.
As per java doc, the Long.MIN_VALUE is -2^63 and Long.MAX_VALUE is 2^63 - 1.
But Long.MIN_VALUE actual value is -2^63 - 1 and Long.MAX_VALUE value is 2^63 if I compute it like here:
long min = -(long) Math.pow(2, 63);
long max = (long) Math.pow(2, 63) - 1;
System.out.println(min);
System.out.println(max);
Over all the range between minimum and maximum value is the same but the actual values are not. Is my understanding of the above code wrong?
My bad, its the way I checked that lead to wrong values. The following is the simplest way I could think of to verify 2 power values in java.
long num = 1;
for(long count = 0; count < 63; count ++) {
num = num * 2;
}
System.out.println(num);
}

Math.pow's return value is double.
double can only exactly represent all integer values up to 2^53 - 1, and down to -2^53, because it only has 53 mantissa bits.
The error is simply in how you are attempting to check these values.

Related

Round up when multiplying an integer by 0.5 [duplicate]

This question already has answers here:
Always Round UP a Double
(8 answers)
Closed 7 years ago.
Is there a way to automatically round up a value in java?
For example:
//generate a random integer value
int randomVal = RandomHelper.nextIntFromTo(1, otherVal);
/* then divide the integer value in half... the error I am getting is that its a double, probably
because the number generated isn't an even number, but I NEED it to be an integer. Can I round up?*/
int value = randomVal * 0.5;
You can add 1 then divide by 2 instead of multiplying by 0.5. That way, you avoid a floating point operation followed by a conversion to int.
int value = (randomVal + 1) / 2;
Use Math.ceil():
int value = (int)Math.ceil(randomVal * 0.5);

Get the next higher integer value in java [duplicate]

This question already has answers here:
How to round up the result of integer division?
(18 answers)
How to round up integer division and have int result in Java? [duplicate]
(9 answers)
Closed 4 years ago.
I know I can use Math.java functions to get the floor, ceil or round value for a double or float, but my question is -- Is it possible to always get the higher integer value if a decimal point comes in my value
For example
int chunkSize = 91 / 8 ;
which will be equal to 11.375
If I apply floor, ceil or round to this number it will return 11 I want 12.
Simply If I have 11.xxx I need to get 12 , if I have 50.xxx I want 51
Sorry The chunkSize should be int
How can I achieve this?
Math.ceil() will do the work.
But, your assumption that 91 / 8 is 11.375 is wrong.
In java, integer division returns the integer value of the division (11 in your case).
In order to get the float value, you need to cast (or add .0) to one of the arguments:
float chunkSize = 91 / 8.0 ;
Math.ceil(chunkSize); // will return 12!
ceil is supposed to do just that. I quote:
Returns the smallest (closest to negative infinity) double value that
is greater than or equal to the argument and is equal to a
mathematical integer.
EDIT: (thanks to Peter Lawrey): taking another look at your code you have another problem. You store the result of integer division in a float variable: float chunkSize = 91 / 8 ; java looks at the arguments of the division and as they are both integers it performs integer division thus the result is again an integer (the result of the division rounded down). Now even if you assign this to the float chunkSize it will still be an integer(missing the double part) and ceil, round and floor will all return the same value. To avoid that add a .0 to 91: float chunkSize = 91.0 / 8;. This makes one of the arguments double precision and thus double division will be performed, returning a double result.
If you want to do integer division rounding up you can do
x / y rounded up, assuming x and y are positive
long div = (x + y - 1) / y;
In your case
(91 + 8 - 1) / 8 = 98 / 8 = 12
This works because you want to round up (add one) for every value except an exact multiple (which is why you add y - 1)
Firstly, when you write float chunkSize = 91 / 8 ; it will print 11.0 instead of 11.375
because both 91 and 8 are int type values. For the result to be decimal value, either dividend or divisor
or both have to be decimal value type. So, float chunkSize = 91.0 / 8; will result 11.375 and
now you can apply Math.ceil() to get the upper value. Math.ceil(chunkSize); will result in 12.0

Why does java return a 0? [duplicate]

This question already has answers here:
Why does the division of two integers return 0.0 in Java? [duplicate]
(6 answers)
Double value returns 0 [duplicate]
(3 answers)
Closed 9 years ago.
Why does Java return a 0 when I divide 10/60?
the code I tried is
double pay_per_minute = (10/60);
10 being the pay rate per hour and 60 being the minutes.
Because you're building an integer. When you store it in a double variable, it's already too late : it's 0.
Do
double pay_per_minute = (10.0/60);
If you have variables, cast them :
double pay_per_minute = ((double)pay_per_hour) / 60;
any primitive digit in java is treated as an integer, so when you do 10/60, it is integer division and due to precision loss it is giving 0
Here 10 and 60 takes as int values and then you get int dividing result it is 0 then you get answer as 0. use following way.
double a=10;
double b=60;
double div=a/b;
you need to type cast it first because by default numericals are considered as integers
double pay_per_minute = ((double)10/60);
System.out.println(pay_per_minute);
output 0.16666666666666666
double pay_per_minute = (10/60);
Here, you are dividing integer 10 by integer 60. So, this is like doing
int temp = 10/60;
double pay_per_minute = double(temp)
your temp will be 0 (since 10/60 is 0 when considered as integer division)
You need to do,
double pay_per_minute = (10.0/60);

Dividing long by long returns 0 [duplicate]

This question already has answers here:
Is "long x = 1/2" equal to 1 or 0, and why? [duplicate]
(6 answers)
Closed 9 years ago.
I am trying to calculate % of used diskspace in Windows and totaldrive denotes total diskspace of c drive in Long and freedrive dentoes free space in Long.
long totaloccupied = totaldrive - freedrive;
Here calculating % of usage
Long Percentageused =(totaloccupied/totaldrive*100);
System.out.println(Percentageused);
The print statement returns 0. Can someone help as I am not getting the desired value
You are probably dividing a long with a long, which refers to (long/long = long) operation, giving a long result (in your case 0).
You can achieve the same thing by casting either operand of the division to a float type.
Long Percentageused = (long)((float)totaloccupied/totaldrive*100);
You are doing integer division! Since totaloccupied is smaller than totaldrive, the division of both gives the answer 0. You should convert to double first:
double percentageUsed = 100.0 * totalOccupied / totalDrive;
Note that adding the decimal point to the 100 ensures it is treated as a double.
That will be evaluated left to right, the first integer division will return 0 (e.g. 8/10 evaluates to 0). Either convert values to floats or do 100*a/b. Floats will give you a more precise result.

Java percent of number [duplicate]

This question already has answers here:
Int division: Why is the result of 1/3 == 0?
(19 answers)
Closed 4 years ago.
Is there any way to calculate (for example) 50% of 120?
I tried:
int k = (int)(120 / 100)*50;
But it doesn't work.
int k = (int)(120 / 100)*50;
The above does not work because you are performing an
integer division expression (120 / 100) which result is
integer 1, and then multiplying that result to 50, giving
the final result of 50.
If you want to calculate 50% of 120, use:
int k = (int)(120*(50.0f/100.0f));
more generally:
int k = (int)(value*(percentage/100.0f));
int k = (int)(120*50.0/100.0);
Never use floating point primitive types if you want exact numbers and consistent results, instead use BigDecimal.
The problem with your code is that result of (120/100) is 1, since 120/100=1.2 in reality, but as per java, int/int is always an int.
To solve your question for now, cast either value to a float or double and cast result back to int.
I suggest using BigDecimal, rather than float or double. Division by 100 is always exact in BigDecimal, but can cause rounding error in float or double.
That means that, for example, using BigDecimal 50% of x plus 30% of x plus 20% of x will always sum to exactly x.
it is simple as 2 * 2 = 4 :)
int k = (int)(50 * 120) / 100;
Division must be float, not int
(120f * 50 / 100f)
You don't need floating point in this case you can write
int i = 120 * 50 / 100;
or
int i = 120 / 2;

Categories

Resources