pxt-microbit icon indicating copy to clipboard operation
pxt-microbit copied to clipboard

micro:bit makecode-editor simulator behaves differently from real micro:bit (v2) in the function Math.pow() with floats as exponents.

Open matthiasamberg opened this issue 4 years ago • 6 comments

Describe the bug The microbit simulators behaviour differs from the microbit behaviour when using Math.pow(a,b) if the exponent (b) is not an integer.

The result of Math.pow(2, 3.5) is 8 on the micro:bit (it only seems to use the integer part (3) instead of (3.5). Math.pow(2, 3.5) is 11.31370849898476 in the simulator.

To Reproduce Steps to reproduce the behavior:

  1. run the following code from the makecode.microbit.com javascript editor in the simulator and on a real microbit device. '''basic.showNumber(2 ** 3.5)'''
  2. observe the number displayed on the micro:bit.
  3. The real (V2) microbit device will display 8 whereas the simulator will display 11.3137...

Expected behavior The simulator and microbit must produce the same result.

Either the simulator also uses integers only for the exponent and the result is 8 also in the simulator or the microbit Math.pow() function has an issue and needs to be fixed to accept float values as well for the exponent part.

  • Which version of the micro:bit is this relevant to [ EG V1.3, V1.5, V2.0, or specify it's not hardware related ]

V2

Desktop (please complete the following information): The simulator and microbit behaviour does not seem to be os dependent

  • OS: MacOS 12 and LInux (Ubuntu Mate 20.04)
  • Browser Chrome
  • Version 94.0.4606.81 (on Ubuntu)

Additional context None that seems relevant.

matthiasamberg avatar Nov 23 '21 13:11 matthiasamberg

i have also experienced something similar with rounding being different from the simulator to the Micro:Bit V2.

The result of a Blocks calculation differs at the "math_log" variable. In the online simulator it displays 0.003, but when flashed to the micro:bit it rounds down to 0 meaning the "inv_t" variable miscalculates.

make:code link (remove "analog pin0" from "reading" variable and replace with 489 to follow below example)

b = 4275
r2 = 10000
r25 = 10000
t0k = 273.15
n = 1000
t0 = t0k + 25

reading = 489
vout = (reading * 3.3) / 1023
r = (r2 * (3.3 - vout)) / vout
math_log = n * (((r / r25) ** (1 / n)) - 1)
inv_t = (1 / t0) + ((1 / b) * math_log)
t = (1 / inv_t) - t0k

samnotwise avatar Jan 28 '22 10:01 samnotwise

Unfortunately floating point pow is harder to implement in the hardware.

abchatra avatar Feb 23 '22 00:02 abchatra

@abchatra I am not saying the micro:bit must implement a floating point version of the power function. Merely the simulator and the micro:bit should produce the same result.

matthiasamberg avatar Feb 23 '22 08:02 matthiasamberg

Request to match the simulator with the hardware pow algorithm

abchatra avatar Mar 03 '23 17:03 abchatra

@abchatra @matthiasamberg @samnotwise

Arising from support ticket https://support.microbit.org/helpdesk/tickets/68062 (private)

x to the power y can be calculated accurately for float exponents in Javascript as exp(y*log(x)). For some parameters at least, it is possible to use a series (faster but less accurate).

https://makecode.microbit.org/_KzMRbKDMRdpA

In micro:bit, the C++ pow function is about 2.9 times slower than x ** y in V1, and 1.36 times slower in V2. Math.exp(y*Math.log(x)) is about 4.3 times slower than x ** y in V1 and V2, and trapping x=0 and y=1 obviously takes longer.

martinwork avatar Oct 22 '23 17:10 martinwork

The need to calculate powers with floating point exponents has been raised again in support ticket https://support.microbit.org/helpdesk/tickets/68438 (private)

martinwork avatar Nov 02 '23 12:11 martinwork