Java:
float big = (float) 1e12;
float ulp = Math.ulp(big);
float result = (big + 2/3*ulp) - big;
result is 0.0, while I was expecting ulp (65536.0). Can somebody explain why?
This is because 2/3 = 0 (integer division), try 2.0 / 3
You may try this:
float result = (big + 2f/3*ulp) - big;
ie, you need to typecast the division values else integer/integer will result to zero.
Try typecasting either 2 or 3 to float and try, as 2/3=0, thats why result=0.0
Related
I have this strange issue of having to cap the precision of double numbers to a number of decimal places, or no decimal places at all, where the precision is handed to me like so: 0.001, 0.1, 1, 0.00001, etc.
So I could be given, for example, 1.234247324 and a precision indicator of 0.001, and with that I would need to return 1.234. If Instead I had been handed a precision of 1.0, I would then return 1 (no decimal places), and so forth.
I'm not sure how to go about this. Does anyone has pointers on how to tackle this?
Thank you!
Start with your value, and create a BigDecimal, then round to what you want.
double value = 1.23456; //not actually equal to that, but pretend.
BigDecimal better = new BigDecimal(value);
BigDecimal rounded = better.round(new MathContext(3));
Rounded is now the actual value you want. What can you do with the precision indicator?
BigDecimal precision = new BigDecimal("0.001");
Now you can get the scale of the precision, which you can use for rounding.
System.out.println(precision.scale());
//outputs 3.
That would be similar to using the logarithm.
Use a hybrid approach of the two other answers allows unusual precisions like 0.25 to be specified:
BigDecimal rounded = (x / precision).round(new MathContext(0)) * precision;
Try this code
double x = 1.234247324;
double p = 0.001;
double y = (int)(x * 1 / p) / (1 / p);
Don't really understand what you want to do but this results 1.234.
I have two values
5042034.0
1425837.2
while I am adding like below
float abc = (float) (5042034.0 + 1425837.2);
I am expecting a result of 6467871.2
But I am getting 6467871.0
How could I get the 6467871.2 with the help of float?
I tried with
float c = (float) (1.1 + 2.2) ;
I got a result : 3.3
What is the reason behind this?
Floats are IEEE 754 32-bit numbers.
5042034.0 is over 2^22. This means, that it fills 23 bits of mantisa which is the maximum. It actually skips the trailing 0.
When you're trying to add it to 1425837.2 it adjusts both numbers:
10011001110111101110010.00
+ 101011100000110101101.0011001100110011001101....
--------------------------
11000101011000100011111.0
in binary system. It means that .0 and .2 are out of 22 bit and are skipped.
If you want your arithmetic to be better, use double or BigDecimal instead of float:
double result = 5042034.0d + 1425837.2d;
BigDecimal bd = BigDecimal.valueOf(5042034.0d + 1425837.2d);
Try double instead.
double abc = (double) (5042034.0 + 1425837.2);
To support #xenteros answer, use BigDecimal.
BigDecimal abc = BigDecimal.valueOf(5042034.0 + 1425837.2);
System.out.println(abc);
will result in 6467871.2.
Better still, use double which is a primitive:
double abc = (double) (5042034.0 + 1425837.2);
System.out.println(abc);
N.B: Just thought to contribute this, I know the OP asked with the help of float.
Java uses IEEE 754 standard which support 6-7 significant decimal digits. Your addition result cross the range limit so you are getting such result.
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;
I think its pretty self explanatory from the code. Obviously I'm no evaluating the same thing over and over, its just example numbers to explain my problem. I'm guessing its over/underflow but I don't know how to deal with it.
double d = (1 / (684985+157781));
System.out.println(d); // returns 0.0
System.out.println(Math.log(d)); // returns -Infinity.
(1 / (684985+157781)) is an integer expression, so it will come out to 0.
The zero then gets assigned to the double d, as 0.0.
Try changing the 1 to 1.0 to force that expression to be a float, or 1.0D to force it to double.
Another person done in by integer division:
double d = (1.0 / (684985.0+157781.0));
No, in Java if you use integers, the result of division would be again integer, you have to cast least one operand to double.
double d = (1 / (double)(684985+157781));
Try using double d = (1.0 / (684985+157781));
Note the 1.0 part: you want to force the double evaluation.
That first expression is computed in integer arithmetic. To get the answer you're expecting, you need to compute it in floating-point arithmetic, thus:
double d = (1.0 / (684985.0+157781.0));
This question already has answers here:
Int division: Why is the result of 1/3 == 0?
(19 answers)
Closed last year.
I have a very simple division in Java (it's a product quantity / production per hour), however whenever I make this division I get strange errors:
float res = quantity / standard;
I have tried the above division with several values and I always get errors, however the one that I've tried everywhere else and gotten right was this:
Everywhere in the world:
13.6 = 6800 / 500;
Java:
13.0 = 6800 / 500;
I've researched BigDecimal and BigInteger, however I haven't found a way to create this division with them, is there any other way to do this division in Java without having precision errors??
Any help will be greatly appreciated.
You're dividing integers, which means that you're using integer division.
In integer division the fractional part of the result is thrown away.
Try the following:
float res = (float) quantity / standard;
^^^^^^^
The above forces the numerator to be treated as a float which in turn promotes the denominator to float as well, and a float-division is performed instead of an int-division.
Note that if you're dealing with literals, you can change
float f = 6800 / 500;
to include the f suffix to make the denominator a float:
float f = 6800f / 500;
^
If you concerned about precision I would suggest using double which has more than double the number of digits of precision. However floating point only accurately represents fractions which are a sum or powers of 0.5. This means 0.6 is only approximately represented. This doesn't have to be a problem with appropriate rounding.
double d = (double) 6800 / 500;
or
double d = 6800.0 / 500;
In my case I was doing this:
double a = (double) (MAX_BANDWIDTH_SHARED_MB/(qCount+1));
Instead of the "correct" :
double a = (double)MAX_BANDWIDTH_SHARED_MB/(qCount+1);
hi try this one it may help ful your requirement
double percent=(7819140000l-3805200000l)*100f/7819140000l;
public String format_Decimal(double decimalNumber) {
NumberFormat nf = NumberFormat.getInstance();
nf.setMaximumFractionDigits(5);
nf.setMinimumFractionDigits(2);
nf.setRoundingMode(RoundingMode.HALF_UP);
String x = nf.format(decimalNumber);
return x;
}