question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Time equality comparisons are possibly too precise

See original GitHub issue

In astro_metadata_translator I’ve been adding some tests to make sure that the end date of an observation comes after the start date of an observation. Sometimes DATE-END header is missing so the end date is derived by adding the exposure time to the start date. This is fine until biases are included which have 0.0 second exposures, whereupon the end date is now in the past.

 $ python
Python 3.7.3 (default, Mar 27 2019, 16:54:48) 
[Clang 4.0.1 (tags/RELEASE_401/final)] :: Anaconda, Inc. on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> from astropy.time import Time
>>> import astropy.units as u
>>> t1 = Time("2018-07-24T10:41:56.807015240")
>>> t2 = t1 + 0.0*u.s
>>> from astropy.time import TimeDelta
>>> t3 = t1 + TimeDelta(0.0*u.s)
>>> t3 == t1
False
>>> t2 < t1
True
>>> t1.jd2
-0.054203622508796234
>>> t2.jd2
-0.054203622508796345
>>> t3.jd2
-0.054203622508796345

i.e. if you add (at least on my Mac) 0 seconds to a Time the resulting time object is, in this case, 10 picoseconds behind the original time. This breaks my tests because the end date is not allowed to be behind the start date. I’m fixing the tests by converting the Time to a ISO string and then converting back to a Time object before doing the >= test but I’m wondering whether this behavior is deliberate or if no-one has ever tried it.

  • Should __eq__ be comparing floating point numbers to 0.0 without some form of precision check (math.isclose?).
  • If a precision is specified for the Time should that be used in the equality?
  • If a time delta of 0 seconds is added to a Time should the new Time go backwards in time?
  • Is Time meant to support picoseconds precision? If it isn’t then the equality test should take that into account. If it is then the above suggests it’s not.

Issue Analytics

  • State:open
  • Created 4 years ago
  • Comments:23 (23 by maintainers)

github_iconTop GitHub Comments

1reaction
taldcroftcommented, Apr 8, 2020

Looking at np.isclose, I’m a bit dubious of shoe-horning that to work for Time since rtol and equal_nan are both not applicable in this case. I would instead suggest to have the __array_function__ override raise NotImplemented and tell the user to use Time.isclose, which will have the proper documentation etc.

1reaction
mhvkcommented, May 21, 2019

p.p.s. That said, it would be good to have a version of isclose or so - I end up writing too many tests of the sort of assert abs(t1 - t2) < 1 * u.ns

Read more comments on GitHub >

github_iconTop Results From Across the Web

Equality comparisons and sameness - JavaScript | MDN
If the values have the same type, are not numbers, and have the same value, they're considered equal. Finally, if both values are...
Read more >
How should I do floating point comparison? - Stack Overflow
If you really care about equality in any precise sense, you're probably seeking a solution that doesn't involve floating point. Share.
Read more >
Comparing Floating Point Numbers, 2012 Edition
Comparing for equality​​ Simple values like 0.1 cannot be precisely represented using binary floating point numbers, and the limited precision of ...
Read more >
Improving the performance of equality or comparison evaluation
Hashing expressions can improve performance at equality-test stage. Test evaluations that involve variables from two different rule conditions can be time ...
Read more >
Equality and Comparison in Java: Pitfalls and Best Practices
Java has different methods of comparing objects and primitives, each with its own semantics. Using the “wrong” one can lead to unexpected ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found