mirror of
https://github.com/aljazceru/lightning.git
synced 2026-01-08 16:44:22 +01:00
common: Add multiplication primitives for amount_msat and amount_sat
The scale variants weren't usable since they use `double` as a scale factor, which in turn causes imprecisions.
This commit is contained in:
@@ -507,6 +507,22 @@ struct amount_sat amount_sat_div(struct amount_sat sat, u64 div)
|
||||
return sat;
|
||||
}
|
||||
|
||||
bool amount_sat_mul(struct amount_sat *res, struct amount_sat sat, u64 mul)
|
||||
{
|
||||
if ( mul_overflows_u64(sat.satoshis, mul))
|
||||
return false;
|
||||
res->satoshis = sat.satoshis * mul;
|
||||
return true;
|
||||
}
|
||||
|
||||
bool amount_msat_mul(struct amount_msat *res, struct amount_msat msat, u64 mul)
|
||||
{
|
||||
if ( mul_overflows_u64(msat.millisatoshis, mul))
|
||||
return false;
|
||||
res->millisatoshis = msat.millisatoshis * mul;
|
||||
return true;
|
||||
}
|
||||
|
||||
bool amount_msat_fee(struct amount_msat *fee,
|
||||
struct amount_msat amt,
|
||||
u32 fee_base_msat,
|
||||
|
||||
@@ -92,6 +92,9 @@ WARN_UNUSED_RESULT bool amount_sat_scale(struct amount_sat *val,
|
||||
struct amount_msat amount_msat_div(struct amount_msat msat, u64 div);
|
||||
struct amount_sat amount_sat_div(struct amount_sat sat, u64 div);
|
||||
|
||||
bool amount_sat_mul(struct amount_sat *res, struct amount_sat sat, u64 mul);
|
||||
bool amount_msat_mul(struct amount_msat *res, struct amount_msat msat, u64 mul);
|
||||
|
||||
/* Is a == b? */
|
||||
bool amount_sat_eq(struct amount_sat a, struct amount_sat b);
|
||||
bool amount_msat_eq(struct amount_msat a, struct amount_msat b);
|
||||
|
||||
Reference in New Issue
Block a user