-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtypeConversion.cpp
More file actions
112 lines (75 loc) · 3.49 KB
/
typeConversion.cpp
File metadata and controls
112 lines (75 loc) · 3.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
/*
Implicit (Automatic) Type Conversion
When you assign a value of a fundamental (built-in) type to a variable of another fundamental type,
C++ automatically converts the value to the receiving type, if the two types are compatible. For examples,
If you assign an int value to a double variable, the compiler automatically casts the int value to a double
double (e.g., from 1 to 1.0) and assigns it to the double variable. If you assign a double value of to an int
variable, the compiler automatically casts the double value to an int value (e.g., from 1.2 to 1) and assigns
it to the int variable. The fractional part would be truncated and lost. Some compilers issue a warning/error
"possible loss in precision"; others do not.
Explicit Type-Casting
You can explicitly perform type-casting via the so-called unary type-casting operator in the form of (new-type)
operand or new-type(operand). The type-casting operator takes one operand in the particular type, and returns an
equivalent value in the new type. Take note that it is an operation that yields a resultant value, similar to an
addition operation although addition involves two operands. For example,
Operator static-cast<type>
C++ introduces a new operator called static_cast<type> to perform type conversion (because the regular cast
mentioned earlier is too lax and could produce expected results). static_cast signal an error if conversion fails.
*/
/*
* Converting between Celsius and Fahrenheit
* Celsius = (5/9)(Fahrenheit–32)
* Fahrenheit = (9/5)Celsius+32
*/
#include <iostream>
#include <iomanip> // needed for formatting floating-point numbers
using namespace std;
int main() {
double celsius, fahrenheit;
// Format floating-points in fixed with 2 decimal places
cout << fixed << setprecision(2);
cout << "Enter the temperature in celsius: ";
cin >> celsius;
fahrenheit = celsius * 9 / 5 + 32;
// 9/5*celsius + 32 gives wrong answer! Why?
cout << celsius << "C is " << fahrenheit << "F" << endl;
cout << "Enter the temperature in fahrenheit: ";
cin >> fahrenheit;
celsius = (fahrenheit - 32) * 5 / 9;
// 5/9*(fahrenheit - 32) gives wrong answer! Why?
cout << fahrenheit << "F is " << celsius << "C" << endl;
// Print floating-point number in fixed format with 1 decimal place
cout << fixed << setprecision(1);
// Test explicit type casting
int i1 = 4, i2 = 8;
cout << i1 / i2 << endl; // 0
cout << (double)i1 / i2 << endl; // 0.5
cout << i1 / (double)i2 << endl; // 0.5
cout << (double)(i1 / i2) << endl; // 0.0
double d1 = 5.5, d2 = 6.6;
cout << (int)d1 / i2 << endl; // 0
cout << (int)(d1 / i2) << endl; // 0
// Test implict type casting
d1 = i1; // int implicitly casts to double
cout << d1 << endl; // 4.0
i2 = d2; // double truncates to int! (Warning?)
cout << i2 << endl; // 6
/* EXPLICIT CAST*/
// Print floating-point number in fixed format with 1 decimal point (need <iomanip>)
cout << fixed << setprecision(1);
cout << (double)5 << endl; // int 5 → double 5.0
cout << (int)5.5 << endl; // double 5.5 → int 5
double aDouble = 5.6;
int anInt = (int)aDouble; // return 5 and assign to anInt. aDouble does not change!
// C++ also supports function-style type cast.
cout << double(5) << endl; // 5.0
cout << int(5.5) << endl; // 5
cout << int(aDouble) << endl; // 5
/* STATIC TYPE_CAST */
double d = 5.5;
int i = static_cast<int>(d);
float f = static_cast<float>(i);
long l = static_cast<long>(d);
system("pause");
return 0;
}