Question about Java math operations on float
and int
data types.
I have to calculate the date difference in years between two dates.
The code:
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
Object date = obj.getInfoItem().getInfoValue();
Date today = new Date();
Date birthDate = null;
float dateDiff;
int age0, age1;
try {
// unify the date format
birthDate = format.parse(date.toString());
today = format.parse(format.format(today));
} catch (ParseException e) {
e.printStackTrace();
}
// calculate the age in years with round
dateDiff = today.getTime() - birthDate.getTime();
age0 = (int)((dateDiff / (24 * 60 * 60 * 1000)) / 365);
age1 = (int)(dateDiff / (365 * 24 * 60 * 60 * 1000));
Since the date difference in Java is calculated in milliseconds, thus we have to do some housekeeping work after calculation and convert received result from milliseconds to years.
After code execution, I got the following results in debugger:
dateDiff = 8.4896639E11
age0 = 26
age1 = 577
age0
is a correct result.
Since both operations on age0
and age1
are mathematically equal, why outcomes are different?
Why there is a difference between operations «(float / (a\*b\*c)) / d
» and «(float / (a\*b\*c\*d))
», where a
, b
, c
, d
are int
.