Skip to content

Instantly share code, notes, and snippets.

@smosher
Last active July 21, 2019 10:29
Show Gist options
  • Save smosher/60b11f3bb44877d63e0866f729e9f377 to your computer and use it in GitHub Desktop.
Save smosher/60b11f3bb44877d63e0866f729e9f377 to your computer and use it in GitHub Desktop.
SI Prefixes for Io
# -- SiPrefixes.io --
#
# convenience Number methods essential
# for dealing with large scale ranges
# eg. EE calculations
#
# Ex:
# 24 giga
# 2.400000e+010
#
SiPrefixes := Object clone do (
tonic := method(self * 10**0)
deca := method(self * 10**1)
centa := method(self * 10**2)
hecto := method(self * 10**2)
deci := method(self * 10**(-1))
centi := method(self * 10**(-2))
kilo := method(self * 1000**1)
mega := method(self * 1000**2)
giga := method(self * 1000**3)
tera := method(self * 1000**4)
peta := method(self * 1000**5)
exa := method(self * 1000**6)
zetta := method(self * 1000**7)
yotta := method(self * 1000**8)
milli := method(self * 1000**(-1))
micro := method(self * 1000**(-2))
nano := method(self * 1000**(-3))
pico := method(self * 1000**(-4))
femto := method(self * 1000**(-5))
atto := method(self * 1000**(-6))
zepto := method(self * 1000**(-7))
yocto := method(self * 1000**(-8))
)
_Prefixes := list("tonic", "kilo", "mega", "giga", "tera", "peta", "exa", "zetta", "yotta")
_PrefixesNeg := list("tonic", "milli", "micro", "nano", "pico", "femto", "atto", "zepto", "yocto")
SiPrefixesIntrospect := Object clone do (
siPrefix := method(
// Nb. parametric log is bugged but seems okay at base 1000
// + roundDown is truncate, sometimes things will appear right of the decimal
scale := self log(1000) roundDown
prefix := if(scale > 0,
_PrefixMap at(scale min(9)),
_PrefixMapNeg at(scale min(9)))
list(self / scale, prefix) join(" ")
)
)
Number appendProto(SiPrefixes)
Number appendProto(SiPrefixesIntrospect)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment