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

[Bug]: Weird/wrong implementation of power in macros #5117

Open
irisiflimsi opened this issue Dec 18, 2024 · 5 comments
Open

[Bug]: Weird/wrong implementation of power in macros #5117

irisiflimsi opened this issue Dec 18, 2024 · 5 comments
Labels

Comments

@irisiflimsi
Copy link
Contributor

Describe the Bug

All kind of odd numbers appear when using power, log and e. The situation is quite confusing and I don't know which of these functions is at fault. I think the problem is quite old, because MT macro code isn't first choice to do math calculations.

To Reproduce

  1. Open a new macro
  2. Enter
    [r: pow(math.e(),math.log(0.5))]
    [r: pow(math.e(),math.log(0.25))]
  3. Execute macro, result:
    1
    0.367879441171442353448074937747561

Expected Behaviour

0.5
0.25

Screenshots

No response

MapTool Info

1.13.2 and 1.14.3

Desktop

Linux + Windows

Additional Context

The same things happen, when using power instead of pow.

@kwvanderlinde
Copy link
Collaborator

pow() only supports integer exponents, and truncates any fractional exponents. So for pow(math.e(), math.log(0.5)), the exponent (which is about -0.69) get truncated to 0, which is why the result is 1.

I think this is a common pattern:

  • math.* functions support fractional values
  • Other math functions may only support integer values

Documentation of these functions definitely needs to be improved so this is clear.

You can use math.pow() instead which allows fractional base and exponent:

[r: math.pow(math.e(),math.log(0.5))]
[r: math.pow(math.e(),math.log(0.25))]

Results in

0.5
0.25000000000000006

@irisiflimsi
Copy link
Contributor Author

irisiflimsi commented Dec 18, 2024

Mostly valid points. I totally didn't look into math.* . Why isn't math.e taking any parameters? It would have been trivial providing a exponential function. Maybe turn this subquestion into a feature request.

  1. Why are there power and pow?
  2. Can this added to the wiki? This "triplicate" is not mentioned. (Does math.power exist, making it a quadruple?)
  3. If this were all happening in the integer domain, I would have spotted this, rounding and all, but 0.367879441171442353448074937747561 is clearly not an integer. I would have expected another 0 or 1 as output but not that.
    (This was all part of another calculation, which hid this faulty result in another set of later results. Maybe better throw an exception?)

@irisiflimsi
Copy link
Contributor Author

I'd add all the answers to the wiki myself, if I knew how to access it. Except how 0.36787... came to be an integer, because that one I don't know. :-)

@FullBleed
Copy link

I'd add all the answers to the wiki myself, if I knew how to access it.

Contact Taustinoc (per the main page in the wiki) and he can set up a wiki account for you: https://wiki.rptools.info/index.php/Main_Page

@irisiflimsi
Copy link
Contributor Author

I added an appropriate comment into the Wiki. Bug can be closed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants