Skip to content

Instantly share code, notes, and snippets.

@alifarazz
Last active December 17, 2025 18:36
Show Gist options
  • Select an option

  • Save alifarazz/d097bd050ee901bb7fcf9611c0dd277c to your computer and use it in GitHub Desktop.

Select an option

Save alifarazz/d097bd050ee901bb7fcf9611c0dd277c to your computer and use it in GitHub Desktop.

When you're converting from microseconds to milliseconds, or go from higher precision to a lower one, or downscaling in any form; you have to be careful not to just do a divide. You also need to adjust the y-intercept. There are better methods for higher-accuracy downscaling methods, but they're more complex than just an add and a divide (also, the divide is a division by a constant, so it gets implemented as a multiplication and a bunch of logical shifts).

Blue line is the high-precision that we're converting from, orange line is what we get if we just int divide by scale, green line is what we get if we first add half of the divisor first and then divide. As you see, the orange line is closer to the ground truth that we're converting from. However, it overshoots the ground truth at of the time, which is something to be aware of. pngout

import seaborn as sns
import numpy as np
src = np.arange(-3_000, 3_001, 1)
bad = ((src) // 1000) * 1000
better = ((src + 500) // 1000) * 1000
sns.lineplot(y=src, x=src)
sns.lineplot(y=bad, x=src, alpha=.5)
sns.lineplot(y=better, x=src, alpha=.5)
plt.xlabel("Actual Values")
plt.ylabel("Downscaled-then-Upscaled Values")
plt.tight_layout()
plt.show()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment