Thread: A Question About Floating Numbers

  1. #1
    Registered User
    Join Date
    Nov 2010
    Posts
    9

    Question 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. #2
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    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. #3
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    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. #4
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    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. #5
    Anti-Poster
    Join Date
    Feb 2002
    Posts
    1,401
    Quote Originally Posted by Adak View Post
    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.
    Quote Originally Posted by bthomson900 View Post
    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.
    If I did your homework for you, then you might pass your class without learning how to write a program like this. Then you might graduate and get your degree without learning how to write a program like this. You might become a professional programmer without knowing how to write a program like this. Someday you might work on a project with me without knowing how to write a program like this. Then I would have to do you serious bodily harm. - Jack Klein

  6. #6
    Registered User
    Join Date
    Nov 2010
    Posts
    9
    Thanks heaps. It makes much more sense to me now.

  7. #7
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    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. #8
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by pianorain View Post
    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.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  9. #9
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote 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.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  10. #10
    Anti-Poster
    Join Date
    Feb 2002
    Posts
    1,401
    Quote Originally Posted by Adak View Post
    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.
    If I did your homework for you, then you might pass your class without learning how to write a program like this. Then you might graduate and get your degree without learning how to write a program like this. You might become a professional programmer without knowing how to write a program like this. Someday you might work on a project with me without knowing how to write a program like this. Then I would have to do you serious bodily harm. - Jack Klein

  11. #11
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    Quote Originally Posted by grumpy View Post
    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 subscribe to a feed

Similar Threads

  1. Multiple classes question
    By TriKri in forum C++ Programming
    Replies: 20
    Last Post: 06-11-2010, 04:03 PM
  2. Replies: 2
    Last Post: 04-24-2010, 11:06 PM
  3. Replies: 5
    Last Post: 10-08-2004, 05:30 AM
  4. floating point question
    By Eric Cheong in forum C Programming
    Replies: 8
    Last Post: 09-10-2004, 10:48 PM
  5. Floating point numbers
    By noor_mirza in forum C++ Programming
    Replies: 3
    Last Post: 10-23-2002, 03:40 AM

Tags for this Thread