On Mon, Feb 6, 2017 at 5:42 AM, Juraj Sukop <juraj.su...@gmail.com> wrote:
> Do you mean something like: > > isclose(f(x), 0.0, rel_tol, abs_tol) > > If so, what should `rel_tol` and `abs_tol` be? > isclose is mostly about "relative" closeness, so rel_tol is more-or-less the number of decimal digits you want the same: In [13]: isclose(1.11111111119, 1.11111111118, rel_tol=1e-11) Out[13]: True 11 digits are the same In [14]: isclose(1.11111111119, 1.11111111118, rel_tol=1e-12) Out[14]: False 12 aren't ( I say more or less, because FP values are in binary and we tend to think in decimal) You might think you'd want to know if the two values are as close as possible given the FP representation -- which is what nextafter() would give you -- but it is very, very, rare that a computation accurately preserves ALL the digits -- and if it did, you could use the regular old ==, <, > checks. But NOTHING is relatively close to zero, so if you want to compare to zero you need an absolute tolerance -- what is close to zero in your application: In [19]: isclose(1e-100, 0.0, abs_tol = 1e-101) Out[19]: False In [20]: isclose(1e-100, 0.0, abs_tol = 1e-100) Out[20]: True Again, you probably don't want the smallest possible representable FP value -- cause if your computation was that perfect, why not just check for zero? Figuring out what value makes sense in your use case requires either careful numerical analysis (that few of us are capable of!), or experimentation -- how small a value can you use and get the algorithm to converge? -Chris See PEP 485 for more detail: https://www.python.org/dev/peps/pep-0485/ > On Mon, Feb 6, 2017 at 2:16 PM, M.-A. Lemburg <m...@egenix.com> wrote: > >> On 06.02.2017 13:22, Juraj Sukop wrote: >> > On Mon, Feb 6, 2017 at 11:29 AM, M.-A. Lemburg <m...@egenix.com> wrote: >> > >> >> >> >> Juraj: Could you provide some use cases, where such a function >> >> would help in Python applications ? (I can see use cases >> >> written in C, but due to the low level, find it hard to >> >> believe that people would use this at the Python level) >> >> >> > >> > In my case, `nextafter` would be used to check if a number is close to >> > polynomial zero, e.g.: >> > >> > def f(x): >> > return 2.0*x**3 - 3.0*x**2 + 5.0*x - 7.0 >> > >> > # x = 1.4455284586795218 >> > x = 1.445528458679522 >> > # x = 1.4455284586795223 >> > # x = 1.4455284586795225 >> > >> > left = nextafter(x, -float('inf')) >> > right = nextafter(x, float('inf')) >> > >> > print((f(left) < 0.0) != (f(x) < 0.0) or (f(x) < 0.0) != (f(right) < >> > 0.0)) >> >> Isn't this something you can do with math.isclose() ? >> >> This would even give you a predefined error range, >> not a dynamic one. >> >> -- >> Marc-Andre Lemburg >> eGenix.com >> >> Professional Python Services directly from the Experts (#1, Feb 06 2017) >> >>> Python Projects, Coaching and Consulting ... http://www.egenix.com/ >> >>> Python Database Interfaces ... http://products.egenix.com/ >> >>> Plone/Zope Database Interfaces ... http://zope.egenix.com/ >> ________________________________________________________________________ >> >> ::: We implement business ideas - efficiently in both time and costs ::: >> >> eGenix.com Software, Skills and Services GmbH Pastor-Loeh-Str.48 >> D-40764 Langenfeld, Germany. CEO Dipl.-Math. Marc-Andre Lemburg >> Registered at Amtsgericht Duesseldorf: HRB 46611 >> http://www.egenix.com/company/contact/ >> http://www.malemburg.com/ >> >> > > _______________________________________________ > Python-ideas mailing list > Python-ideas@python.org > https://mail.python.org/mailman/listinfo/python-ideas > Code of Conduct: http://python.org/psf/codeofconduct/ > -- Christopher Barker, Ph.D. Oceanographer Emergency Response Division NOAA/NOS/OR&R (206) 526-6959 voice 7600 Sand Point Way NE (206) 526-6329 fax Seattle, WA 98115 (206) 526-6317 main reception chris.bar...@noaa.gov
_______________________________________________ Python-ideas mailing list Python-ideas@python.org https://mail.python.org/mailman/listinfo/python-ideas Code of Conduct: http://python.org/psf/codeofconduct/