Thanks to visit codestin.com
Credit goes to github.com

Skip to content

py: implemented round(f, N>0), added test #1376

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

Closed
wants to merge 3 commits into from
Closed

py: implemented round(f, N>0), added test #1376

wants to merge 3 commits into from

Conversation

turbinenreiter
Copy link
Contributor

In the forum someone asked about round with arguments bigger than 0, and I found that easy enough to be my first patch in C.

(forum thread: http://forum.micropython.org/viewtopic.php?f=2&t=802 )

@turbinenreiter
Copy link
Contributor Author

>>>round(1.123456789, 9)
1.123456768

should be 1.123456789.
It rounds wrong.

@dhylands
Copy link
Contributor

@turbinenreiter I mentioned this on the forum, but I figured I'd mention it here as well.

  • You've got some build failures - click on the red x to see the full log. I think you need to add some casts to convert doubles to mp_float_t
  • I'd recommend squashing this into a single commit
  • The PR title should have a py: at the front

@turbinenreiter turbinenreiter changed the title implemented round(f, N>0), added test py: implemented round(f, N>0), added test Jul 12, 2015
@turbinenreiter
Copy link
Contributor Author

I think I got the casts figured out, it builds on all platforms, but the test fails, because it rounds wrong in some cases.

round(1.123456789, 8) rounds to 1.1234568, but should be ``1.12345679`

I'll try to figure that out and come back with a better pull request.

It works up until 7 decimals, everything bigger than that gives wrong values.

@dhylands
Copy link
Contributor

@turbinenreiter That's because the FPU on the pyboard is only 32-bit, so you can only get 6 significant digits.

So I'd remove that test and call it a day :)

@pfalcon
Copy link
Contributor

pfalcon commented Jul 12, 2015

@turbinenreiter: Pretty good for the first patch. Few issues: it should not call round or roundf and friends directly, we have a special macro which use function appropriate for a particular port. Rounding by multiplying to power of 10 and then dividing can lead to overflow. It's ok for a first implementation, but there should be "// TODO: Mat lead to overflow" comment.

As for tests, just use "lowest common denominator" for all ports + fuzz factor, which is rounding up to 5.

tannewt pushed a commit to tannewt/circuitpython that referenced this pull request Dec 6, 2018
Add Particle boards and hopefully speed up the SAMD build a little
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

Successfully merging this pull request may close these issues.

4 participants