nroux
nroux

Reputation: 11

confusion with float datatype

I understand the integer range but i cannot know how float works

Range 3.4 e-38 to 3.4 e+38 (7 significant digits)

The range of float would be 0.000000000000000000000000000000000000034 to 340000000000000000000000000000000000000

how are 7 significant digits calculated?

Upvotes: 1

Views: 2484

Answers (8)

Carl Norum
Carl Norum

Reputation: 225232

The significand of an IEEE754 single-precision floating point number is 23 bits in length. That means you have 24 binary digits of precision (23 significand bits plus the implied leading 1). Converting 24 binary digits (224 total possible numbers) to an equivalent number of decimal digits:

log10(224) = 7.2247199

Rounding that result down yields 7 decimal digits of precision.

To compare, a double-precision floating point number has a 52 bit significand. That means 53 binary digits of precision. In base 10:

log10(253) = 15.9545898

Or roughly 16 decimal digits of precision.

Upvotes: 4

ilmale
ilmale

Reputation: 406

Float have: 1 bit for the sign (1 negative numbers, 0 positive numbers) 8 bit of the exponent represented as unsigned byte shifted by -127 (0x00 and 0xFF are special cases) 23 for mantissa were each bit of the mantissa count as 2^(-bitIndex) so the value of the bit 23 is 0.5, the bit 22 is 0.25 the bit 21 is 0.125 and so on

the final number value is (1+ mantissa)*(2^exponent)

so if you want to represent 0.1015625 you must decompose the number in mantissa and exponent to do that you have to find the fist power of two that multiplied your number give a number in the form 1.something in this case 16. 0.1015625 * 16 = 1.625. So the exponent is 16. 16 + 127 is 10001111 Now we have to represent 0.625 in the mantissa. 0.625 is 0.5 + 0.125 so our number is 0 10001111 1010000000000000000

in this case we are lucky and the number is precise. If you try to encode a number like 0.12 you will find that you only get something like 0.1199999(random_number) so the 7 digit precision is only a warning to be considered is you are doing computation with very close numbers. You also have to consider special cases, a number with the exponent with all ones and mantissa all zeros represent infinite. Number with exponent with all ones and mantissa not zero don't represent a number NaN.

Upvotes: 2

David Hammen
David Hammen

Reputation: 33136

How are 7 significant digits calculated?

Look how you wrote that range, nroux: 3.4e-38 to 3.4e+38. You used scientific notation to do that, splitting each number into a significand (3.4) and an exponent (-38 or +38). Computers do essentially the same thing for floating point numbers, storing the significand and the exponent as two separate, fixed-size parts of the chunk of memory that represents a floating point number.

Upvotes: 0

dan04
dan04

Reputation: 91217

Floating-point numbers are stored in a form of scientific notation, but in binary. So, for example,

  • 2011 = binary 11111011011 = 1.1111011011 × 210
  • 0.75 = binary 0.11 = 1.1 × 2-1

In IEEE 754, (single-precision) float has 24 significant binary digits, which can represent the same amount of information as 24 log10 2 ≈ 7.22 decimal digits.

Upvotes: 0

Jon
Jon

Reputation: 1

In C++, a float is a floating point integer represented in 32 bits using the IEEE 754 format. In this format, 1 bit is used for the sign (positive or negative), 8 bits are used for the exponent, and the significand is contained in 24 bits but only 23 of them are explicitly stored due to the encoding of the exponent. The significand encodes the significant digits. With 24 bits of storage, the float value can have around 7 significant digits.

See http://en.wikipedia.org/wiki/Single_precision_floating-point_format for more information on how a float is represented by a computer.

Upvotes: 0

trutheality
trutheality

Reputation: 23455

A float is made up of two parts: the "significand" s between 0 and 1 which is about 7 decimal digits, and the exponent e that gives you the power of two by which it is multiplied.

Representing (1+s)*2e

Upvotes: 1

Yuri
Yuri

Reputation: 81

0.00000000000000001234567 and 1234567000000000000000000 but not 123415678000

Upvotes: 2

Andrew Cooper
Andrew Cooper

Reputation: 32596

The 7 digits refers to the precision of representing a number.

For example 123000000000 can be represented because there's only 3 significant digits of precision (1.23 x 10^11). But 123000000010 can't be represented because that involves 11 digits of precision (1.2300000001 x 10^11).

Upvotes: 3

Related Questions