# How to do an operation on a variable and assign it that new value for easing calculations?

**URL:** <https://discuss.kotlinlang.org/t/how-to-do-an-operation-on-a-variable-and-assign-it-that-new-value-for-easing-calculations/19621>\
**Category:** Support\
**Created:** [October 23, 2020, 1:00pm UTC](https://discuss.kotlinlang.org/t/how-to-do-an-operation-on-a-variable-and-assign-it-that-new-value-for-easing-calculations/19621 "2020-10-23T13:00:57Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![absoluterookie](https://avatars.discourse-cdn.com/v4/letter/a/f17d59/32.png) [@absoluterookie](https://discuss.kotlinlang.org/u/absoluterookie)\
**Post date:** [October 23, 2020, 1:00pm UTC](https://discuss.kotlinlang.org/t/how-to-do-an-operation-on-a-variable-and-assign-it-that-new-value-for-easing-calculations/19621/1 "2020-10-23T13:00:58Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![al3c](https://avatars.discourse-cdn.com/v4/letter/a/e47774/32.png) [@al3c](https://discuss.kotlinlang.org/u/al3c)\
**Post date:** [October 23, 2020, 1:08pm UTC](https://discuss.kotlinlang.org/t/how-to-do-an-operation-on-a-variable-and-assign-it-that-new-value-for-easing-calculations/19621/2 "2020-10-23T13:08:37Z")

</div>

> [@absoluterookie](#):
>
> Do calculations with double seems a mess.

Care to explain a bit better what’s messy about them?  
Also I have no idea what you mean by “pendant”.

---

<div class="post-metadata">

**Author:** ![absoluterookie](https://avatars.discourse-cdn.com/v4/letter/a/f17d59/32.png) [@absoluterookie](https://discuss.kotlinlang.org/u/absoluterookie)\
**Post date:** [October 23, 2020, 1:10pm UTC](https://discuss.kotlinlang.org/t/how-to-do-an-operation-on-a-variable-and-assign-it-that-new-value-for-easing-calculations/19621/3 "2020-10-23T13:10:51Z")

</div>

Here you go

pendant = equivalent

Within my function i use this caclulation

```auto
/*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

---

<div class="post-metadata">

**Author:** ![kyay10](https://avatars.discourse-cdn.com/v4/letter/k/c57346/32.png) [@kyay10](https://discuss.kotlinlang.org/u/kyay10)\
**Post date:** [October 23, 2020, 2:32pm UTC](https://discuss.kotlinlang.org/t/how-to-do-an-operation-on-a-variable-and-assign-it-that-new-value-for-easing-calculations/19621/4 "2020-10-23T14:32:03Z")

</div>

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)

---

<div class="post-metadata">

**Author:** ![al3c](https://avatars.discourse-cdn.com/v4/letter/a/e47774/32.png) [@al3c](https://discuss.kotlinlang.org/u/al3c)\
**Post date:** [October 23, 2020, 2:48pm UTC](https://discuss.kotlinlang.org/t/how-to-do-an-operation-on-a-variable-and-assign-it-that-new-value-for-easing-calculations/19621/5 "2020-10-23T14:48:23Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![bjonnh](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/bjonnh/32/8106_2.png) [@bjonnh](https://discuss.kotlinlang.org/u/bjonnh)\
**Post date:** [October 23, 2020, 2:55pm UTC](https://discuss.kotlinlang.org/t/how-to-do-an-operation-on-a-variable-and-assign-it-that-new-value-for-easing-calculations/19621/6 "2020-10-23T14:55:38Z")

</div>

Even better provide us with code we can run here:

> [@How to Embed runnable code in a post?](https://discuss.kotlinlang.org/t/how-to-embed-runnable-code-in-a-post/16650):
>
> How can I include runnable code samples into my post?

---

<div class="post-metadata">

**Author:** ![absoluterookie](https://avatars.discourse-cdn.com/v4/letter/a/f17d59/32.png) [@absoluterookie](https://discuss.kotlinlang.org/u/absoluterookie)\
**Post date:** [October 23, 2020, 3:02pm UTC](https://discuss.kotlinlang.org/t/how-to-do-an-operation-on-a-variable-and-assign-it-that-new-value-for-easing-calculations/19621/7 "2020-10-23T15:02:34Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![absoluterookie](https://avatars.discourse-cdn.com/v4/letter/a/f17d59/32.png) [@absoluterookie](https://discuss.kotlinlang.org/u/absoluterookie)\
**Post date:** [October 23, 2020, 3:12pm UTC](https://discuss.kotlinlang.org/t/how-to-do-an-operation-on-a-variable-and-assign-it-that-new-value-for-easing-calculations/19621/8 "2020-10-23T15:12:07Z")

</div>

Hold on, still a mess and i don’t know why

```auto
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

```auto
for(i in (0..100)){
         println("Value "+i.toFloat()+" "+elasticInOut((i.toFloat())))
        }
```

Returns

```auto
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
```

---

<div class="post-metadata">

**Author:** ![al3c](https://avatars.discourse-cdn.com/v4/letter/a/e47774/32.png) [@al3c](https://discuss.kotlinlang.org/u/al3c)\
**Post date:** [October 23, 2020, 4:18pm UTC](https://discuss.kotlinlang.org/t/how-to-do-an-operation-on-a-variable-and-assign-it-that-new-value-for-easing-calculations/19621/9 "2020-10-23T16:18:44Z")

</div>

> [@absoluterookie](#):
>
> `(Math.pow(2.0, (-10 * o).toDouble())`

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.

---

<div class="post-metadata">

**Author:** ![absoluterookie](https://avatars.discourse-cdn.com/v4/letter/a/f17d59/32.png) [@absoluterookie](https://discuss.kotlinlang.org/u/absoluterookie)\
**Post date:** [October 23, 2020, 4:45pm UTC](https://discuss.kotlinlang.org/t/how-to-do-an-operation-on-a-variable-and-assign-it-that-new-value-for-easing-calculations/19621/10 "2020-10-23T16:45:32Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![absoluterookie](https://avatars.discourse-cdn.com/v4/letter/a/f17d59/32.png) [@absoluterookie](https://discuss.kotlinlang.org/u/absoluterookie)\
**Post date:** [October 23, 2020, 5:04pm UTC](https://discuss.kotlinlang.org/t/how-to-do-an-operation-on-a-variable-and-assign-it-that-new-value-for-easing-calculations/19621/11 "2020-10-23T17:04:22Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![bjonnh](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/bjonnh/32/8106_2.png) [@bjonnh](https://discuss.kotlinlang.org/u/bjonnh)\
**Post date:** [October 23, 2020, 5:42pm UTC](https://discuss.kotlinlang.org/t/how-to-do-an-operation-on-a-variable-and-assign-it-that-new-value-for-easing-calculations/19621/12 "2020-10-23T17:42:20Z")

</div>

Any reason why you are mixing floats and doubles?

---

<div class="post-metadata">

**Author:** ![absoluterookie](https://avatars.discourse-cdn.com/v4/letter/a/f17d59/32.png) [@absoluterookie](https://discuss.kotlinlang.org/u/absoluterookie)\
**Post date:** [October 23, 2020, 5:44pm UTC](https://discuss.kotlinlang.org/t/how-to-do-an-operation-on-a-variable-and-assign-it-that-new-value-for-easing-calculations/19621/13 "2020-10-23T17:44:21Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![absoluterookie](https://avatars.discourse-cdn.com/v4/letter/a/f17d59/32.png) [@absoluterookie](https://discuss.kotlinlang.org/u/absoluterookie)\
**Post date:** [October 23, 2020, 5:45pm UTC](https://discuss.kotlinlang.org/t/how-to-do-an-operation-on-a-variable-and-assign-it-that-new-value-for-easing-calculations/19621/14 "2020-10-23T17:45:29Z")

</div>

Is there a kotlin equvalent for Math operator -= and /= ?

---

<div class="post-metadata">

**Author:** ![absoluterookie](https://avatars.discourse-cdn.com/v4/letter/a/f17d59/32.png) [@absoluterookie](https://discuss.kotlinlang.org/u/absoluterookie)\
**Post date:** [October 23, 2020, 5:59pm UTC](https://discuss.kotlinlang.org/t/how-to-do-an-operation-on-a-variable-and-assign-it-that-new-value-for-easing-calculations/19621/15 "2020-10-23T17:59:53Z")

</div>

Okay, maybe you guys can help me out a very last time with this

That’s my new function

```auto
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

```auto
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

```auto
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

---

<div class="post-metadata">

**Author:** ![absoluterookie](https://avatars.discourse-cdn.com/v4/letter/a/f17d59/32.png) [@absoluterookie](https://discuss.kotlinlang.org/u/absoluterookie)\
**Post date:** [October 23, 2020, 6:09pm UTC](https://discuss.kotlinlang.org/t/how-to-do-an-operation-on-a-variable-and-assign-it-that-new-value-for-easing-calculations/19621/16 "2020-10-23T18:09:23Z")

</div>

i gave a guess. e is never overwritten. Instead it is counting upwards

---

<div class="post-metadata">

**Author:** ![daveTheCoder](https://avatars.discourse-cdn.com/v4/letter/d/b5e925/32.png) [@daveTheCoder](https://discuss.kotlinlang.org/u/daveTheCoder)\
**Post date:** [October 23, 2020, 6:12pm UTC](https://discuss.kotlinlang.org/t/how-to-do-an-operation-on-a-variable-and-assign-it-that-new-value-for-easing-calculations/19621/17 "2020-10-23T18:12:34Z")

</div>

> 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.

---

<div class="post-metadata">

**Author:** ![absoluterookie](https://avatars.discourse-cdn.com/v4/letter/a/f17d59/32.png) [@absoluterookie](https://discuss.kotlinlang.org/u/absoluterookie)\
**Post date:** [October 23, 2020, 6:17pm UTC](https://discuss.kotlinlang.org/t/how-to-do-an-operation-on-a-variable-and-assign-it-that-new-value-for-easing-calculations/19621/18 "2020-10-23T18:17:40Z")

</div>

This is what i do for the last hours

---

<div class="post-metadata">

**Author:** ![daveTheCoder](https://avatars.discourse-cdn.com/v4/letter/d/b5e925/32.png) [@daveTheCoder](https://discuss.kotlinlang.org/u/daveTheCoder)\
**Post date:** [October 23, 2020, 6:20pm UTC](https://discuss.kotlinlang.org/t/how-to-do-an-operation-on-a-variable-and-assign-it-that-new-value-for-easing-calculations/19621/19 "2020-10-23T18:20:15Z")

</div>

So which expression is yielding the wrong result?

---

<div class="post-metadata">

**Author:** ![absoluterookie](https://avatars.discourse-cdn.com/v4/letter/a/f17d59/32.png) [@absoluterookie](https://discuss.kotlinlang.org/u/absoluterookie)\
**Post date:** [October 23, 2020, 6:23pm UTC](https://discuss.kotlinlang.org/t/how-to-do-an-operation-on-a-variable-and-assign-it-that-new-value-for-easing-calculations/19621/20 "2020-10-23T18:23:57Z")

</div>

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?

[Next page](https://discuss.kotlinlang.org/t/how-to-do-an-operation-on-a-variable-and-assign-it-that-new-value-for-easing-calculations/19621.md?page=2)
