mirror of
https://github.com/aljazceru/lightning.git
synced 2025-12-19 07:04:22 +01:00
amount: amount_sat_scale method
For scaling/multiplying sats
This commit is contained in:
@@ -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;
|
||||
|
||||
@@ -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);
|
||||
|
||||
Reference in New Issue
Block a user