amount: amount_sat_scale method

For scaling/multiplying sats
This commit is contained in:
niftynei 2021-04-23 14:00:40 -05:00 committed by Rusty Russell
parent 916edaa839
commit 16c92b7da3
2 changed files with 19 additions and 2 deletions

View file

@ -325,10 +325,10 @@ WARN_UNUSED_RESULT bool amount_msat_add_sat(struct amount_msat *val,
}
WARN_UNUSED_RESULT bool amount_msat_scale(struct amount_msat *val,
struct amount_msat sat,
struct amount_msat msat,
double scale)
{
double scaled = sat.millisatoshis * scale;
double scaled = msat.millisatoshis * scale;
/* If mantissa is < 64 bits, a naive "if (scaled >
* UINT64_MAX)" doesn't work. Stick to powers of 2. */
@ -338,6 +338,20 @@ WARN_UNUSED_RESULT bool amount_msat_scale(struct amount_msat *val,
return true;
}
WARN_UNUSED_RESULT bool amount_sat_scale(struct amount_sat *val,
struct amount_sat sat,
double scale)
{
double scaled = sat.satoshis * scale;
/* If mantissa is < 64 bits, a naive "if (scaled >
* UINT64_MAX)" doesn't work. Stick to powers of 2. */
if (scaled >= (double)((u64)1 << 63) * 2)
return false;
val->satoshis = scaled;
return true;
}
bool amount_sat_eq(struct amount_sat a, struct amount_sat b)
{
return a.satoshis == b.satoshis;

View file

@ -83,6 +83,9 @@ WARN_UNUSED_RESULT bool amount_sat_sub_msat(struct amount_msat *val,
WARN_UNUSED_RESULT bool amount_msat_scale(struct amount_msat *val,
struct amount_msat msat,
double scale);
WARN_UNUSED_RESULT bool amount_sat_scale(struct amount_sat *val,
struct amount_sat sat,
double scale);
struct amount_msat amount_msat_div(struct amount_msat msat, u64 div);
struct amount_sat amount_sat_div(struct amount_sat sat, u64 div);