float

This is a discussion on float within the C++ Programming forums, part of the General Programming Boards category; Good day A simple question, whats the difference between : Code: float number = 12.34f; and Code: float number = ...

  1. #1
    Registered User
    Join Date
    Oct 2002
    Posts
    291

    float

    Good day

    A simple question, whats the difference between :

    Code:
    float number = 12.34f;
    and

    Code:
    float number = 12.34;

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >float number = 12.34f;
    This assigns a single precision value to a single precision variable.

    >float number = 12.34;
    This assigns a double precision value to a single precision variable, your compiler may warn about this.

    -Prelude
    My best code is written with the delete key.

  3. #3
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    that then begs the question, What's the difference between a single precision variable and a double precision variable?

  4. #4
    Registered User
    Join Date
    Jul 2002
    Posts
    66
    A double precision variable has twice the precision of a single precision variable. Isn't that kind of obvious?

  5. #5
    Programming Sex-God Polymorphic OOP's Avatar
    Join Date
    Nov 2002
    Posts
    1,078
    Actually, double doesn't necissarily mean twice the precission.

  6. #6
    Registered User
    Join Date
    Jul 2002
    Posts
    66
    Yea, but you can't quantify exactly what the difference is, so twice as much as as good as any for a "Duh" question like that.

  7. #7
    Its not rocket science vasanth's Avatar
    Join Date
    Jan 2002
    Posts
    1,683
    consider a number 3.14285

    If you store it in a variable do you want to save it as 3.14 or 3.142.

  8. #8
    Registered User
    Join Date
    Oct 2002
    Posts
    291
    According to my c++ book float uses 7 digits precision and a total of 4 bytes. Does that mean if I write
    Code:
    float number = 12.34;
    it uses 7 digits precision and if I write
    Code:
    float number = 12.34f;
    it uses 14 digits precision ?

  9. #9
    Programming Sex-God Polymorphic OOP's Avatar
    Join Date
    Nov 2002
    Posts
    1,078
    Originally posted by laasunde
    According to my c++ book float uses 7 digits precision and a total of 4 bytes. Does that mean if I write
    Code:
    float number = 12.34;
    it uses 7 digits precision and if I write
    Code:
    float number = 12.34f;
    it uses 14 digits precision ?
    No, the former has more precision than the latter.

  10. #10
    Registered User
    Join Date
    Oct 2002
    Posts
    291
    Originally posted by Polymorphic OOP
    No, the former has more precision than the latter.
    So this is correct then

    float number = 12.34; // 14 digits precision

    float number = 12.34f; // 7 digits precision

    Both using 4 bytes of memory.

  11. #11
    Programming Sex-God Polymorphic OOP's Avatar
    Join Date
    Nov 2002
    Posts
    1,078
    Nono, float is usually 4 bytes and double is usually 8 bytes. Notice it's usually -- the standard only says that double has to be greater than or equal to the size and precission of float.

    4-byte float is generally accurate to 23 binary digits and 8-byte double is generally accurate to 52 binary digits.
    Last edited by Polymorphic OOP; 12-30-2002 at 06:59 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 05-13-2009, 03:25 PM
  2. Replies: 14
    Last Post: 06-28-2006, 01:58 AM
  3. Could somebody please help me with this C program
    By brett73 in forum C Programming
    Replies: 6
    Last Post: 11-25-2004, 01:19 AM
  4. Half-life SDK, where are the constants?
    By bennyandthejets in forum Game Programming
    Replies: 29
    Last Post: 08-25-2003, 11:58 AM
  5. How do you search & sort an array?
    By sketchit in forum C Programming
    Replies: 30
    Last Post: 11-03-2001, 04:26 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21