# Thread: A Question About Floating Numbers

1. ## A Question About Floating Numbers

It is stated in my lecture notes that float is to 7 significant figures.

However, if I was to do the following in a code:

float a;
a = 123.456;
printf("a = %f", a);

then why does it print the following?

123.456000

I ask this because 123.456 is already 7 significant figures.

It just seems strange, as I could enter a number like 1234567.123 and its output will be 1234567.123000 (i.e. it will print the integer and six digits after the decimal place).

So I am just wondering exactly why my lecture notes define float as having 7 sig. figs. 2. I guess it refers to the precision of floats, and the number of bits in the mantissa, which should be 23 (24 really) but I guess saying that it's 7 digits base ten is safe.

Single precision floating-point format - Wikipedia, the free encyclopedia 3. This is an issue of floating point storage vs. representation in output.

A 32-bit float can store up to (roughly) 7 significant decimal digits, because log10(2^24) is roughly 7, but you can print it with however many digits you want, including a bunch of not-actually-significant trailing zeros. Interestingly enough, not all numbers that can be represented perfectly in a decimal system can be represented perfectly in a binary system, but that's some fun number theory for another discussion. 4. Significant figures refers to the range of the float after the decimal point, not the number of digits it can hold.

Floating point numbers are composed of a sign bit, an exponent that is an int, and then a number of significant digits bits, which represent numbers after the decimal point.
Code:
```s exponent.significant figures
1       4          7```
To see it better go here:
YouTube - Lecture 2 | Programming Paradigms (Stanford)

and move the player forward to 38:00 minutes. The prof draws up on the chalkboard, and describes, each part of a floating point number. 5. Originally Posted by Adak Significant figures refers to the range of the float after the decimal point, not the number of digits it can hold.

Floating point numbers are composed of a sign bit, an exponent that is an int, and then a number of significant digits bits, which represent numbers after the decimal point.
Everything the prof says in the video is correct, but your explanation of it is wrong. Take a good look at the formula the prof writes on the board explaining how the parts of a single-precision float make up a number. The fraction part (or ".xxxx", as he puts it) is not just the digits after the decimal point of a floating number. It is, in effect, all the significant digits. The rest of the bits are just to scale the number up or down. Originally Posted by bthomson900 So I am just wondering exactly why my lecture notes define float as having 7 sig. figs.
The reason has to do with the length of the fraction part. The fraction part of a single-precision float is 23 bits long, but there's an implied 1 at the beginning, so it's really 24 bits long. The calculation to find how many significant figures there are is log10(2^24), which is a little more than 7. 6. Thanks heaps. It makes much more sense to me now. 7. When I mentioned that the format has 1 sign bit, 1 exponent (an int), and 7 significant digits after the decimal point, I'm repeating what the professor was teaching, minus the details he spent several minutes and a chalkboard, of drawings, to elaborate on.

So now you're back saying I'm wrong, and a floating point number has 1 sign bit, 1 exponent (an int - which oddly, acts just like an exponent!), and about 7 significant digits.

Yeah, I can see I'm just crazy wrong on this!    8. Originally Posted by pianorain The reason has to do with the length of the fraction part. The fraction part of a single-precision float is 23 bits long, but there's an implied 1 at the beginning, so it's really 24 bits long.
If that was true, it would suggest a value of zero cannot be represented in floating point. 9. Originally Posted by grumpy
If that was true, it would suggest a value of zero cannot be represented in floating point.
I recall that (as in the existence of a leading 1) to be true, yet a value of zero can be represented in this floating point representation. This article on The IEEE standard for floating point arithmetic states that zero is represented by the exponent and fractional portion being entirely zero, despite this implied leading 1. 10. Originally Posted by Adak Yeah, I can see I'm just crazy wrong on this!   Ha. After re-reading your post without the first sentence, it sounds right. I think just the wording of the first sentence made me think you were going down the wrong path. My bad. 11. Originally Posted by grumpy If that was true, it would suggest a value of zero cannot be represented in floating point.
That's true only for Normalized numbers; all others i.e. plus/minus zero, infinity, NANs and denormalized nos. have a leading 0 instead of 1. Popular pages Recent additions #### Tags for this Thread

define, float, floating, numbers 