Skip to content

Round down to the nearest multiple¤

Round down to the nearest multiple.

round_to_multiple(value, multiple) ¤

Round down to the nearest multiple.

Helper function for creating default values.

Example

round_to_multiple(1023, 100) 1000

Parameters:

Name Type Description Default
value int | float

The value to round down.

required
multiple int

The multiple to round down to.

required

Returns:

Type Description
int

The value rounded down to the nearest multiple.

Raises:

Type Description
ValueError

If value is less than multiple.

Source code in sparse_autoencoder/train/utils/round_down.py
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
def round_to_multiple(value: int | float, multiple: int) -> int:  # noqa: PYI041
    """Round down to the nearest multiple.

    Helper function for creating default values.

    Example:
        >>> round_to_multiple(1023, 100)
        1000

    Args:
        value: The value to round down.
        multiple: The multiple to round down to.

    Returns:
        The value rounded down to the nearest multiple.

    Raises:
        ValueError: If `value` is less than `multiple`.
    """
    int_value = int(value)

    if int_value < multiple:
        error_message = f"{value=} must be greater than or equal to {multiple=}"
        raise ValueError(error_message)

    return int_value - int_value % multiple