Make printf trim trailing zeroes with %[Gg] format #458

Open
MCJack123 wants to merge 1 commit from MCJack123/patch-1 into develop
MCJack123 commented 2021-05-28 17:23:27 -05:00 (Migrated from github.com)

This PR modifies the behavior of pico_printf to automatically trim trailing zeroes when using the %G or %g format specifiers. This is the behavior that is specified by GCC:

The ‘%g’ and ‘%G’ conversions print the argument in the style of ‘%e’ or ‘%E’ (respectively) if the exponent would be less than -4 or greater than or equal to the precision; otherwise they use the ‘%f’ style. A precision of 0, is taken as 1. Trailing zeros are removed from the fractional portion of the result and a decimal-point character appears only if it is followed by a digit.

Previously, printf("%.14g", 2000) would output 2000.0000000000, but with this change it outputs just 2000. This is useful for programs that want the minimum-length conversion of a number with the most precision necessary. (My example is Lua, which uses sprintf(s, "%.14g", num) to convert numbers to strings; previously this would result in unnecessarily large conversions for small integers.)

This change does not affect the default printf - newlib exhibits the same behavior as pico_printf currently does, so that would have to be adjusted as well (and I can say after reading the code that I'm not the one to do that). Users will have to link pico_printf manually to benefit AFAICT.

Please let me know if this breaks anything - I didn't have a chance to test this fully (I'm embroiled in debugging some other stuff), but looking at the code I don't think there should be much to break.

This PR modifies the behavior of `pico_printf` to automatically trim trailing zeroes when using the `%G` or `%g` format specifiers. This is the behavior that is [specified by GCC](https://www.gnu.org/software/libc/manual/html_node/Floating_002dPoint-Conversions.html): > The ‘`%g`’ and ‘`%G`’ conversions print the argument in the style of ‘`%e`’ or ‘`%E`’ (respectively) if the exponent would be less than -4 or greater than or equal to the precision; otherwise they use the ‘`%f`’ style. A precision of `0`, is taken as 1. **Trailing zeros are removed from the fractional portion of the result and a decimal-point character appears only if it is followed by a digit.** Previously, `printf("%.14g", 2000)` would output `2000.0000000000`, but with this change it outputs just `2000`. This is useful for programs that want the minimum-length conversion of a number with the most precision necessary. (My example is Lua, which uses `sprintf(s, "%.14g", num)` to convert numbers to strings; previously this would result in unnecessarily large conversions for small integers.) This change does not affect the default `printf` - newlib exhibits the same behavior as `pico_printf` currently does, so that would have to be adjusted as well (and I can say after reading the code that I'm not the one to do that). Users will have to link `pico_printf` manually to benefit AFAICT. Please let me know if this breaks anything - I didn't have a chance to test this fully (I'm embroiled in debugging some other stuff), but looking at the code I don't think there should be much to break.
kilograham commented 2021-06-16 20:23:33 -05:00 (Migrated from github.com)

Note that people using the SDK CMake build get pico_printf by default.

Note that people using the SDK CMake build get pico_printf by default.
This pull request can be merged automatically.
You are not authorized to merge this pull request.
View command line instructions

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin 'MCJack123/patch-1:MCJack123/patch-1'
git switch 'MCJack123/patch-1'

Merge

Merge the changes and update on Forgejo.

Warning: The "Autodetect manual merge" setting is not enabled for this repository, you will have to mark this pull request as manually merged afterwards.

git switch 'develop'
git merge --no-ff 'MCJack123/patch-1'
git switch 'MCJack123/patch-1'
git rebase 'develop'
git switch 'develop'
git merge --ff-only 'MCJack123/patch-1'
git switch 'MCJack123/patch-1'
git rebase 'develop'
git switch 'develop'
git merge --no-ff 'MCJack123/patch-1'
git switch 'develop'
git merge --squash 'MCJack123/patch-1'
git switch 'develop'
git merge --ff-only 'MCJack123/patch-1'
git switch 'develop'
git merge 'MCJack123/patch-1'
git push origin 'develop'
Sign in to join this conversation.
No reviewers
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set

Reference
gh-raspberrypi/pico-sdk!458
No description provided.