Skip to content

NumberToString (ratpak) fails if using Scientific format for negative exponents, precision reduction and digits >= requested precision #2328

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
axtar opened this issue May 6, 2025 · 0 comments

Comments

@axtar
Copy link

axtar commented May 6, 2025

Describe the bug
NumberToString fails if:
NumberFormat is set to Scientific or Engineering, the exponent is negative, the requested precision is < as the original precision and the number of digits in PNUMBER is >= as the requested precision.
RatToString calls NumberToString and also fails.

Steps To Reproduce

#include <iostream>
#include <ratpak.h>

constexpr auto RAT_RADIX = 10U;
constexpr auto RAT_PRECISION = 32;
constexpr auto RAT_REDUCED_PRECISION = 16;

int main()
{
	ChangeConstants(RAT_RADIX, RAT_PRECISION);
	PNUMBER n = StringToNumber(std::wstring_view(L"1.456789234243564534e-20"), RAT_RADIX, RAT_PRECISION);
	std::wcout << NumberToString(n, NumberFormat::Scientific, RAT_RADIX, RAT_REDUCED_PRECISION) << std::endl;  // not ok, expected 1.456789234243565e-20
	destroynum(n);
}

Output:

0.e-20 

Expected behavior
Instead of 0.e-20 the expected output should be 1.456789234243565e-20
The function works as expected if the exponent is positive.

Additional context
I use ratpak in a (hardware) calculator project, The precision of the calculations and the internal registers is set to 32 but I only display 14 digits, This is why I'm reducing the precision in the conversion.
Ratpak is great - small, and efficient enough to even run on a microcontroller. I think the library just needs a little TLC...

Requested Assignment
I'm just reporting this problem. I don't want to fix it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant