Multiplication bug in java? [duplicate] - java

This question already has answers here:
Why does Perl's sprintf not round floating point numbers correctly?
(2 answers)
Closed 8 years ago.
So, I made this lines of code to return 10% of the value in the parameter. For example, if the value being given is 7, the code should return 0.7. But it is returning 0.7000000000000001. Any thoughts why? The code is below:
public double calculateDiscount(double price){
double discount;
discount = price*0.10;
return discount;
}
I'm using eclipse IDE, and using the debug, I didn't find anything weird, the multiplication is simply returning that huge number.
PS.: I know that I can return only two decimal cases, but I want to know whats going on.
PPS.: Using /10 instead of *0.10 works just fine. But, as I said, I want to know why.

If you really care about precision, may be you want to use java.math.BigDecimal.
See more in How to Use Java BigDecimal: A Tutorial.

Related

How to set sth. on infinity in Java? [duplicate]

This question already has answers here:
How to implement infinity in Java?
(11 answers)
Closed 5 years ago.
I want to implement the dijkstra algorithm and have to set each note at the beginning to infinity.
I would like to know, if there is any function in Java which makes it easy.
Double.POSITIVE_INFINITY if you are using Double to store your data.
Also note that this is not a number, which is nice depending on what you want to do. Double supports this concept.

Factorial in Scientific notation [duplicate]

This question already has answers here:
Format double value in scientific notation
(4 answers)
Closed 6 years ago.
I want to show factorial of a number in scientific notation.
For example - 100! = "9.332621544e+157"
Is there any other way to do this besides NumberFormat? This is my code so far -
BigDecimal res=new BigDecimal("1");
for(int i=2;i<=x;i++){
res=res.multiply(BigDecimal.valueOf(i));
}
str=String.valueOf(res+"");
display.setText(str);
I'm really sorry if this question is vague to understand. Any help would be appreciated. Thanks in advance.
Not nice but changing
str=String.valueOf(res+"");
to
String str=res.doubleValue()+"";
will do the job
Using NumberFormat is the way to go but if you don't want to use that you could use a PrintStream
See Formatting Numeric Print Output for more info

why 0.3+0.3+0.3 = 0.899999999999999 in java? [duplicate]

This question already has answers here:
How to avoid floating point precision errors with floats or doubles in Java?
(12 answers)
Closed 8 years ago.
In my Java class.
My teacher has asked this question.
Can anyone help me out, I would appreciate it. TY.
why
0.3+0.3+0.3 = 0.899999999999999
in java ?????
Its because of floating point precision errors. The reason is that these data types are built for fast and accurate approximations and not for exact results. For that we use BigDecimal
For more info
Java Types
Java Float Types

Choose function in Java math? [duplicate]

This question already has answers here:
Combinatoric 'N choose R' in java math?
(17 answers)
Closed 8 years ago.
Sorry about the ambiguous title, I basically need to know how to use the choose function in java, to work out the chance of something happening, for example say I wanted to work out my chance of winning the lottery, on a calculator I'd do:
QUANTITY_OF_LOTTERY_NUMBERS CHOOSE 6
and that would return the chance of those six numbers appearing at random. I believe it's called the binomial theorem, but I dropped out of college before I could learn anything else about it :P
Anyway, if you do 49 choose 6 on Google for example, you'll see the function I'm trying to use
Question: How do I implement this function in Java code?
Apache Commons Math has it. If you are curious, you can read about Pascal's triangle.
http://commons.apache.org/proper/commons-math/apidocs/org/apache/commons/math3/util/CombinatoricsUtils.html#binomialCoefficient(int, int)

Why adding these two double does not give correct answer? [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Retain precision with Doubles in java
import static java.lang.System.out;
public class q2{
public static void main(String args[]){
double x=4.02, y=0.05;
out.println(x+y);
}
}
Output:
4.069999999999999
Why is it outputting the that. I thought it would be 4.07. Please explain why this happens in java ?
Sorry for the inaccurate Question title. I can't have a better title than this
That is because some numbers -- such as 0.1 -- cannot be represented exactly in binary floating-point.
Consider reading the following article:
What Every Computer Scientist Should Know About Floating-Point
Arithmetic
You are seeing a rounding error. See How to resolve a Java Rounding Double issue
To resolve it you can change to BigDecimal instead of double as mentioned in the accepted answer to the linked question.

Categories