• Log in or sign up for an account here: https://www.sythe.org/login

C++ Question

Furnace

Newcomer
Joined
Jul 23, 2014
Sythe Gold
0
Messages
11
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 n4n0
Discord Verified
Two Factor Authentication User
Community Participant
Spam Forum Participant
Sythe's 10th Anniversary
Sythe Awards 2011 Winner
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?
 
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
 
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?
 
<3 n4n0
Discord Verified
Two Factor Authentication User
Community Participant
Spam Forum Participant
Sythe's 10th Anniversary
Sythe Awards 2011 Winner
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?

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.
 
Runescape Gamblingrunescape gambling
Back
Top