Skip to content

Commit

Permalink
[MIRROR] Extends the metric prefixes. (#1343)
Browse files Browse the repository at this point in the history
* Extends the metric prefixes. (#81739)

## About The Pull Request
Extends the metric prefixes some things will display. Adds the quecto,
ronto, yocto, zepto. atto, exa, zetta, yotta, ronna and quetta prefixes.
## Why It's Good For The Game
Makes it easier to read the numbers when someone manages to break atmos
or whatever.
## Changelog
:cl:
qol: Extended the metric prefixes.
/:cl:

* Extends the metric prefixes.

---------

Co-authored-by: Pickle-Coding <[email protected]>
  • Loading branch information
2 people authored and StealsThePRs committed Mar 8, 2024
1 parent ba6c736 commit f9899cc
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 6 deletions.
10 changes: 5 additions & 5 deletions code/__HELPERS/maths.dm
Original file line number Diff line number Diff line change
Expand Up @@ -131,23 +131,23 @@
* Returns: [SI_COEFFICIENT = si unit coefficient, SI_UNIT = prefixed si unit.]
*/
/proc/siunit_isolated(value, unit, maxdecimals=1)
var/static/list/prefixes = list("f","p","n","μ","m","","k","M","G","T","P")
var/static/list/prefixes = list("q","r","y","z","a","f","p","n","μ","m","","k","M","G","T","P","E","Z","Y","R","Q")

// We don't have prefixes beyond this point
// and this also captures value = 0 which you can't compute the logarithm for
// and also byond numbers are floats and doesn't have much precision beyond this point anyway
if(abs(value) <= 1e-18)
if(abs(value) < 1e-30)
. = list(SI_COEFFICIENT = 0, SI_UNIT = " [unit]")
return

var/exponent = clamp(log(10, abs(value)), -15, 15) // Calculate the exponent and clamp it so we don't go outside the prefix list bounds
var/exponent = clamp(log(10, abs(value)), -30, 30) // Calculate the exponent and clamp it so we don't go outside the prefix list bounds
var/divider = 10 ** (round(exponent / 3) * 3) // Rounds the exponent to nearest SI unit and power it back to the full form
var/coefficient = round(value / divider, 10 ** -maxdecimals) // Calculate the coefficient and round it to desired decimals
var/prefix_index = round(exponent / 3) + 6 // Calculate the index in the prefixes list for this exponent
var/prefix_index = round(exponent / 3) + 11 // Calculate the index in the prefixes list for this exponent

// An edge case which happens if we round 999.9 to 0 decimals for example, which gets rounded to 1000
// In that case, we manually swap up to the next prefix if there is one available
if(coefficient >= 1000 && prefix_index < 11)
if(coefficient >= 1000 && prefix_index < 21)
coefficient /= 1e3
prefix_index++

Expand Down
2 changes: 1 addition & 1 deletion code/modules/unit_tests/siunit.dm
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,4 @@
TEST_ASSERT_EQUAL(siunit_pressure(999.9e3), "999.9 MPa" , "")
TEST_ASSERT_EQUAL(siunit_pressure(999.9e3, 0), "1 GPa", "")
TEST_ASSERT_EQUAL(siunit_pressure(1e6), "1 GPa", "")
TEST_ASSERT_EQUAL(siunit_pressure(3e17), "300000 PPa", "")
TEST_ASSERT_EQUAL(siunit_pressure(3e32), "300000 QPa", "")

0 comments on commit f9899cc

Please sign in to comment.