precision in calculation with long
in
Programming Questions
•
1 year ago
I want to map a LocalDate (joda time).
I made this which works fine.
- public float mapLocalDate(LocalDate value, LocalDate low1, LocalDate high1, float low2, float high2) {
- // value probably must be within range of low1 and high1
- float h1 = Days.daysBetween(low1, high1).getDays();
- float v = Days.daysBetween(low1, value).getDays();
- return map(v, 0, h1, low2, high2);
- }
I only don't know what happens if a value is out of the range of low1 and high1.
Also normally with map you can switch low1 with high1 for example.
In this case it that won't work (daysBetween is a positive value).
I thought the easiest way is to get the millis() and work with that:
- long val = value.toDateTimeAtStartOfDay(DateTimeZone.UTC).getMillis();
- long istart = low1.toDateTimeAtStartOfDay(DateTimeZone.UTC).getMillis();
- long istop = high1.toDateTimeAtStartOfDay(DateTimeZone.UTC).getMillis();
- return low2 + (high2 - low2) * ((val - istart) / (istop - istart));
This gives very bad results, why is that?
1