from contextlib import suppress

from django import template

register = template.Library()


def _to_float(val: object) -> float:
    conv: float = 0.0
    with suppress(Exception):
        return float(val)  # ty: ignore[invalid-argument-type]
    return conv


@register.filter(name="sub")
def sub(value: object, arg: object) -> float:
    return _to_float(value) - _to_float(arg)


@register.filter(name="neg")
def neg(value: object) -> float:
    return -_to_float(value)
