Win a copy of Functional Reactive Programming this week in the Other Languages forum!

# Data range of float and double types

vatsalya rao
Ranch Hand
Posts: 63
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?

Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15484
43
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 ]

Campbell Ritchie
Sheriff
Posts: 50217
79
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".

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 184ff).

vatsalya rao
Ranch Hand
Posts: 63
Thanks lot for both of you for explaining

in such a detailed manner.

Campbell Ritchie
Sheriff
Posts: 50217
79
You're welcome, but I don't think we explained a lot. I think there are two lectures in our Principles of Computing course about floating-point arithmetic and numbers, so there is a lot to the subject.