mirror of
https://github.com/trezor/trezor-firmware.git
synced 2025-02-18 10:32:02 +00:00
stellar: rework number formatting to not use double
This commit is contained in:
parent
cc630a1ebb
commit
1e602998c7
@ -448,7 +448,7 @@ bool stellar_confirmManageOfferOp(const StellarManageOfferOp *msg)
|
|||||||
|
|
||||||
char str_buying[32];
|
char str_buying[32];
|
||||||
char str_buying_asset[32];
|
char str_buying_asset[32];
|
||||||
char str_price[17];
|
char str_price[32];
|
||||||
|
|
||||||
stellar_format_asset(&(msg->buying_asset), str_buying_asset, sizeof(str_buying_asset));
|
stellar_format_asset(&(msg->buying_asset), str_buying_asset, sizeof(str_buying_asset));
|
||||||
stellar_format_price(msg->price_n, msg->price_d, str_price, sizeof(str_price));
|
stellar_format_price(msg->price_n, msg->price_d, str_price, sizeof(str_price));
|
||||||
@ -528,7 +528,7 @@ bool stellar_confirmCreatePassiveOfferOp(const StellarCreatePassiveOfferOp *msg)
|
|||||||
|
|
||||||
char str_buying[32];
|
char str_buying[32];
|
||||||
char str_buying_asset[32];
|
char str_buying_asset[32];
|
||||||
char str_price[17];
|
char str_price[32];
|
||||||
|
|
||||||
stellar_format_asset(&(msg->buying_asset), str_buying_asset, sizeof(str_buying_asset));
|
stellar_format_asset(&(msg->buying_asset), str_buying_asset, sizeof(str_buying_asset));
|
||||||
stellar_format_price(msg->price_n, msg->price_d, str_price, sizeof(str_price));
|
stellar_format_price(msg->price_n, msg->price_d, str_price, sizeof(str_price));
|
||||||
@ -1273,27 +1273,32 @@ void stellar_format_price(uint32_t numerator, uint32_t denominator, char *out, s
|
|||||||
{
|
{
|
||||||
memset(out, 0, outlen);
|
memset(out, 0, outlen);
|
||||||
|
|
||||||
// early exist for invalid denominator
|
// early exit for invalid denominator
|
||||||
if (denominator == 0) {
|
if (denominator == 0) {
|
||||||
strlcpy(out, _("[Invalid Price]"), outlen);
|
strlcpy(out, _("[Invalid Price]"), outlen);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
int scale = 0;
|
// early exit for zero
|
||||||
double dbl_value = (double)numerator / (double)denominator;
|
if (numerator == 0) {
|
||||||
|
strlcpy(out, "0", outlen);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
// Multiply by 10 until the value is larger than the largest possible offer size
|
int scale = 0;
|
||||||
// Largest possible offer size is UINT32_MAX (4294967296)
|
uint64_t value = numerator;
|
||||||
while (dbl_value < UINT32_MAX) {
|
while (value < (UINT64_MAX / 10)) {
|
||||||
dbl_value *= (double)10;
|
value *= 10;
|
||||||
|
scale++;
|
||||||
|
}
|
||||||
|
value /= denominator;
|
||||||
|
while (value < (UINT64_MAX / 10)) {
|
||||||
|
value *= 10;
|
||||||
scale++;
|
scale++;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Cast back to an integer
|
|
||||||
uint64_t scaled_value = (uint64_t) dbl_value;
|
|
||||||
|
|
||||||
// Format with bn_format_uint64
|
// Format with bn_format_uint64
|
||||||
bn_format_uint64(scaled_value, NULL, NULL, scale, 0, false, out, outlen);
|
bn_format_uint64(value, NULL, NULL, scale, 0, false, out, outlen);
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|
Loading…
Reference in New Issue
Block a user