# Data range of float and double types

vatsalya rao

Ranch Hand

Posts: 63

posted 9 years ago

I recently started to go thru Kathy and Sierra book.

In the 10th page of the book it is mentioned that its compicated\

to determine the data range of float and double data types.

My question is we know that float occupies 32 bits and double

occupies 64 bit.

Why cant we mention that float data range is -2 power 31 ro 2 power 31 -1 and same with double?

In the 10th page of the book it is mentioned that its compicated\

to determine the data range of float and double data types.

My question is we know that float occupies 32 bits and double

occupies 64 bit.

Why cant we mention that float data range is -2 power 31 ro 2 power 31 -1 and same with double?

posted 9 years ago

For an N-bit signed integer stored in two's complement, the range is -(2^(N-1)) to 2^(N-1) - 1.

But floating point numbers (in the types float and double) are not stored the same way as integers; they are stored in the IEEE 754 format. See that page for details on the layout of float and double in memory.

[ July 13, 2007: Message edited by: Jesper Young ]

But floating point numbers (in the types float and double) are not stored the same way as integers; they are stored in the IEEE 754 format. See that page for details on the layout of float and double in memory.

[ July 13, 2007: Message edited by: Jesper Young ]

Campbell Ritchie

Sheriff

Posts: 49411

62

posted 9 years ago

That's not float range, but int.

Float runs from (1 - 2^-24) * 2 ^128 to 2^-126 with a subnormal range down to 2^149.

The first bit is the sign bit.

The next 8 bits are the exponent, in binary, but biased by 127, so X100_0000_0 means an exponent of +1, but in binary.

The remaining 23 bits represent the fractional part of the mantissa, with an imaginary 1 before the radix point. In the case of subnormal numbers, the exponent is X000_0000_0 which is equivalent to -126 and the mantissa is the remaining 23 digits as a fraction following an imaginary 0 before the radix point. As the mantissa gets smaller, the precision does too, until 0000_0000_0000_0000_0000_0000_0000_0001 is half of 0000_0000_0000_0000_0000_0000_0000_0010 and the next smaller value (X000_0000_0000_0000_0000_0000_0000_0000) means plus zero or minus zero.

If the exponent is X111_1111_1 and all the other figures are zero, then you have infinity (or minus infinity).

If the exponent is X111_1111_1 and there is a 1 anywhere else, you have "NaN".

Bet you wish you had never asked, now.

If you really want to know, you need to find a Computer Technologies course, which we have here as a module in the Computing BSc for freshers. There is an article in Wikipedia, but I don't think it makes it much clearer. There are bits in the computer hardware books (eg Alan Clements, Principles of Computer Hardware, 3/e, Oxford: OUP [2000], pages 184

Float runs from (1 - 2^-24) * 2 ^128 to 2^-126 with a subnormal range down to 2^149.

The first bit is the sign bit.

The next 8 bits are the exponent, in binary, but biased by 127, so X100_0000_0 means an exponent of +1, but in binary.

The remaining 23 bits represent the fractional part of the mantissa, with an imaginary 1 before the radix point. In the case of subnormal numbers, the exponent is X000_0000_0 which is equivalent to -126 and the mantissa is the remaining 23 digits as a fraction following an imaginary 0 before the radix point. As the mantissa gets smaller, the precision does too, until 0000_0000_0000_0000_0000_0000_0000_0001 is half of 0000_0000_0000_0000_0000_0000_0000_0010 and the next smaller value (X000_0000_0000_0000_0000_0000_0000_0000) means plus zero or minus zero.

If the exponent is X111_1111_1 and all the other figures are zero, then you have infinity (or minus infinity).

If the exponent is X111_1111_1 and there is a 1 anywhere else, you have "NaN".

Bet you wish you had never asked, now.

If you really want to know, you need to find a Computer Technologies course, which we have here as a module in the Computing BSc for freshers. There is an article in Wikipedia, but I don't think it makes it much clearer. There are bits in the computer hardware books (eg Alan Clements, Principles of Computer Hardware, 3/e, Oxford: OUP [2000], pages 184

*ff*).
vatsalya rao

Ranch Hand

Posts: 63