-2

i am trying to use an exponential to work out the distance the z coordinate would be on screen from the basic z coordinate the object is, as the further away the object is the slower the object moves away and vice versa when the object is coming closer, it speeds up. but the calculation involved in doing so requires a negative double base to double power that may or may not be a negative

Math.pow(-1.1, -(z-25))+10.8347

if i use numbers between -10 and 20 every 1.5, it gives these answers

-10.0 = -17.26773684806433
-8.5 = NaN
-7.0 = 31.948476745352608
-5.5 = NaN
-4.0 = -5.028392971714952
-2.5 = NaN
-1.0 = 22.752876537727232
0.5 = NaN
2.0 = 1.8803975674476092
3.5 = NaN
5.0 = 17.56219994932561
6.5 = NaN
8.0 = 5.780229715007055
9.5 = NaN
11.0 = 14.632198335832413
12.5 = NaN
14.0 = 7.981583293889997
15.5 = NaN
17.0 = 12.978288810000002
18.5 = NaN
20.0 = 9.22419

when using desmos, i should be getting a nice slow exponential with an answer to everyone. but here the calculation will only work the input is an even whole number, decimals give NaN and odd numbers seem to be doing their own thing so the only correct numbers here are -10, -4, 2, 8, 14, 20, putting these answers into excel with their answer shows the exponential but i would need this equation to work for pretty much all numbers.

Green.jab
  • 7
  • 3
  • 3
    Did you [read the javadocs for Math.pow](https://docs.oracle.com/javase/8/docs/api/java/lang/Math.html#pow-double-double-)? "If the first argument is finite and less than zero ... [and] if the second argument is finite and not an integer, then the result is NaN." – yshavit Aug 12 '17 at 23:00
  • then is there a way which i can source my calculation from somewhere else? – Green.jab Aug 12 '17 at 23:28
  • With doubles, no. For instance, `Pow.math(x, 1.5)` is `Pow.math(x, 3./2.)`. That means the square root of x, cubed. If x is negative, its square root is an imaginary number -- and doubles can't express those. – yshavit Aug 12 '17 at 23:49
  • like using the answers from a site like desmos – Green.jab Aug 13 '17 at 00:33

2 Answers2

2

A formula that is supposed to give a distance should have a real, not imaginary, result for all applicable inputs.

Raising a negative number to a fractional power has an imaginary number result. Java double is an approximation to the real numbers. Java Math functions return NaN whenever the result would be imaginary.

Raising a negative number to an even integer power gives a positive result. Raising a negative number to an odd integer power gives a negative result.

You prefer the results for the even integer values of z, corresponding to odd integer values of z-25. The better behaved formula -Math.pow(1.1, -(z - 25)) + 10.8347 matches for those inputs, but you need to rethink the theory underlying your formula.

-10.0 : -17.26773684806433
-8.5 : -13.524047455696502
-7.0 : -10.279076745352606
-5.5 : -7.466387494888428
-4.0 : -5.028392971714952
-2.5 : -2.91517790750445
-1.0 : -1.0834765377272344
0.5 : 0.5042132175022935
2.0 : 1.8803975674476092
3.5 : 3.0732523797913576
5.0 : 4.107200050674389
6.5 : 5.00340952651492
8.0 : 5.780229715007055
9.5 : 6.453565158914292
11.0 : 7.037201664167585
12.5 : 7.543088549146725
14.0 : 7.981583293889997
15.5 : 8.36166359815682
17.0 : 8.691111189999997
18.5 : 8.976671148126837
20.0 : 9.22419
Patricia Shanahan
  • 25,849
  • 4
  • 38
  • 75
0

i figured it out by changing the negatives from

Math.pow(-1.1, -(z-25))+10.8347

to

-(Math.pow(1.1, -(z-25))-10.8347)

somehow it let all the answers work

Green.jab
  • 7
  • 3
  • I agree that the changed formula matches for the values you like, but do you understand why it is right in terms of the theory that led you to your original formula? If not, you may get problems in other cases. – Patricia Shanahan Aug 13 '17 at 00:40