Skip to main content

Pay attention in Kotlin language for Double numbers calculations!


Even if you declare the variable as double, still pay attention for divisions

If you do not cast in every step (especially in division part) then unconsciously you might get wrong calculation

Example1:
var doubleNumber1: Double = 289.19 + (465 / 2)
Ø  521.19

What Kotlin gives is 521.19, what expected is 521.69. As you see in example1 even if you declare the variable name as double in division part it still make wrong calculation.

In division part, even if you cast to toDouble(), there is still wrong calculations.
Example2:
var doubleNumber2: Double = 289.19.toDouble() + (465 / 2).toDouble()
Ø  521.19

If you want to handle this wrong calculation, then casting should be inside of the parenthesis.
Example3:
var doubleNumber3: Double = 289.19.toDouble() + (465 / 2.toDouble())
Ø  521.69

Or you can write numbers with its precision part such as:
var doubleNumber4: Double = 289.19.toDouble() + 232.5
Ø  521.69


Another thing I have faced in Kotlin so far is Binary Floating Point (BFP) Exception. Which means there is a missing part in precision part while converting from binary to decimal in CPU based calculations!

For example;
0.1  + 0.2 = 0.30000000000000004441  

What expected is 0.3 but what returned is 0.30000000000000004441.
There is a standard called IEEE 754 standard.

Comments

Post a Comment

Popular posts from this blog

Vodafone'da Sms Gönderimi için Bakiyeniz Yetersizdir Sorunu Çözüldü

Vodafone'nun sms gönderimi için bakiyeniz yetersizdir Sorunu Çözüldü!.

How to Hack WhatsApp Account Bluestack

How to hack WhatsApp account??? WhatsApp is currently one of the most secure messaging services purely because it works with a particular phone number and requires a certain code.

EXCELde Belli Satirlari ya da Sütunları Dondurmak, Sabitlemek

Excel'de önemli gördüğümüz üst satır -başlık- kısmını veya sol sutunu sabit tutmak, her sayfada görünmesini sağlamak artık çok kolay.

Simply Hacking Facebook Account with Kali Linux

Hi guys, today I will show you how to hack a Facebook account with  Kali Linux.

Polonya'da Erasmus, Szczecin'e Yol Haritasi

#Erasmus için Polonya'nın Szczecin şehrine gidecek arkadaşlar için detaylı #yol #harita sı ve Szczecin hakkında bilmeniz gerekenler... Polonya Szczecin'de Erasmus için Yol Haritasi