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

String formatting: plus sign ignored for %+g #3265

Closed
mk-pmb opened this issue Aug 31, 2020 · 16 comments
Closed

String formatting: plus sign ignored for %+g #3265

mk-pmb opened this issue Aug 31, 2020 · 16 comments

Comments

@mk-pmb
Copy link
Contributor

mk-pmb commented Aug 31, 2020

Expected behavior

My number has a plus sign, as on Ubuntu:

$ lua <<<'print(("%+g"):format(123), _VERSION)'
+123    Lua 5.2

Actual behavior

No sign in the REPL on my ESP8266 (using this firmware built with LUA=53 from current dev branch + tmr fix.

Test code

> print(("%+g"):format(123), _VERSION)
123     Lua 5.3

NodeMCU startup banner

NodeMCU 3.0.0.0 built with Docker provided by frightanic.com
        branch: lua53-dev
        commit: 9ff8d9f3e99b5c815b90cc40cb93b0102f80c2dd
        release: 3.0-master_20190907 +104
        release DTS: 202008311952
        SSL: false
        build type: float
        LFS: 0x10000 bytes total capacity
        modules: adc,bit,coap,color_utils,cron,crypto,encoder,file,gpio,gpio_pulse,http,i2c,mdns,mqtt,net,node,ow,pcm,perf,pipe,pwm,pwm2,rfswitch,rotary,rtcfifo,rtcmem,rtctime,sigma_delta,sjson,sntp,spi,struct,tls,tmr,uart,websocket,wifi,wifi_monitor
 build 2020-08-31 19:55 powered by Lua 5.3.5 on SDK 3.0.1-dev(fce080e)
cannot open init.lua: 

Hardware

ESP8266 LoLin v3

@vsky279
Copy link
Contributor

vsky279 commented Sep 6, 2020

@mk-pmb Can you please check the fix in the PR #3270?

@mk-pmb
Copy link
Contributor Author

mk-pmb commented Sep 6, 2020

I tested it with this firmware and this program. The results show that now the + is the same on NodeMCU as on Ubuntu's LUA.
Thank you for fixing it! 👍

However, at the same time they indicate they have a different interpretation of the number of spaces when the sign is omitted. Also you might wonder why I used 3.1410 as the number, not 3.1415… that's because they also have different opinions about rounding.
Should we extend the issue and PR to tackle that?

@vsky279
Copy link
Contributor

vsky279 commented Sep 7, 2020

Thanks for testing. I'll try to have a look at both issues. If there's easy way to fix it it can be done. Maybe we should continue the discussion directly in the PR.

@nwf
Copy link
Member

nwf commented Sep 11, 2020

This didn't get picked up as fixed automagically. If it should remain open, please feel free to reopen it!

@nwf nwf closed this as completed Sep 11, 2020
@mk-pmb
Copy link
Contributor Author

mk-pmb commented Sep 12, 2020

I tested with this firmware built from commit 64bbf00 = the current dev branch. The results are as from my previous test above.

@vsky279 Since the MR is merged now, I guess it would be confusing to continue the discussion there. Will you make a new MR for the spacing and/or rounding bugs? Shall I open issues for them? If the MR seems easy maybe we should just extend and re-open this issue.

@vsky279
Copy link
Contributor

vsky279 commented Sep 12, 2020

You were too fast guys. I suggest reopening this issue rather than opening another one.

I have almost ready the PR for testing for the ' ' flag (e.g. % 8.3f).

As for the rounding - I think it is not something to be corrected. I think the difference @mk-pmb observes between Ubuntu Lua 5.2 and NodeMCU Lua 5.3 is due to the fact that we are using 32bit float while Ubuntu is using 64bit double.

3.1415 does not has precise representation in 32bit float. It is in fact stored as 3.141499996185303 (0x40490e56 = 2.00000000 * 1.5707499980926514) so when rounded to 3 decimals we are still at 3.141. However I don't know how C manage to print 3.1415 instead of 3.141499...
Maybe some more experience programmer can elaborate on this.

print(3.1415) prints number using %.7g format so 3.1415 prints correctly.

@mk-pmb
Copy link
Contributor Author

mk-pmb commented Sep 12, 2020

(If anyone else wonders: He's using big endian notation.) Maybe they multiply by 10**precision first, or calculate the next decimal digit and decide based on that.
Update: In-depth discussion of sprintf rounding modes. Quote by Vincent Lefèvre:

You get different results because the C standard doesn't specify these cases and the C libraries are different.

Would be waaaay to easy if computer rounding would be the same as we learned in school math.

@vsky279
Copy link
Contributor

vsky279 commented Sep 14, 2020

@mk-pmb please test the PR #3283.

@mk-pmb
Copy link
Contributor Author

mk-pmb commented Sep 15, 2020

Will do, probably tomorrow. Meanwhile: For changing the rounding I think we should ensure a wide consensus, do we have that? Edit: I see it's being discussed in the PR.

@vsky279
Copy link
Contributor

vsky279 commented Sep 15, 2020

Definitely there needs to be wide agreement. This is also why I have two separate commits.

@mk-pmb
Copy link
Contributor Author

mk-pmb commented Sep 15, 2020

Done for ESP8266. Meanwhile I got an ESP32 as well, but it may take another few hours until I can test on that. I can't currently test on that so nevermind.

@pjsg
Copy link
Member

pjsg commented Oct 4, 2020

I think that this should be solved with the merging of the snprintf implementation. Can you verify?

@vsky279
Copy link
Contributor

vsky279 commented Oct 4, 2020

I tested this already. Both flags '+' & ' ' work fine with the new snprintf implementation.

The double rounding - it would be nice to have it implemented. Is it possible to submit a PR on source files provided by a submodule?

@pjsg
Copy link
Member

pjsg commented Oct 4, 2020 via email

@vsky279
Copy link
Contributor

vsky279 commented Nov 7, 2020

I think that with 64-bit doubles (PR #3225) there's no need to have the double rounding implemented.
If someone needed precise rounding with doubles it is achieved ("%.3f"%3.1415 gives 3.142 with #define LUA_NUMBER_64BITS). So think we can close this issue.

@nwf
Copy link
Member

nwf commented Nov 17, 2020

Closing by request of @vsky279.

@nwf nwf closed this as completed Nov 17, 2020
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

4 participants