Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Long Integer and Float

If a Long Integer and a float both take 4 bytes to store in memory then why are their ranges different?

like image 861
Dinesh Avatar asked Jan 28 '10 12:01

Dinesh


People also ask

Is long and float the same?

They have different sizes and precision. Basically all of them represent the decimal values such as 3.14 The main difference between them is that in float we can store values upto 4 bytes (6 places after decimal point) Double upto 8 bytes And long double even more than float and double.

What is difference between integer and float?

Integers and floats are two different kinds of numerical data. An integer (more commonly called an int) is a number without a decimal point. A float is a floating-point number, which means it is a number that has a decimal place. Floats are used when more precision is needed.

What is long and float?

The long float is a K&R C first edition type that existed. It is synonymous with double . After the first standard C89/C90, long float is removed. It is not deprecated.


3 Answers

Integers are stored like this:

  • 1 bit for the sign (+/-)
  • 31 bits for the value.

Floats are stored differently, giving greater range at the expense of accuracy:

  • 1 bit for the sign (+/-)
  • N bits for the mantissa S
  • M bits for the exponent E

Float is represented in the exponential form: (+/-)S*(base)^E

BTW, "long" isn't always 32 bits. See this article.

like image 142
mingos Avatar answered Oct 05 '22 05:10

mingos


Different way to encode your numbers.

Long counts up from 1 to 2^(4*8).

Float uses only 23 of the 32 bits for the "counting". But it adds "range" with an exponent in the other bits. So you have bigger numbers, but they are less accurate (in the lower based parts):

1.2424 * 10^54 (mantisse * exponent) is certainly bigger than 2^32. But you can discern a long 2^31 from a long 2^31-1 whereas you can't discern a float 1.24 * 10^54 and a float 1.24 * 10^54 - 1: the 1 just is lost in this representation as float.

like image 37
Leonidas Avatar answered Oct 05 '22 04:10

Leonidas


They are not always the same size. But even when they are, their ranges are different because they serve different purposes. One is for integers with no decimal places, and one is for decimals.

like image 36
SoapBox Avatar answered Oct 05 '22 05:10

SoapBox