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

replace pxt_powi implementation with better one #1484

Merged
merged 2 commits into from
Aug 26, 2024
Merged

Conversation

riknoll
Copy link
Member

@riknoll riknoll commented Aug 26, 2024

Fixes microsoft/pxt-microbit#2192

We've long had an optional define (PXT_POWI) which can be used to prevent the compile from pulling in the std implementation of pow (which is 4k of code by itself). As far as I know, this flag is only actually used in the pxt-microbit to prevent hex size issues with the v1 micro:bit.

The historic PXT_POWI implementation casts the exponent to an integer which obviously provides pretty inaccurate results. This new implementation instead takes advantage of the fact that for positive non-zero x values:

Math.pow(x, y) = Math.exp(y * Math.log(x))

exp and log are already pulled into the hex, so this doesn't increase the hex file size at all (i verified this locally)

this is mostly based on @martinwork's implementation with some extra handling when x=0

@riknoll riknoll requested a review from a team August 26, 2024 16:49
@riknoll riknoll merged commit 151e6f7 into master Aug 26, 2024
3 checks passed
@riknoll riknoll deleted the dev/riknoll/pxt_powi branch August 26, 2024 19:56
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.

Error in calculating power function
3 participants