Skip to content

Commit

Permalink
Add percentile representation (ordinals#378)
Browse files Browse the repository at this point in the history
  • Loading branch information
casey authored Aug 29, 2022
1 parent 0439fec commit 645dd1f
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 4 deletions.
56 changes: 52 additions & 4 deletions src/ordinal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ impl Ordinal {
Epoch::from(self).0 / CYCLE_EPOCHS
}

pub(crate) fn percentile(self) -> String {
format!("{}%", (self.0 as f64 / Self::LAST.0 as f64) * 100.0)
}

pub(crate) fn epoch(self) -> Epoch {
self.into()
}
Expand Down Expand Up @@ -79,8 +83,8 @@ impl Ordinal {
Ok(Ordinal(Self::SUPPLY - x))
}

fn from_degree(s: &str) -> Result<Self> {
let (cycle_number, rest) = s
fn from_degree(degree: &str) -> Result<Self> {
let (cycle_number, rest) = degree
.split_once('°')
.ok_or_else(|| anyhow!("Missing degree symbol"))?;
let cycle_number = cycle_number.parse::<u64>()?;
Expand Down Expand Up @@ -133,8 +137,10 @@ impl Ordinal {
Ok(height.starting_ordinal() + block_offset)
}

fn from_decimal(s: &str) -> Result<Self> {
let (height, offset) = s.split_once('.').ok_or_else(|| anyhow!("Missing period"))?;
fn from_decimal(decimal: &str) -> Result<Self> {
let (height, offset) = decimal
.split_once('.')
.ok_or_else(|| anyhow!("Missing period"))?;
let height = Height(height.parse()?);
let offset = offset.parse::<u64>()?;

Expand All @@ -144,6 +150,20 @@ impl Ordinal {

Ok(height.starting_ordinal() + offset)
}

fn from_percentile(percentile: &str) -> Result<Self> {
if !percentile.ends_with('%') {
bail!("Invalid percentile: {}", percentile);
}

let percentile = percentile[..percentile.len() - 1].parse::<f64>()?;

let position = percentile / 100.0;

let n = position * Ordinal::LAST.n() as f64;

Ok(Ordinal(n.round() as u64))
}
}

impl PartialEq<u64> for Ordinal {
Expand Down Expand Up @@ -174,6 +194,8 @@ impl FromStr for Ordinal {
Self::from_name(s)
} else if s.contains('°') {
Self::from_degree(s)
} else if s.contains('%') {
Self::from_percentile(s)
} else if s.contains('.') {
Self::from_decimal(s)
} else {
Expand Down Expand Up @@ -487,4 +509,30 @@ mod tests {
assert_eq!(Ordinal(50 * 100_000_000).third(), 0);
assert_eq!(Ordinal(50 * 100_000_000 + 1).third(), 1);
}

#[test]
fn percentile() {
assert_eq!(Ordinal(0).percentile(), "0%");
assert_eq!(
Ordinal(Ordinal::LAST.n() / 2).percentile(),
"49.99999999999998%"
);
assert_eq!(Ordinal::LAST.percentile(), "100%");
}

#[test]
fn percentile_round_trip() {
fn case(n: u64) {
let expected = Ordinal(n);
let actual = expected.percentile().parse::<Ordinal>().unwrap();
assert_eq!(expected, actual);
}

for n in 0..1024 {
case(n);
case(Ordinal::LAST.n() / 2 + n);
case(Ordinal::LAST.n() - n);
case(Ordinal::LAST.n() / (n + 1));
}
}
}
3 changes: 3 additions & 0 deletions src/subcommand/server/templates/ordinal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ mod tests {
<dt>offset</dt><dd>0</dd>
<dt>rarity</dt><dd><span class=mythic>mythic</span></dd>
<dt>time</dt><dd>1970-01-01 00:00:00</dd>
<dt>percentile</dt><dd>0%</dd>
</dl>
<a>prev</a>
<a href=/ordinal/1>next</a>
Expand Down Expand Up @@ -66,6 +67,7 @@ mod tests {
<dt>offset</dt><dd>0</dd>
<dt>rarity</dt><dd><span class=uncommon>uncommon</span></dd>
<dt>time</dt><dd>1970-01-01 00:00:00</dd>
<dt>percentile</dt><dd>100%</dd>
</dl>
<a href=/ordinal/2099999997689998>prev</a>
<a>next</a>
Expand Down Expand Up @@ -95,6 +97,7 @@ mod tests {
<dt>offset</dt><dd>1</dd>
<dt>rarity</dt><dd><span class=common>common</span></dd>
<dt>time</dt><dd>1970-01-01 00:00:00</dd>
<dt>percentile</dt><dd>0.000000000000047619047671428595%</dd>
</dl>
<a href=/ordinal/0>prev</a>
<a href=/ordinal/2>next</a>
Expand Down
1 change: 1 addition & 0 deletions templates/ordinal.html
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ <h1>Ordinal {{ self.ordinal.n() }}</h1>
<dt>offset</dt><dd>{{ self.ordinal.third() }}</dd>
<dt>rarity</dt><dd><span class={{self.ordinal.rarity()}}>{{ self.ordinal.rarity() }}</span></dd>
<dt>time</dt><dd>{{ self.blocktime }}</dd>
<dt>percentile</dt><dd>{{ self.ordinal.percentile() }}</dd>
</dl>
%% if self.ordinal.n() > 0 {
<a href=/ordinal/{{self.ordinal.n() - 1}}>prev</a>
Expand Down

0 comments on commit 645dd1f

Please sign in to comment.