Thread: Basic question on floats and double

  1. #1
    Registered User
    Join Date
    Sep 2011
    Posts
    4

    Basic question on floats and double

    My text book doesn't go into much detail on floats and doubles. I know what a float is. I've read some wiki's on doubles but I'm still a bit confused, and have a couple related questions, that are not in my textbook.

    1. What is the difference between a float and a double? What are the advantages/disadvantages of using either.

    2. what is the difference between using %lf and %f ?

    Thanks

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    > 1. What is the difference between a float and a double? What are the advantages/disadvantages of using either.
    Doubles have more digits of precision, and a larger range of exponents.
    Short answer, you can store larger numbers with more accuracy.

    > 2. what is the difference between using %lf and %f ?
    You have to use the right one when using scanf to read floats or doubles.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    The difference is the precision, a double has a larger mantissa (actually larger than double the size). In a float it's 23 bits and in a double it's 52 bits. The range is a bit larger than that since the signed bit is not part of the mantissa and it's normalized as well.

    The format specifiers are dedicated to the each type, so %f is for float and %lf is for doubles.

    Double precision floating-point format - Wikipedia, the free encyclopedia

  4. #4
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,949
    Quote Originally Posted by Bobaflex View Post
    1. What is the difference between a float and a double? What are the advantages/disadvantages of using either.
    The difference has to do with precision. Read What every computer scientist should know about floating point arithmetic. Specifically look for the section entitled "Precision".

    Quote Originally Posted by Bobaflex View Post
    2. what is the difference between using %lf and %f ?
    For printf nothing. For scanf and family it specifies whether or not you are reading in a double "%lf" or a float "%f".
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Double and Floats
    By a.mlw.walker in forum C Programming
    Replies: 12
    Last Post: 08-15-2011, 12:13 PM
  2. Question Functions (Integers & Floats)
    By mwong in forum C Programming
    Replies: 3
    Last Post: 04-12-2011, 02:42 AM
  3. Question about strings and floats
    By shiroaisu in forum C++ Programming
    Replies: 3
    Last Post: 07-27-2010, 08:12 AM
  4. Double's and floats, i need their bytes.
    By Florian in forum C++ Programming
    Replies: 26
    Last Post: 07-08-2008, 05:42 PM
  5. question: how to divide two floats
    By mkappel in forum C++ Programming
    Replies: 1
    Last Post: 07-13-2002, 04:13 PM