Thread: Why use float?

  1. #1
    For Narnia! Sentral's Avatar
    Join Date
    May 2005
    Location
    Narnia
    Posts
    719

    Why use float?

    Hello, I have come across a problem. I don't understand why someone would use float nowadays. Float is only accurate up to about 6 numbers, but uses less memory. Wouldn't someone rather use double, it has twice the accuracy, but uses more memory. Would this be a problem if you were holding alot of large nmbers, or will todays processors hold up?
    Videogame Memories!
    A site dedicated to keeping videogame memories alive!

    http://www.videogamememories.com/
    Share your experiences with us now!

    "We will game forever!"

  2. #2
    Registered User
    Join Date
    Nov 2001
    Posts
    1,348
    You said it. Memory usage is key, so save the bits.

    Kuphryn

  3. #3
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    Well, perhaps you don't need the same precision (or don't have very precise data), but you need a very sizable number... then memory becomes an issue.

    Not really an issue for most applications, though.
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

  4. #4
    For Narnia! Sentral's Avatar
    Join Date
    May 2005
    Location
    Narnia
    Posts
    719
    OK. Thanks.
    Videogame Memories!
    A site dedicated to keeping videogame memories alive!

    http://www.videogamememories.com/
    Share your experiences with us now!

    "We will game forever!"

  5. #5
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    You should always use double unless you specifically need the memory savings and don't need the precision - double is the default floating point type in C++. It is the same reason you should use int instead of short. float would be better named short double.

  6. #6
    For Narnia! Sentral's Avatar
    Join Date
    May 2005
    Location
    Narnia
    Posts
    719
    Would default int be considered long? In other words would I have to do long int for it to be long , or is int automatically long?
    Videogame Memories!
    A site dedicated to keeping videogame memories alive!

    http://www.videogamememories.com/
    Share your experiences with us now!

    "We will game forever!"

  7. #7
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    In terms of size, you have the following guarantee:
    short <= int <= long

    On most common (new) systems, with most compilers I have run across (nowhere near exhaustive), int and long are the same size.
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

  8. #8
    Hardware Engineer
    Join Date
    Sep 2001
    Posts
    1,398
    An int is the same as a short. If you want a long, you have to specify long.

    [EDIT]-------------------------
    But like Zach says, a long and short can be the same size.
    Last edited by DougDbug; 07-14-2005 at 07:41 PM.

  9. #9
    For Narnia! Sentral's Avatar
    Join Date
    May 2005
    Location
    Narnia
    Posts
    719
    Ok.Cooool.
    Videogame Memories!
    A site dedicated to keeping videogame memories alive!

    http://www.videogamememories.com/
    Share your experiences with us now!

    "We will game forever!"

  10. #10
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    >> An int is the same as a short. If you want a long, you have to specify long.

    This is not correct. There are three sizes of integers, short int (i.e. short), int, and long int (i.e. long). Zach L.'s size display is correct.

  11. #11
    ---
    Join Date
    May 2004
    Posts
    1,379
    I thought that the size of an int was implementation specific.
    two sizes, short and long. int could be either one of them

    Code:
    #include <iostream>
    
    int main(int argc, char *argv[])
    {
      std::cout << sizeof(short) << std::endl;
      std::cout << sizeof(int) << std::endl;
      std::cout << sizeof(long) << std::endl;
    
      return 0;
    }
    
    /*My output
    * 2
    * 4
    */4

  12. #12
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    There are three potential sizes, and they are implementation specific. They could all three be the same, or all three be different, or two the same and one different. On a 64 bit machine you might have 2, 4, 8. On another machine you might have 2, 2, 2. The rule is short <= int <= long.

  13. #13
    Dump Truck Internet valis's Avatar
    Join Date
    Jul 2005
    Posts
    357
    I've never really liked the C types, I see why they don't use the 'standard' names as those are usually architecturally specific.
    If your compiling for IA-32 you can almost be guaranteed an int is a (IA-32) double word, a long is a double word, a short is a word, and a char is a byte and a long long or long int is a quad word. I like microsofts __int8 __int16 __int32 and __int64 types because they are more meaningful to me.

  14. #14
    myNegReal
    Join Date
    Jun 2005
    Posts
    100
    Double is more used but float is a lot better for something like a game to save memory.

  15. #15
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    Quote Originally Posted by valis
    I've never really liked the C types, I see why they don't use the 'standard' names as those are usually architecturally specific.
    If your compiling for IA-32 you can almost be guaranteed an int is a (IA-32) double word, a long is a double word, a short is a word, and a char is a byte and a long long or long int is a quad word. I like microsofts __int8 __int16 __int32 and __int64 types because they are more meaningful to me.
    because they're architecturally specific.

    Standard C/C++ is designed for use on as many platforms as possible. that's why you can port a standard C program back and forth from linux to windows without a problem. The standards leave all the architecture-specific things to the compiler, and sets guidelines for the compiler to follow. that's why the sizes are defined in relation to eachother.
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

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, 02: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, 05:26 PM