Win a copy of Java EE 8 High Performance this week in the Java/Jakarta EE forum!
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
Sheriffs:
Saloon Keepers:
Bartenders:

# Data range of float and double types

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?

Java Cowboy
Sheriff
Posts: 16080
88
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 ]

Marshal
Posts: 57499
175
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
Marshal
Posts: 57499
175
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.

 A teeny tiny vulgar attempt to get you to buy our stuff The WEB SERVICES and JAX-RS Course https://coderanch.com/t/690789/WEB-SERVICES-JAX-RS