Skip to content

Commit

Permalink
add ratio ceil and floor operators on amount_msat
Browse files Browse the repository at this point in the history
Changelog-none: add ratio ceil and floor operators on amount_msat

Signed-off-by: Lagrang3 <[email protected]>
  • Loading branch information
Lagrang3 authored and rustyrussell committed Nov 21, 2024
1 parent 4dc1a44 commit 225939a
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 0 deletions.
10 changes: 10 additions & 0 deletions common/amount.c
Original file line number Diff line number Diff line change
Expand Up @@ -516,6 +516,16 @@ double amount_msat_ratio(struct amount_msat a, struct amount_msat b)
return (double)a.millisatoshis / b.millisatoshis;
}

u64 amount_msat_ratio_floor(struct amount_msat a, struct amount_msat b)
{
return a.millisatoshis / b.millisatoshis;
}

u64 amount_msat_ratio_ceil(struct amount_msat a, struct amount_msat b)
{
return (a.millisatoshis + b.millisatoshis - 1) / b.millisatoshis;
}

struct amount_msat amount_msat_div(struct amount_msat msat, u64 div)
{
msat.millisatoshis /= div;
Expand Down
6 changes: 6 additions & 0 deletions common/amount.h
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,12 @@ bool amount_msat_eq_sat(struct amount_msat msat, struct amount_sat sat);
/* a / b */
double amount_msat_ratio(struct amount_msat a, struct amount_msat b);

/* floor(a/b) */
u64 amount_msat_ratio_floor(struct amount_msat a, struct amount_msat b);

/* ceil(a/b) */
u64 amount_msat_ratio_ceil(struct amount_msat a, struct amount_msat b);

/* min(a,b) and max(a,b) */
static inline struct amount_msat amount_msat_min(struct amount_msat a,
struct amount_msat b)
Expand Down

0 comments on commit 225939a

Please sign in to comment.