Darkempire78 / OpenCalc

A simple and beautiful calculator for Android
GNU General Public License v3.0
740 stars 83 forks source link

Fractional and decimal powers not calculated #288

Closed wmgrove closed 9 months ago

wmgrove commented 10 months ago

Describe the bug App does not accurately calculate the power of a number if that number is not an integer, specifically, it truncated to the nearest integer.

To Reproduce Steps to reproduce the behavior:

  1. Calculate a random number to a given power, e.g. 2^3
  2. Change calculation to include a decimal, e.g. 2^3.5
  3. Also worth noting that 2^(7/2) is not a workaround

Expected behavior Steps 2 and 3 above should return approximately 11.314, not 8.

Smartphone (please complete the following information):

wmgrove commented 10 months ago

If I had to assume, your func call for exponents only accepts integers, and should probably be changed. If that is unreasonable, you would at least get a more accurate result if you took the decimal remainder, and multiplied the previous result by the nth root of the starting number where n is the reciprocal of the decimal, though this would likely suffer from having a decimal itself.

wmgrove commented 10 months ago

Issue begins on line 286 of Calculator.kt, where you explicitly truncate to an integer

wmgrove commented 10 months ago

Also, you make the result infinity if the power is 10000 or so, however, this will cause errors when x is -1 <= x <= 1

wmgrove commented 10 months ago

After further research, kotlin specifically allows non-integers as arguments in the pow function

Darkempire78 commented 10 months ago

Partially fixed (https://github.com/Darkempire78/OpenCalc/commit/6efcee90b9ece58b61502c4211711a2afff149e6). Negative float exponents still cause issues

Darkempire78 commented 9 months ago

Fixed in the latest v3-beta3