round_to_power#

gwpy.utils.round_to_power(x: float, base: float = 2, which: str | None = None) float[source]#

Round a positive value to the nearest integer power of base.

Parameters:
xfloat

Value to round, must be strictly positive.

basefloat

Base to whose power x will be rounded.

whichstr or None

Which value to round to, must be one of 'lower', 'upper', or None to round to whichever is nearest.

Returns:
roundedfloat

The rounded value.

Notes

The object returned will be of the same type as base.

Examples

>>> from gwpy.utils.misc import round_to_power
>>> round_to_power(2)
2
>>> round_to_power(9, base=10)
10
>>> round_to_power(5, which='lower')
4