1. A quiz from site, "convert the following numbers to C++ style scientific notation. Using an e to represent the exponent, and determine how many significant digits each has. Keep trailing zeros after the decimal.
float f;
f = 34.5e2f;
std::cout << f << std::endl;
f = 0.004000;
std::cout << f << std::endl;
f = 123.005;
std::cout << f << std::endl;
return 0;
actually 2nd one make more sense.
2. Could I have more explaining and example of forward declarations.
What exact purpose does it do. I mean why don't I just do int main first then put everything below it as normally. I could think of, if computer have to work on line before getting to return. Is it why most of function are in front of mint main? Then using void, return true to avoid end the code premature?
3. A guide explaining about int8_t signed or unsigned. Tho after reading, it mention I will never use it in the basic tutorial. The example it show are mostly telling how big itself is. Which doesnt help me with example.
it kept showing
std::int16_t i(5);
std::cout << i;
return 0;
4. Using fast and least type, what example would it be if I needed to use int_fast#_t. I would love to see few lines that would use it to do something.
the fact if I'm using large byte would cause overflow. I haven't seen anything in tutorial would cause it. Anybody got example that I'll have to use the int8, 16_t etc.
sorry about bunches of question. My brain are really bad at reading and convert into my brain. I really relying on example and where the line represent to and such.
guessing what the poorly worded #1 wants, I would say they just want you to write
4.0e-3 or 1.23005e2
and sig digits is like you learned in science class etc, just how many digits the value has.
123005 is 6 digits, no matter where the decimal is; its the count of values between zeros on left and right (visible or implied zeros).
2) google it, but a forward declare is simply to tell the compiler that something is coming and this is what it is.. its like a function prototype for classes, a placeholder to say what will be seen later to enable the compiler to continue. (Function prototypes may actually BE a forward declare or a type of it; I am not 100% up on the semantics of the term).
this literally looks like this for a class:
class a; //forward declare: tells compiler that a class named a will exist.
class b
{
a x; //uses class a, but a does not yet exist, compiler is ok.
};
class a //and here it is, as promised...
{
};
the use of this is more obvious if a had a b, and b had an a, you can't code that so that they are both seen before the other one, so you need the declare...
3) int 8 is an 8 bit integer, 1 byte, it is identical to char for normal discussions.
an 8 bit value can hold 2 to the 8 distinct values, 0 to 255 or -127 to 128 including zero. That is the overview, is this what you asked?
4) fast type is a hardware thing. some hardware is faster to use a larger value, because of bus sizes inside the machine or how its machine language works or similar reasons. so lets say that on YOUR cpu, 32 bit integers are faster than 16 bit integers. If you ask for a 16 bit integer (to store that much data) but specify fast, it will use the 32 which is plenty to hold 16, its wasteful, but its faster. Least is the opposite, and uses the smallest size that it can for the data you need to store.
examples?
if you had a file, and wanted to process it as raw bytes, then you might use a vector of uint8_t types. Every byte of the file can be touched this way, easily. If you had a bunch of factorials, you might use uint64_t so you have a large value and unsigned because you know you don't need negatives. If you just want a loop counter, maybe you will use int, because its good enough for general purpose use. If you were reading stream of data from a network socket, and the format is 5 bytes of header followed by a 16 bit integer code followed by other junk, you would use int16_t for that second field, and maybe int8_t for the first 5 fields. And so on. The sizes matter a lot if you are reading a file in binary mode, where you grab a bunch of bytes off the disk and shovel them into a data structure directly, if the sizes are mismatched it will garble the data, but if they are correct, it is extremely efficient, and when you write it back out, its the right size so you can do it as a block instead of part by part.
Sizing ints .... is something you do when you NEED to do so, and marking signed and unsigned is something you do when you want to self document (hey reader, I know this one is never negative!) and when the math requires it (you want to store the value 60000 in 16 bits, it won't fit in a signed 16). General programming, you will want to use auto or int 90% or more of the time. These other guys are for when that is insufficient, and I hate to say it, but experience and debugging and practice is the way to know when int or auto is not good enough.
Basically I have to just wait until I got my first error of overflow or breaking point. Then I'll see to look back in chapter and add in different size, unid and such. Finally repeat it until I memorized all those painful overflow.
Seem most of those are not include this tutorial, which mean I need to finish this basic tutorial of c++ and move on to different major?
I got no goal as of right now beside finishing this course of self taught.
_________________________________________________________
Question:
1. Double and Float are basically same, but different are byte size it handle?
Double is basically larger than Float? Same with Long Double..
float f;
double d;
f = 9.445f;
std::cout << f << std::endl;
d = 5.34245d; // while i assume its not long enough, but pretend its larger than
9,223,372,036,854,775,807 byte.... is double only use for int64_t?... d65_t does that exist? //
now I'm not sure if I really understand which is which size. double is like int64? due to it being 8 bytes.
std::cout << d << std::endl;
return 0;
(is this correctly way of coding?)
d, f, ld? is that correct way of use for size
_________________________________
The first question I asked about quiz. I found the answer.
guessing what the poorly worded #1 wants, I would say they just want you to write
4.0e-3 or 1.23005e2
and sig digits is like you learned in science class etc, just how many digits the value has.
123005 is 6 digits, no matter where the decimal is; its the count of values between zeros on left and right (visible or implied zeros).
I'm not sure what this means. The results should be printed as:
Zeros that are written where otherwise unnecessary are significant.
this is true for trailing, for sure. Good call. Leading, I am unconvinced.
we said the same thing on the upper part.
------------------------------
double and float are 'decimal' numbers, so in that sense they are the same. Double can hold more bits of precision (be careful talking base 10 digits that humans use here, its not compatible to discuss those). Doubles take up twice as many bits and can represent larger and smaller values due to having an internal exponent that is larger. Float is almost never used. Double is used almost everywhere, and some systems support larger doubles. Do not try to compare doubles and 64 bit integers. Doubles may use 64 bits but they are stored very differently, and its a lot to go into -- google IEEE floating point to see how they work. there is no d64_t type. the types for floating point (decimal) numbers are float, double, long double, and a few nonstandard extensions that some hardware can handle that gives 80 to well over 100 bits depending on the specific system. You don't need to worry about those. Unlike integers, which have a solid 50+ type names (many redundant) there are only those 3 for floating point.
doubles can be printed with cout << d yes. You often want to use the setprecision or other qualifiers in the cout statement to control how it is displayed (you may want scientific notation, or only 2 digits after decimal, or something so there are a lot of ways to control how it prints).