forked from Mooophy/Cpp-Primer
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
159 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
#include <iostream> | ||
|
||
int main() | ||
{ | ||
unsigned u = 10, u2 = 42; | ||
std::cout << u2 - u << std::endl; // 32 | ||
std::cout << u - u2 << std::endl; // 4294967264 | ||
int i = 10, i2 = 42; | ||
std::cout << i2 - i << std::endl; // 32 | ||
std::cout << i - i2 << std::endl; // -32 | ||
std::cout << i - u << std::endl; // 0 | ||
std::cout << u - i << std::endl; // 0 | ||
|
||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
##Exercise 2.1 | ||
> What are the differences between int, long, long long, | ||
and short? Between an unsigned and a signed type? Between a float and | ||
a double? | ||
|
||
C++ guarantees `short` and `int` is **at least** 16 bits, `long` **at least** 32 bits, `long long` **at least** 64 bits. | ||
|
||
The `unsigned` and `signed` type have the **same size**. for example, sizeof(unsigned short) == sizeof(short). | ||
|
||
The C and C++ standards do not specify the representation of float, double and long double. | ||
It is possible that all three implemented as IEEE double-precision. | ||
Nevertheless, for most architectures (gcc, MSVC; x86, x64, ARM) float is indeed a IEEE **single-precision** floating point number (binary32), | ||
and double is a IEEE **double-precision** floating point number (binary64). | ||
|
||
Usage: | ||
|
||
- Use `int` for integer arithmetic. `short` is usually too small and, in practice, | ||
`long` often has the same size as `int`. If your data values are larger than | ||
the minimum guaranteed size of an `int`, then use `long long`. | ||
(In a word: short < **int** < long < long long) | ||
|
||
- Use an unsigned type when you know that the values cannot be negative. | ||
(In a word: no negative, unsigned.) | ||
|
||
- Use double for floating-point computations; float usually does not have | ||
enough precision, and the cost of double-precision calculations versus | ||
single-precision is negligible. In fact, on some machines, double-precision | ||
operations are faster than single. The precision offered by long double | ||
usually is unnecessary and often entails considerable run-time cost. | ||
(In a word: float < **double** < long double) | ||
|
||
Reference: | ||
|
||
- [What are the criteria for choosing between short / int / long data types?](http://www.parashift.com/c++-faq/choosing-int-size.html) | ||
- [Difference between float and double](http://stackoverflow.com/questions/2386772/difference-between-float-and-double) | ||
- Advice: Deciding which Type to Use(This book.) | ||
|
||
##Exercise 2.2 | ||
>To calculate a mortgage payment, what types would you use | ||
for the rate, principal, and payment? Explain why you selected each type. | ||
|
||
use `double`, or also `float`. | ||
|
||
The rate most like that: 4.50 % per year. | ||
The principal most like that: $854.36 | ||
The payment most like that: $1,142.36 | ||
|
||
Reference: | ||
|
||
- [mortgage-calculator](http://www.bankrate.com/calculators/mortgages/mortgage-calculator.aspx) | ||
- [What's in a Mortgage Payment?](http://www.homeloanlearningcenter.com/mortgagebasics/whatsinamortgagepayment.htm) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
##Exercise 2.3 | ||
> What output will the following code produce? | ||
```cpp | ||
unsigned u = 10, u2 = 42; | ||
std::cout << u2 - u << std::endl; | ||
std::cout << u - u2 << std::endl; | ||
int i = 10, i2 = 42; | ||
std::cout << i2 - i << std::endl; | ||
std::cout << i - i2 << std::endl; | ||
std::cout << i - u << std::endl; | ||
std::cout << u - i << std::endl; | ||
``` | ||
|
||
Output(g++ 4.8): | ||
|
||
``` | ||
32 | ||
4294967264 | ||
32 | ||
-32 | ||
0 | ||
0 | ||
``` | ||
|
||
##Exercise 2.4 | ||
> Write a program to check whether your predictions were | ||
correct. If not, study this section until you understand what the problem is. | ||
|
||
[Here](https://github.com/pezy/Cpp-Primer/blob/master/ch02/ex2_4.cpp) is the code, please test it in your computer. | ||
|
||
##Exercise 2.5 | ||
> Determine the type of each of the following literals. Explain | ||
the differences among the literals in each of the four examples: | ||
- (a) 'a', L'a', "a", L"a" | ||
- (b) 10, 10u, 10L, 10uL, 012, 0xC | ||
- (c) 3.14, 3.14f, 3.14L | ||
- (d) 10, 10u, 10., 10e-2 | ||
|
||
(a): character literal, wide character literal, string literal, string wide character literal. | ||
|
||
(b): decimal, unsigned decimal, long decimal, unsigned long decimal, octal, hexadecimal. | ||
|
||
(c): double, float, long double. | ||
|
||
##Exercise 2.6 | ||
> What, if any, are the differences between the following | ||
definitions: | ||
```cpp | ||
int month = 9, day = 7; | ||
int month = 09, day = 07; | ||
``` | ||
|
||
The first line's integer is decimal, the second line's integer is octal. | ||
|
||
##Exercise 2.7 | ||
> What values do these literals represent? What type does each | ||
have? | ||
- (a) "Who goes with F\145rgus?\012" | ||
- (b) 3.14e1L | ||
- (c) 1024f | ||
- (d) 3.14L | ||
|
||
(a): Who goes with Fergus?(new line) "string" | ||
|
||
(b): 31.4 "long double" | ||
|
||
(c): 1024 "float" | ||
|
||
(d): 3.14 "long double" | ||
|
||
Reference: | ||
|
||
- [ASCII Table](http://www.asciitable.com/) | ||
|
||
##Exercise 2.8 | ||
> Using escape sequences, write a program to print 2M followed | ||
by a newline. Modify the program to print 2, then a tab, then an M, followed | ||
by a newline. | ||
|
||
```cpp | ||
#include <iostream> | ||
|
||
int main() | ||
{ | ||
std::cout << 2 << "\115\012"; | ||
std::cout << 2 << "\t\115\012"; | ||
|
||
return 0; | ||
} | ||
|
||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters