Do calculations with double seems a mess.
But within my calculation i have numbers like 2.245, so my vars can’t be int
Horrible
How do youe guys manage that?
Do calculations with double seems a mess.
But within my calculation i have numbers like 2.245, so my vars can’t be int
Horrible
How do youe guys manage that?
Care to explain a bit better what’s messy about them?
Also I have no idea what you mean by “pendant”.
Here you go
pendant = equivalent
Within my function i use this caclulation
/*e is a number from 0 to 100
t comes as 0
n and r comes as 100*/
if ((e / r) < 1 / 2.75) {
println("IS A")
return (n * 7.5625 * e * e + t).toDouble()
} else if (e < 2 / 2.75) {
println("IS B")
return (n * (7.5625 * (e- 1.5 / 2.75) * e + 0.75) + t).toDouble()
} else if (e < 2.5 / 2.75) {
println("IS C")
return (n * (7.5625 * (e - 2.25 / 2.75) * e + 0.9375) + t).toDouble()
} else {
println("IS D")
return (n * (7.5625 * (e - 2.625 / 2.75) * e + 0.984375) + t).toDouble()
}
results should be in between - 100 to 200
But what i get is for example this 6.5485458 or even worse 6548545.5
Mostly what people do is that they use BigDecimal
if you are on the JVM, or they use an Int
that represents the lowest precision (i.e. the number of cents for example in a financial calculation)
It’s unclear to me what’s wrong with that code. Those .toDouble
at the end are a bit suspicious as I’d expect inputs to be doubles, otherwise you might do the computations with Ints and then converting the result to a double, which is likely not what you’d want.
For what’s worth 6.5485458
is between -100
and 200
, so that seems to comply with your requirements.
Also we’d need to see the types of all the parameters, e, n, r, t and possibly a set of values for which you get an unexpected result.
Even better provide us with code we can run here:
Thanks guys, i kind of fixed it.
What i tried was to create the Robert Penners easing (as i did on Javascript too), but someone was faster and smarter than me. Ramji already did it. So i use that one.
Hold on, still a mess and i don’t know why
fun elasticInOut(t: Float): Float {
val pi2 = Math.PI * 2
val s = .45 / pi2 * Math.asin(1.0)
var o = t * 2f
return if (o < 1) {
o -= 1f
(-0.5f * (Math.pow(2.0, (10 * o).toDouble()) * Math.sin((o - s) * pi2 / .45))).toFloat()
} else {
o -= 1f
(Math.pow(2.0, (-10 * o).toDouble()) * Math.sin((o - s) * pi2 / .45) * 0.5 + 1).toFloat()
}
}
and In Main
for(i in (0..100)){
println("Value "+i.toFloat()+" "+elasticInOut((i.toFloat())))
}
Returns
Value 0.0 8.478915E-5
Value 1.0 0.9999152
Value 2.0 1.0
Value 3.0 1.0
Value 4.0 1.0
Value 5.0 1.0
Value 6.0 1.0
Value 7.0 1.0
Value 8.0 1.0
I/System.out: Value 9.0 1.0
Value 10.0 1.0
Value 11.0 1.0
Value 12.0 1.0
Value 13.0 1.0
Value 14.0 1.0
Value 15.0 1.0
Value 16.0 1.0
Value 17.0 1.0
Value 18.0 1.0
Value 19.0 1.0
Value 20.0 1.0
I/System.out: Value 21.0 1.0
Value 22.0 1.0
Value 23.0 1.0
Value 24.0 1.0
Value 25.0 1.0
Value 26.0 1.0
Value 27.0 1.0
Value 28.0 1.0
Value 29.0 1.0
Value 30.0 1.0
Value 31.0 1.0
Value 32.0 1.0
Value 33.0 1.0
I/System.out: Value 34.0 1.0
Value 35.0 1.0
Value 36.0 1.0
Value 37.0 1.0
Value 38.0 1.0
Value 39.0 1.0
Value 40.0 1.0
I/System.out: Value 41.0 1.0
Value 42.0 1.0
Value 43.0 1.0
Value 44.0 1.0
Value 45.0 1.0
Value 46.0 1.0
Value 47.0 1.0
Value 48.0 1.0
Value 49.0 1.0
Value 50.0 1.0
I/System.out: Value 51.0 1.0
Value 52.0 1.0
Value 53.0 1.0
Value 54.0 1.0
Value 55.0 1.0
Value 56.0 1.0
Value 57.0 1.0
I/System.out: Value 58.0 1.0
Value 59.0 1.0
Value 60.0 1.0
Value 61.0 1.0
Value 62.0 1.0
Value 63.0 1.0
Value 64.0 1.0
Value 65.0 1.0
I/System.out: Value 66.0 1.0
Value 67.0 1.0
Value 68.0 1.0
Value 69.0 1.0
I/System.out: Value 70.0 1.0
Value 71.0 1.0
Value 72.0 1.0
Value 73.0 1.0
Value 74.0 1.0
Value 75.0 1.0
Value 76.0 1.0
Value 77.0 1.0
I/System.out: Value 78.0 1.0
Value 79.0 1.0
Value 80.0 1.0
Value 81.0 1.0
Value 82.0 1.0
Value 83.0 1.0
Value 84.0 1.0
Value 85.0 1.0
Value 86.0 1.0
I/System.out: Value 87.0 1.0
Value 88.0 1.0
Value 89.0 1.0
Value 90.0 1.0
Value 91.0 1.0
Value 92.0 1.0
Value 93.0 1.0
Value 94.0 1.0
I/System.out: Value 95.0 1.0
Value 96.0 1.0
Value 97.0 1.0
Value 98.0 1.0
Value 99.0 1.0
I/System.out: Value 100.0 1.0
where o = 2*t - 1
is basically 0 when t
is just 2 so the +1
at the end is the result you get.
Also you have a o < 1
check which is never true, except when t = 0 which is the first iteration. I don’t know whether that’s intended but looks off.
Hmmm. That’s weird, because that’s not my but Ramijs function
I’ll have a closer look at it. Maybe i should’nt go wit Hundret but with 0…1 as given percentage
0
0.01
0.02
0.03
and so on
I have forgot the PathInterpolatorCompat
I guess that’s why
Hmm. I go back to the roots with my own function i think. I don’t want to import the whole RamiJ thing
Any reason why you are mixing floats and doubles?
I didn’t, as i said, that was a actually working and in many apps used function from RamiJ
But im overworking my own function. Floats only
Is there a kotlin equvalent for Math operator -= and /= ?
Okay, maybe you guys can help me out a very last time with this
That’s my new function
fun bounceEaseOut(de:Float, t:Float=0f, n:Float=100f, r:Float=100f):Float {
var e:Float=de
if (e.div(r) < 1f / 2.75f) {
println("A By VAL")
return n * 7.5625f * e * e + t
} else if (e < 2f / 2.75f) {
println("B By VAL")
return n * (7.5625f * e.minus(1.5f / 2.75f) * e + 0.75f) + t
} else if (e < 2.5f / 2.75f) {
println("C By VAL")
return n * (7.5625f * e.minus(2.25f / 2.75f) * e + 0.9375f) + t
} else {
println("D By VAL")
return n * (7.5625f * e.minus(2.625f / 2.75f) * e + 0.984375f) + t
}
}
The working equvalent in Javascript is that
function bounceEaseOut(e, t, n, r) {
if ((e /= r) < 1 / 2.75) {
return n * 7.5625 * e * e + t
} else if (e < 2 / 2.75) {
return n * (7.5625 * (e -= 1.5 / 2.75) * e + 0.75) + t
} else if (e < 2.5 / 2.75) {
return n * (7.5625 * (e -= 2.25 / 2.75) * e + 0.9375) + t
} else {
return n * (7.5625 * (e -= 2.625 / 2.75) * e + 0.984375) + t
}
}
when i call my new kotlin function by
var f:Float=0f
for(i in (0..100)){
println("Value "+i.toFloat()+" "+bounceEaseOut(f))
f+=1f
}
it still refuses to do the right calculation
I should receive a number from 0 to 100. Instead i get Numbers from 0.0 to 7490411.0 and im at the end of my latin
Don’t know what else i should change
i gave a guess. e is never overwritten. Instead it is counting upwards
Don’t know what else i should change
When I’m in that situation, I step through the code with a debugger and examine the variables before and/or after each loop iteration or expression until I isolate the problem. If a debugger isn’t available, print out the variables instead.
This is what i do for the last hours
So which expression is yielding the wrong result?
As i said. Looks like e is just counting up. But i can’t define it closeli.
Is there any other cast beside float and double, that can handle seperators?