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

Fix of string.format("%.0f") #3199

Merged
merged 1 commit into from
Jul 7, 2020
Merged

Fix of string.format("%.0f") #3199

merged 1 commit into from
Jul 7, 2020

Conversation

vsky279
Copy link
Contributor

@vsky279 vsky279 commented Jul 6, 2020

Fixes #3169.

  • This PR is for the dev branch rather than for master.
  • This PR is compliant with the other contributing guidelines as well (if not, please describe why).
  • I have thoroughly tested my contribution.
  • The code changes are reflected in the documentation at docs/*.

This PR fixes the string.format("%.0f") to output float number without decimals after decimal point. At this same time it preserves 6 decimals when no format is specified (%f).

The default value needs to be set in the vsprintf function rather than in dtoa function of app/libc/stdio.c.

The following testing code gives results as expected (also for %e and %g formatting strings).

local pi=3.14159265359
print(("%f"):format(pi)) 
for i=0,12 do print(("%"..i.."f"):format(pi)) end
for i=0,8 do print(("%."..i.."f"):format(pi)) end
for i=0,12 do print(("%"..i..".4f"):format(pi)) end

@TerryE
Copy link
Collaborator

TerryE commented Jul 7, 2020

BTW, did you see that I've added the % operator for strings, so

for i=0,12 do print(("%"..i.."f"):format(pi)) end

Can be rewritten:

for i=0,12 do print("%%%uf" % i % pi) end

@marcelstoer marcelstoer added this to the Next release milestone Jul 7, 2020
@TerryE
Copy link
Collaborator

TerryE commented Jul 7, 2020

a=1.23456789  for _,f in pairs{"%%%uf","%%.%uf","%%%u.4f"} do 
for i=0,12 do print(f % i % a) end end

works fine now, so I will commit this.

@TerryE TerryE merged commit 2fa63a1 into nodemcu:dev Jul 7, 2020
@vsky279
Copy link
Contributor Author

vsky279 commented Jul 7, 2020

BTW, did you see that I've added the % operator for strings

@TerryE I missed that. It's quite compact way to write string format functionality. 👍
In term of efficiency of compiled code I think there is no advantage. Or am I wrong?

@TerryE
Copy link
Collaborator

TerryE commented Jul 7, 2020

Of the 3 ways of coding this, the modulus operator generates the least code. Try doing a luac.cross -l if you want to see:

                        ; a=string.format(f,1)
        3       [3]     GETTABUP        2 0 -2  ; _ENV "string"
        4       [3]     GETTABLE        2 2 -3  ; "format"
        5       [3]     MOVE            3 1
        6       [3]     LOADK           4 -4    ; 1
        7       [3]     CALL            2 3 2
        8       [3]     MOVE            0 2
                        ; a=f:format(1)
        9       [4]     SELF            2 1 -3  ; "format"
        10      [4]     LOADK           4 -4    ; 1
        11      [4]     CALL            2 3 2
        12      [4]     MOVE            0 2
                        ; a=f%1
        13      [5]     MOD             0 1 -4

It will also be slightly faster.

@vsky279
Copy link
Contributor Author

vsky279 commented Jul 7, 2020

I see. Very good. I did not realize the interpretation of the operator is done inside Lua and not in the compiled code :-)

@vsky279 vsky279 deleted the c_stringformat branch July 8, 2020 09:43
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants