Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Min and Max values of Float and Double types in Kotlin

Tags:

kotlin

It's simple to find out what the exact minimum and maximum values for Integer and Long integer are in Kotlin:

Signed 32 bit Integer:

Int.MIN_VALUE       // -2147483648
Int.MAX_VALUE       //  2147483647

Signed 64 bit Integer:

Long.MIN_VALUE      // -9223372036854775808
Long.MAX_VALUE      //  9223372036854775807

But if I try to print Float or Double types' ranges for min and max values I'll get unbalanced numbers.

Signed 32 bit Floating Point Number:

Float.MIN_VALUE     //  1.4E-45
Float.MAX_VALUE     //  3.4028235E38

Signed 64 bit Floating Point Number:

Double.MIN_VALUE    //  4.9E-324
Double.MAX_VALUE    //  1.7976931348623157E308

Why are the positive and negative values in Float and Double types so different?

like image 673
Andy Jazz Avatar asked May 26 '19 18:05

Andy Jazz


People also ask

What is the difference between double and float in Kotlin?

Use Float or Double ? The precision of a floating point value indicates how many digits the value can have after the decimal point. The precision of Float is only six or seven decimal digits, while Double variables have a precision of about 15 digits. Therefore it is safer to use Double for most calculations.

What is the max value of a double?

The value of this constant is positive 1.7976931348623157E+308.


1 Answers

The conceptual definition of MIN_VALUE is different for integers vs floating-point numbers.

  • Int.MIN_VALUE is the largest negative value.
  • Float.MIN_VALUE is the smallest positive value.

In other words, 1.4E-45 is 0.00[40 zeroes]0014, and not a very large negative number. The largest possible negative value is represented by -1 * Float.MAX_VALUE.

like image 98
Ben P. Avatar answered Nov 15 '22 01:11

Ben P.