Question:
What are the values of the variables in C++?
david
2012-01-06 21:10:25 UTC
I am taking a beginning programming class and I don't quite understand the difference in variable.
My instructor wants us to use the "least expensive" variable as possible. He said most programmers use "double" too often because they are lazy. Will someone briefly list the in order of "cost" and function??
Seven answers:
Jonathan
2012-01-06 22:36:56 UTC
Variable costs are usually broken out in terms of memory and execution time. There are other costs, though, such as precision and whether or not they follow the rules of real number algebra such as the distributive property (which is violated when using floating point.)



Direct memory cost is simple. You can use the sizeof() operator to find out the size of any variable in C++. A larger number means it costs more memory. Indirect memory costs are not so simple. When a compiler generates machine code (or .NET IL code, I suppose) for something you write, that code also takes up memory. Some types of variables under some operations will require more effort and code. For example, if you use a 64-bit integer and your target is an 8-bit embedded processor, it's almost certain that it will take a lot of instructions to add a value to such a variable. And that is a "memory" cost, as well as an execution time cost. Floating point usage may incur an entire floating point library being hauled in, as well. Particularly in the case of 8-bit embedded microcontrollers which almost certainly have no hardware support for floating point. So even using a single instance might incur many thousands of bytes of memory cost just to drag in that library. As well as all the execution time required, as well.



Execution time costs are usually more for floating point, but not always. In some of the Intel CPUs, the floating point actually takes FEWER clock cycles -- but this was because some of the integer operations used the floating point unit and converted back. Execution time costs will be related to how many memory operations are required in order to read and/or write the entire variable and larger sized variables are more at risk here. Other time costs will be related to library calls that may be required and the number of instructions needed to complete some operation.



Floating point also does not follow some rules you'd normally expect. For example, a*(b+c) is not necessarily the exact same value as a*b+a*c. In integer form, it always is (so long as ranges aren't exceeded.) But in floating point, it only sometimes is the same. If you sum up numbers in floating point after sorting them from smallest to largest, you can easily get a different number than if you sorted them from largest to smallest, instead. Just as another example. So this is sometimes a cost, as well.



Precision and dynamic range are other details I will just hand-wave towards, so you can look them up. But there are significant differences here between integers and floating point, as well. And these are also trade-offs to be weighed.
2016-05-16 02:59:35 UTC
i don't now what you really want. but there is too many choise to do it and if you wish use the value of the variable while that one is decrement or increment you need use a bucle, but first let me show how its work: increment any varible in one : x++ or ++x; each one have diferent result. for example : if the value of the variable x is 5 and we are using x++ begin of: x++; d=x+3; d=9; but if you use ++x; ++x; d=x+3; d=8; so, when you use ++ before of any variable first use the value of the variable and the, it'll increment that varible, and when you use ++ after of any variable first increment the variable in one and them do the operations with. its the same when you use -- to decrement a variable. them than ones (++/---) is usually used in a bucle (for/while/dowhile), cause is more workfull there, so to compare a variable in c. must use the operator == that means equals. for example: a==b// if a=3 and b=3 the result will be true.
2012-01-06 22:31:25 UTC
You can use sizeof() to determine how many bytes of space are used by a variable of any given type on a particular machine, like so:



std::cout << "An int takes up " << sizeof(int) << " bytes." << std::endl;



As other posters have mentioned, the number of bytes any variable takes up is machine dependent. Also, you must remember that a variable can be signed or unsigned. An unsigned variable can store a larger number than a signed variable, but that number can only be positive or zero.



Often, the data that needs to be stored will suggest the data type to use. For example, a sports score could be stored in an int or a short because they are never terribly large.
Techwing
2012-01-06 23:33:53 UTC
Using float rather than double reduces the amount of processor time and memory required for the program to execute at run time. However, the accuracy of float is so terrible (only about 6 digits) that I always use double. Even double isn't very accurate, so sometimes you have to resort to special math packages, especially if you can't afford any rounding errors (then you need decimal arithmetic, as for financial applications).



So using double isn't laziness, it's precaution.
Fredmaster
2012-01-06 21:15:33 UTC
By "cost" and "expensive", I assume he is referring to how much memory they occupy. C++ data types don't occupy a fixed amount of memory (it depends on the system you're running), but they do have an ordering of smaller and larger ones. This page lists the basic data types, what they're used for, and how much memory they generally occupy on a 32-bit system:



http://www.cplusplus.com/doc/tutorial/variables/
2012-01-06 21:54:05 UTC
char = 1

int = 4

double = 8



Varies slightly with your computer
Zappie
2012-01-07 02:42:42 UTC
int < float < double


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Continue reading on narkive:
Loading...