diff --git a/pulser-core/pulser/waveforms.py b/pulser-core/pulser/waveforms.py index 44f8b668f..09456e910 100644 --- a/pulser-core/pulser/waveforms.py +++ b/pulser-core/pulser/waveforms.py @@ -805,12 +805,12 @@ def duration(self) -> int: @cached_property def _samples(self) -> np.ndarray: """The value at each time step that describes the waveform.""" - return cast( - np.ndarray, - np.round( - self._interp_func(np.arange(self._duration)), decimals=9 - ), # Rounds to the order of Hz - ) + samples = self._interp_func(np.arange(self._duration)) + value_range = np.max(np.abs(samples)) + decimals = int( + min(np.finfo(samples.dtype).precision - np.log10(value_range), 9) + ) # Reduces decimal values below 9 for large ranges + return cast(np.ndarray, np.round(samples, decimals=decimals)) @property def interp_function( diff --git a/tests/test_waveforms.py b/tests/test_waveforms.py index c6dd85f37..06dd291b6 100644 --- a/tests/test_waveforms.py +++ b/tests/test_waveforms.py @@ -249,6 +249,12 @@ def test_interpolated(): interp_wf2.samples, np.linspace(0, 1, num=dt) ** 2, atol=1e-3 ) + # Test rounding when range of values is large + wf = InterpolatedWaveform( + 1000, times=[0.0, 0.5, 1.0], values=[0, 2.6e7, 0] + ) + assert np.all(wf.samples >= 0) + def test_kaiser(): duration: int = 40