Skip to content

replace pxt_powi implementation with better one - #1484

Merged
Richard Knoll (riknoll) merged 2 commits into
masterfrom
dev/riknoll/pxt_powi
Aug 26, 2024
Merged

replace pxt_powi implementation with better one#1484
Richard Knoll (riknoll) merged 2 commits into
masterfrom
dev/riknoll/pxt_powi

Conversation

@riknoll

Copy link
Copy Markdown
Member

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 Martin Williams (@martinwork)'s implementation with some extra handling when x=0

@riknoll
Richard Knoll (riknoll) requested a review from a team August 26, 2024 16:49
@riknoll
Richard Knoll (riknoll) merged commit 151e6f7 into master Aug 26, 2024
@riknoll
Richard Knoll (riknoll) deleted the dev/riknoll/pxt_powi branch August 26, 2024 19:56
curlyz pushed a commit to curlyz/pxt-common-packages that referenced this pull request Feb 20, 2025
* replace pxt_powi implementation with better one

* add comment
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