Fix big negative numbers printing incorrect decimals in num_real
This commit is contained in:
parent
dc4b616596
commit
c60ac64e8f
2 changed files with 9 additions and 4 deletions
|
@ -1567,10 +1567,11 @@ String String::num_real(double p_num, bool p_trailing) {
|
||||||
#else
|
#else
|
||||||
int decimals = 6;
|
int decimals = 6;
|
||||||
#endif
|
#endif
|
||||||
// We want to align the digits to the above sane default, so we only
|
// We want to align the digits to the above sane default, so we only need
|
||||||
// need to subtract log10 for numbers with a positive power of ten.
|
// to subtract log10 for numbers with a positive power of ten magnitude.
|
||||||
if (p_num > 10) {
|
double abs_num = Math::abs(p_num);
|
||||||
decimals -= (int)floor(log10(p_num));
|
if (abs_num > 10) {
|
||||||
|
decimals -= (int)floor(log10(abs_num));
|
||||||
}
|
}
|
||||||
return num(p_num, decimals);
|
return num(p_num, decimals);
|
||||||
}
|
}
|
||||||
|
|
|
@ -411,9 +411,13 @@ TEST_CASE("[String] Number to string") {
|
||||||
CHECK(String::num_real(3.141593) == "3.141593");
|
CHECK(String::num_real(3.141593) == "3.141593");
|
||||||
CHECK(String::num_real(3.141) == "3.141"); // No trailing zeros.
|
CHECK(String::num_real(3.141) == "3.141"); // No trailing zeros.
|
||||||
#ifdef REAL_T_IS_DOUBLE
|
#ifdef REAL_T_IS_DOUBLE
|
||||||
|
CHECK_MESSAGE(String::num_real(123.456789) == "123.456789", "Prints the appropriate amount of digits for real_t = double.");
|
||||||
|
CHECK_MESSAGE(String::num_real(-123.456789) == "-123.456789", "Prints the appropriate amount of digits for real_t = double.");
|
||||||
CHECK_MESSAGE(String::num_real(Math_PI) == "3.14159265358979", "Prints the appropriate amount of digits for real_t = double.");
|
CHECK_MESSAGE(String::num_real(Math_PI) == "3.14159265358979", "Prints the appropriate amount of digits for real_t = double.");
|
||||||
CHECK_MESSAGE(String::num_real(3.1415f) == "3.1414999961853", "Prints more digits of 32-bit float when real_t = double (ones that would be reliable for double) and no trailing zero.");
|
CHECK_MESSAGE(String::num_real(3.1415f) == "3.1414999961853", "Prints more digits of 32-bit float when real_t = double (ones that would be reliable for double) and no trailing zero.");
|
||||||
#else
|
#else
|
||||||
|
CHECK_MESSAGE(String::num_real(123.456789) == "123.4568", "Prints the appropriate amount of digits for real_t = float.");
|
||||||
|
CHECK_MESSAGE(String::num_real(-123.456789) == "-123.4568", "Prints the appropriate amount of digits for real_t = float.");
|
||||||
CHECK_MESSAGE(String::num_real(Math_PI) == "3.141593", "Prints the appropriate amount of digits for real_t = float.");
|
CHECK_MESSAGE(String::num_real(Math_PI) == "3.141593", "Prints the appropriate amount of digits for real_t = float.");
|
||||||
CHECK_MESSAGE(String::num_real(3.1415f) == "3.1415", "Prints only reliable digits of 32-bit float when real_t = float.");
|
CHECK_MESSAGE(String::num_real(3.1415f) == "3.1415", "Prints only reliable digits of 32-bit float when real_t = float.");
|
||||||
#endif // REAL_T_IS_DOUBLE
|
#endif // REAL_T_IS_DOUBLE
|
||||||
|
|
Loading…
Reference in a new issue