C++ Question

Discussion in 'Programming General' started by Furnace, Jul 24, 2014.

C++ Question
  1. Unread #1 - Jul 24, 2014 at 2:43 AM
  2. Furnace
    Joined:
    Jul 23, 2014
    Posts:
    11
    Referrals:
    0
    Sythe Gold:
    0

    Furnace Newcomer

    C++ Question

    Recently began my journey into C++, I've been using the book Jumping Into C++ and came across something that I didn't understand.

    Quote from book:
    "A good example of two variables that differ only in the amount of space they require are the double,
    and its lesser twin, the float. The float was actually the original variable type that could store decimal
    numbers, and the name float came from the fact that it has a decimal that can “float” into different
    positions in the number. In other words, you can have two digits before the decimal and four after
    (12.2345), or you can have four digits before the decimal and two after (3421.12). You aren’t limited to a
    specific number of digits before and a specific number of digits after the decimal.

    If you didn’t quite get that, it’s ok—the name is mostly historical. Just know that floating point numbers
    mean “numbers with decimal places”. But floats have only four bytes of space, so they cannot store as
    many different values as doubles, which have eight bytes of space. Back when computers had less
    memory than they do today, this was a bigger deal, and programmers would often go to great lengths to
    save a few bytes. Nowadays, you will almost always be better off using a double, but in cases where
    space is critical (perhaps on low-memory systems like cell phones), you still have the option of using a
    float."

    What does he mean by a float only has 4 bytes? Doesn't a single character take up 1 byte? If someone could explain how memory works in C++, or link me to a page that can explain it, that'd be great.
     
  3. Unread #2 - Jul 24, 2014 at 7:35 AM
  4. SuF
    Joined:
    Jan 21, 2007
    Posts:
    14,212
    Referrals:
    28
    Sythe Gold:
    1,234
    Discord Unique ID:
    203283096668340224
    <3 n4n0 Two Factor Authentication User Community Participant Spam Forum Participant Sythe's 10th Anniversary

    SuF Legend
    Pirate Retired Global Moderator

    C++ Question

    A single ASCII character does take up 1 byte. A float does not represent a character, however. It represents a decimal number like 6.345. Baked into the hardware is support for an encoding strategy to make the bits represent decimals as efficiently as possible. The precision of the decimal (number of digits to the right of the decimal place) is limited by the number of bits that are available.

    So the float data type uses 4 bytes meaning it has 32 bits to encode your number. A double is what they call a floating point with 64 bits (double the number of the float). Since you now have more bits you can be much more precise in your decimal (more digits to the right of the decimal place).

    Does that answer your question?
     
  5. Unread #3 - Jul 24, 2014 at 8:26 PM
  6. kmjt
    Joined:
    Aug 21, 2009
    Posts:
    14,450
    Referrals:
    8
    Sythe Gold:
    449

    kmjt -.- The nocturnal life chose me -.-
    Banned

    C++ Question

    You are confusing the primitive types. A character is simply not the same as a float or double. Every primitive can take up a variable amount of bytes in memory. All of the primitive types are efficiently applicable in different situations. For example, if you know that an integer number in your program will never exceed 32767 and will never be below -32768 (the highest and lowest value a short int can be respectively), you are better off to use a short int (2 bytes) instead of a regular integer (4 bytes). As your textbook says this is not that important in small programs, but imagine if you are dealing with a program that processes millions of variables. You would definitely be better off to use the appropriate data type. In case you didn't know, a program will usually crash when memory is exhausted (and this can be done by using too many bytes).

    This page might help you out:
    http://www.tutorialspoint.com/cplusplus/cpp_data_types.htm
     
  7. Unread #4 - Jul 24, 2014 at 8:50 PM
  8. Furnace
    Joined:
    Jul 23, 2014
    Posts:
    11
    Referrals:
    0
    Sythe Gold:
    0

    Furnace Newcomer

    C++ Question

    Thank-you both very much for the information, it was quite helpful.

    I have on last question, how are the minimum and maximum values for floats and doubles derived?
     
  9. Unread #5 - Jul 24, 2014 at 9:25 PM
  10. kmjt
    Joined:
    Aug 21, 2009
    Posts:
    14,450
    Referrals:
    8
    Sythe Gold:
    449

    kmjt -.- The nocturnal life chose me -.-
    Banned

    C++ Question



    This might help you:
    http://www.youtube.com/watch?v=n-XozGu1viM
     
  11. Unread #6 - Jul 26, 2014 at 4:25 PM
  12. SuF
    Joined:
    Jan 21, 2007
    Posts:
    14,212
    Referrals:
    28
    Sythe Gold:
    1,234
    Discord Unique ID:
    203283096668340224
    <3 n4n0 Two Factor Authentication User Community Participant Spam Forum Participant Sythe's 10th Anniversary

    SuF Legend
    Pirate Retired Global Moderator

    C++ Question

    For floats and doubles it is very tricky. There is a standard for how they get encoded in zeros and ones and they can hold very very small values, but not all very very small values without losing accuracy. You can google for the standard if you want but its just some math and stuff based on the standard. I had to do it in computer architecture and it was not all that enjoyable.
     
< Android Programming Issue - checkedId | How to Prevent Windows From Restarting Without Your Permission >

Users viewing this thread
1 guest


 
 
Adblock breaks this site