Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
8 changes: 6 additions & 2 deletions si-units/src/extra_units.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use crate::{PySIObject, QuantityError, _JOULE, _KELVIN, _METER};
use crate::{_JOULE, _KELVIN, _METER, PySIObject, QuantityError};
use pyo3::PyTypeInfo;
use pyo3::exceptions::PyTypeError;
use pyo3::prelude::*;
use pyo3::types::{PyFloat, PyNotImplemented};
use pyo3::PyTypeInfo;

#[pyclass(module = "si_units")]
#[derive(Clone, Copy)]
Expand Down Expand Up @@ -110,6 +110,10 @@ impl Angle {
Self(-self.0)
}

fn value_in(&self, unit: Self) -> f64 {
self.0 / unit.0
}

fn sin(&self) -> f64 {
self.0.sin()
}
Expand Down
16 changes: 11 additions & 5 deletions si-units/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -135,11 +135,17 @@ impl PySIObject {
self.unit.eq(&other.unit)
}

pub fn value_in<'py>(&self, py: Python<'py>, unit: &Self) -> PyResult<Bound<'py, PyAny>> {
self.check_units(unit)?;
self.value
.bind(py)
.call_method1("__truediv__", (&unit.value,))
pub fn value_in<'py>(
&self,
py: Python<'py>,
unit: &Bound<'py, PyAny>,
) -> PyResult<Bound<'py, PyAny>> {
if unit.cast::<Celsius>().is_ok() {
return self.__truediv__(unit);
}
let u = unit.extract::<PyRef<Self>>()?;
self.check_units(&u)?;
self.value.bind(py).call_method1("__truediv__", (&u.value,))
}

#[classattr]
Expand Down
1 change: 1 addition & 0 deletions si-units/src/si_units/_core.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,7 @@ class Angle:
def __rmul__(self, lhs: float) -> Self: ...
def __truediv__(self, rhs: Any) -> Any: ...
def __neg__(self) -> Self: ...
def value_in(self, unit: Self) -> float: ...
def sin(self) -> float: ...
def cos(self) -> float: ...
def tan(self) -> float: ...
Expand Down
Loading