MaJerle / lwprintf

Lightweight printf library optimized for embedded systems
https://majerle.eu/projects/lwprintf-lightweight-stdio-manager-printf-snprintf-vprintf-vsnprintf-sprintf
MIT License
186 stars 32 forks source link

%g trailing zeros are not removed #2

Closed tho-ma closed 4 years ago

tho-ma commented 4 years ago

For %g trailing zeros are not removed 4.0f is displayed as 4.00000 4.0e7 is displayed as 4.00000e+7 To correct, use the following statement at line 780: if (p->m.precision > 0 && (dblnum.decimal_part > 0 || def_type != 'g')) {

Additional zero values end up in a loop. Terminate loop by checking in_enum for zero at line 666: for (exp_cnt = 0; in_num < 1 && in_num > 0; in_num *= 10, --exp_cnt) {}

MaJerle commented 4 years ago

@tho-ma, I haven't seen your code example before and fixed it different way.

Please check if it works for you. Tests passed on my side for the moment but maybe there are corner cases where it is still failing.

tho-ma commented 4 years ago

This works for me, Thanks

%g format should always display only significant digits For this line 653 should check for the g format } else if ((in_num < -FLOAT_MAX_B_ENG || in_num > FLOAT_MAX_B_ENG) && def_type != 'g') {

MaJerle commented 4 years ago

Thank you for yor inputs. I have made changes on develop branch.