Skip to content
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

Rounding problem #1

Open
tho-ma opened this issue Nov 13, 2020 · 7 comments
Open

Rounding problem #1

tho-ma opened this issue Nov 13, 2020 · 7 comments

Comments

@tho-ma
Copy link

tho-ma commented Nov 13, 2020

%g does not round the values 5307575.0f or 1104515.0f correctly.
Result 5.30757e+6 but should be 5.30758e+6

@MaJerle
Copy link
Owner

MaJerle commented Nov 15, 2020

I have commited new changes to develop branch that seem to work properly.
When calculating decimal and integer parts of double numbers, 0.5 difference (remaining, for check for rounding) was passing statement as < 0.5f and I have fixed this by checking the difference against 0.5f with small epsilon.

Let's see if it works, it passed my tests for the moment.

@tho-ma
Copy link
Author

tho-ma commented Nov 15, 2020

I still get rounding errors for values like 1022265.0
As a workaround I added a small offset at line 676
in_num += 0.000000000000005;

Additional a zero value ends up in a loop.
To correct this the loop at line 672 could be changed to (Checking in_num to be > 0)
for (exp_cnt = 0; in_num < 1 && in_num > 0; in_num *= 10, --exp_cnt) {}

There is small typo in lwprintf.h
Line 107 should be changed to
#define lwprintf_sprintf_ex(lw, s, format, ...) lwprintf_snprintf_ex((lw), (s), SIZE_MAX, (format), ## VA_ARGS)

@MaJerle
Copy link
Owner

MaJerle commented Nov 15, 2020

I still get rounding errors for values like 1022265.0
As a workaround I added a small offset at line 676
in_num += 0.000000000000005;

Additional a zero value ends up in a loop.
To correct this the loop at line 672 could be changed to (Checking in_num to be > 0)
for (exp_cnt = 0; in_num < 1 && in_num > 0; in_num *= 10, --exp_cnt) {}

This has been fixed for the moment and it seems to work. Please check or post list of numbers where you see wrong data.

There is small typo in lwprintf.h
Line 107 should be changed to
#define lwprintf_sprintf_ex(lw, s, format, ...) lwprintf_snprintf_ex((lw), (s), SIZE_MAX, (format), ## VA_ARGS)
Not sure I catch this point?

@tho-ma
Copy link
Author

tho-ma commented Nov 16, 2020

Hi, this approach will not work.
Values like 0.44999999 with format %.1g will fail.

@MaJerle
Copy link
Owner

MaJerle commented Nov 16, 2020

If we add num += 0.000000000000005; in the prv_calculate_dbl_num_data just before calculating integer and decimal parts, does it work properly for you then?

@tho-ma
Copy link
Author

tho-ma commented Nov 16, 2020

Hi, that will do it for me. There are still some values that can fail but the error is much smaller.
The problem seems to be on line 670 and following when dividing or multiplying the value by 10. That may introduce small errors which may have big effects.
Therefore, I was thinking of passing the exponent and orig_num to prv_calculate_dbl_num_data and then calculating the difference.

@tho-ma
Copy link
Author

tho-ma commented Nov 16, 2020

By the way this one of the best solution I found. Also the documentation is great.
Many thanks

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

2 participants