David StJohn

Greenhorn
+ Follow
since Dec 22, 2000
Cows and Likes
Cows
Total received
0
In last 30 days
0
Total given
0
Likes
Total received
0
Received in last 30 days
0
Total given
0
Given in last 30 days
0
Forums and Threads
Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by David StJohn

Does anyone have a book, website, or something else that could help me learn more about date calculations? Specifically adding and subtracting dates to find out time elapsed. For example, I want to take today's date, take a date six months ago and then calculate and display the number of days that has occurred between the two dates.
Any help would be greatly appreciated.
Thanks
21 years ago
I'm still confused. I changed the code to add ten individual float integers and I still got erronous values. How would you add float integers together and expect to get a valid value. Here is the new code and the result. public class Sum2
{
public static void main( String[] args )
{
float sum = 0F;
float i = 1.1F;
float j = 1.1F;
float k = 1.1F;
float l = 1.1F;
float m = 1.1F;
float n = 1.1F;
float o = 1.1F;
float p = 1.1F;
float q = 1.1F;
float r = 1.1F;

sum += i;
System.out.println("The total " + sum + " " + i);
sum += j;
System.out.println("The total " + sum + " " + i);
sum += k;
System.out.println("The total " + sum + " " + i);
sum += l;
System.out.println("The total " + sum + " " + i);
sum += m;
System.out.println("The total " + sum + " " + i);
sum += n;
System.out.println("The total " + sum + " " + i);
sum += o;
System.out.println("The total " + sum + " " + i);
sum += p;
System.out.println("The total " + sum + " " + i);
sum += q;
System.out.println("The total " + sum + " " + i);
sum += r;
System.out.println("The total " + sum + " " + i);

if (sum == 100)
{
System.out.println("The total " + sum + " is equal to 100 " + i);
}
else
{
System.out.println("The total " + sum + " does not equal 100 " + i);
}
}
}
And the result was
C:\Java>java Sum2
The total 1.1 1.1
The total 2.2 1.1
The total 3.3000002 1.1
The total 4.4 1.1
The total 5.5 1.1
The total 6.6 1.1
The total 7.7 1.1
The total 8.8 1.1
The total 9.900001 1.1
The total 11.000001 1.1
The total 11.000001 does not equal 100 1.1
It still doesn't make sense. I understand about formating the output. I just don't understand why this works like this on what I consider a simple calculation.

21 years ago
Hi, I'm just learning Java and have run into a problem I can't figure out. I haven�t found it in any of the books I am reading. This problem was posted on the Java Ranch Cattle Drive. The problem was to total the amount of 0.1 1000 times and compare the total to 100. Pretty simple problem, or so I thought. I coded the following and then got the result at the end. I reduced the total number of summing to 10 for display purposes only.
import javax.swing.*;
public class Sum
{
public static void main( String[] args )
{
double sum = 0;
double i = 0.1;
for (int x = 0 ; x < 10 ; x ++)<br /> { <br /> sum += i; <br /> System.out.println("The total " + sum + " " + i);<br /> }<br /> if (sum == 100) <br /> {<br /> System.out.println("The total " + sum + " is equal to 100 " + i); <br /> }<br /> else <br /> {<br /> System.out.println("The total " + sum + " does not equal 100 " + i); <br /> }<br /> }<br /> }<br /> Produces this result. <br /> C:\Java>java Sum
The total 0.1 0.1
The total 0.2 0.1
The total 0.30000000000000004 0.1
The total 0.4 0.1
The total 0.5 0.1
The total 0.6 0.1
The total 0.7 0.1
The total 0.7999999999999999 0.1
The total 0.8999999999999999 0.1
The total 0.9999999999999999 0.1
The total 0.9999999999999999 does not equal 100 0.1
What is going on here? Why does this occur? The first number is the value of sum and the second number is the value of �i�. I�ve tried it with float numbers and got the following results.
C:\Java>java Sum
The total 0.1 0.1
The total 0.2 0.1
The total 0.3 0.1
The total 0.4 0.1
The total 0.5 0.1
The total 0.6 0.1
The total 0.70000005 0.1
The total 0.8000001 0.1
The total 0.9000001 0.1
The total 1.0000001 0.1
The total 1.0000001 does not equal 100 0.1
Any help would be greatly appreciated.
Thanks
David
21 years ago