Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Deprecate cf_units.util.approx_equal #316

Merged
merged 2 commits into from
Nov 9, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions cf_units/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
"""

import copy
import math
from contextlib import contextmanager

import cftime
Expand All @@ -32,7 +33,7 @@

from . import config
from ._version import version as __version__ # noqa: F401
from .util import _OrderedHashable, approx_equal
from .util import _OrderedHashable

__all__ = [
"CALENDARS",
Expand Down Expand Up @@ -1663,15 +1664,15 @@ def __pow__(self, power):
# But if the power is of the form 1/N, where N is an integer
# (within a certain acceptable accuracy) then we can find the Nth
# root.
if not approx_equal(power, 0.0) and abs(power) < 1:
if not approx_equal(1 / power, round(1 / power)):
if not math.isclose(power, 0.0) and abs(power) < 1:
if not math.isclose(1 / power, round(1 / power)):
raise ValueError("Cannot raise a unit by a decimal.")
root = int(round(1 / power))
result = self.root(root)
else:
# Failing that, check for powers which are (very nearly) simple
# integer values.
if not approx_equal(power, round(power)):
if not math.isclose(power, round(power)):
msg = "Cannot raise a unit by a decimal (got %s)." % power
raise ValueError(msg)
power = int(round(power))
Expand Down
7 changes: 7 additions & 0 deletions cf_units/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
"""

import abc
import warnings
from collections.abc import Hashable


Expand All @@ -18,6 +19,12 @@ def approx_equal(a, b, max_absolute_error=1e-10, max_relative_error=1e-10):
finite precision of floating point numbers.

"""
msg = (
"cf_units.util.approx_equal has been deprecated and will be removed. "
"Please use math.isclose instead."
)
warnings.warn(msg, DeprecationWarning, stacklevel=2)

# Deal with numbers close to zero
if abs(a - b) < max_absolute_error:
return True
Expand Down