Kotlin , code enters if statemant even when condition is false

Issue

I’m really confused here … looks like a really silly mistake but I don’t know what’s happening. Here’s little snippet of my code :

if (tempDeltaDeviation > standardDeltaDeviation) {
                        Log.e(TAG, "handleMessage: plus $tempDeltaDeviation : $standardDeltaDeviation")
                        scaleUpAnimation(deltaAnimStep, "Delta", binding.deltaImg)
}

Really basic stuff, right ? Well checking the logs I can see :

handleMessage: plus 1.57756888292539E14 : 7.8364593205657E13

Dunno but last time I was checking 1 was much smaller than 7 so why app enters if statement ?

Solution

1.57756888292539E14 is in fact larger than 7.8364593205657E13.

These numbers are represented in scientific notation which is used to work with very small or very large numbers. 1.57756888292539E14 means: 1.57756888292539 * 10^14. By increasing the number after E by one we actually increase the resulting number 10 times. By increasing it by 6, we increase the resulting number million times (10^6 1000000).

Making things simple, your numbers are really:

  • 157756888292539 (1.57756888292539 * 100000000000000)
  • 78364593205657 (7.8364593205657 * 10000000000000)

As you can see, the first number is actually bigger.

Answered By – broot

Leave a Comment