Home
Help
Search
Login
Register
Welcome,
Guest
. Please
login
or
register
.
Did you miss your
activation email?
February 22, 2012, 09:51:41 PM
MOON JAVA
>
Java
>
Java
>
weird java problem/question?
Pages: [
1
]
« previous
next »
Print
Author
Topic: weird java problem/question? (Read 193 times)
Jonathan_Nagel
Newbie
Karma: +0/-0
Posts: 9
weird java problem/question?
«
on:
October 21, 2011, 08:37:22 PM »
i\'m no noob when it come to programming but this is the first time ive run into this issue. I w2as testing a program that is incrementing by a certain value to gather intervals for simpson\'s rule. When the values are not of 1 / 2^x increments it gets weird
simplified version of the code with .2 as the increment:
public class test{
public static void main(String[] args) {
double a = 1, b = 5, h = 0.2;
double temp = a;
while (temp <= b){
System.out.println(temp);
temp += h;
}
}
}
the output:
1.0
1.2
1.4
1.5999999999999999
1.7999999999999998
1.9999999999999998
2.1999999999999997
2.4
2.6
2.8000000000000003
3.0000000000000004
3.2000000000000006
3.400000000000001
3.600000000000001
3.800000000000001
4.000000000000001
4.200000000000001
4.400000000000001
4.600000000000001
4.800000000000002
can anyone explain why this is happening, i have used both jGrasp and Eclipse and both have given me this, so its not an issue of IDE
EDIT: i do know the whole number limitation thing, 0.20 is not like .3333, nor is .10 so i dont see why it would happen this way
Logged
MOON JAVA
weird java problem/question?
«
on:
October 21, 2011, 08:37:22 PM »
Logged
larrybud2004
Newbie
Karma: +0/-0
Posts: 3
Re: weird java problem/question?
«
Reply #1 on:
November 05, 2011, 04:25:39 PM »
What happens is that floating point numbers (your double) has a certain precision, and that precision carries over from one iteration to another.
For example, if you divide 1 by 3, we all know it\'s 0.3333 and the 3 repeats forever.
In computers, though, it doesn\'t repeat forever. Double and floats are represented at binary fractions, so they aren\'t exact values.
It\'s not just in java, it\'s computers in general. You can get similar issues in simple spreadsheets in Excel, for example.
Logged
MOON JAVA
Re: weird java problem/question?
«
Reply #1 on:
November 05, 2011, 04:25:39 PM »
Logged
Pages: [
1
]
Print
« previous
next »
Jump to:
Please select a destination:
-----------------------------
Expert Shares
-----------------------------
=> Expert Shares
-----------------------------
EJB
-----------------------------
=> EJB
-----------------------------
Java
-----------------------------
=> Java
-----------------------------
J2EE
-----------------------------
=> J2EE
-----------------------------
Spring
-----------------------------
=> Spring
-----------------------------
Struts
-----------------------------
=> Struts
-----------------------------
J2ME
-----------------------------
=> J2ME
-----------------------------
Tomcat
-----------------------------
=> Tomcat
-----------------------------
JavaFX
-----------------------------
=> JavaFX
-----------------------------
JSTL
-----------------------------
=> JSTL
-----------------------------
Junit
-----------------------------
=> Junit
-----------------------------
Groovy
-----------------------------
=> Groovy
-----------------------------
JDBC
-----------------------------
=> JDBC
-----------------------------
JAVA JOBS (powered by job-adviser.com)
-----------------------------
=> JAVA JOBS