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

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

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

how to store "12000000000" or a 11 digit number in java [duplicate]

This question already has answers here:
Large Numbers in Java
(6 answers)
Closed 6 years ago.
given here only as a form of validating.Which data type to use to store such a big number
Probably what you're looking for is BigInteger, BigDecimal if you want decimals instead.

Round Off Error? - Java [duplicate]

This question already has answers here:
Is floating point math broken?
(31 answers)
Floating point multiplication in java [duplicate]
(3 answers)
Closed 8 years ago.
Ok, so i made a scientific calculator recently and one of my friends was using it when he said that if he types in (2.1 * 3) the answer should be 6.3 but he recieves something like: 6.30000001.
Why does this happen?
Is this some sort of bug in java?
EDIT: Its not just a problem with my calc, i also made a small java file for multiplying 2 numbers and i face the same probelem even in cmd

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