As opposed to an integer, a floating-point number is one that can use a decimal section that represents a fraction of a "whole" (the whole is 1). The Intell processor supports two categories of floating-point numbers. A floating-point number is said to have single precision if its limited fraction requires a maximum of 7 digits in the fraction area. To declare a variable that can hold such a number, use the float keyword. Here is an example: #include <iostream>
using namespace std;
void main()
{
float number;
}
When specifiyinig the value of a float type, you can use a number that appears natural or you can add a fraction part. Here is an example: #include <iostream>
using namespace std;
void main()
{
float number = 51.4608;
cout << number;
putchar(L'\n');
}
The value of a variable of type float should be able to fit in 4 bytes where it would range from 3.4 x 10-38 to 3.4 x 1038. To support decimal numbers with single precision, the Win32 library provides the FLOAT data type. It can be used wherever the float keyword can be used.
A floating-point number with double-precision is a number that requires more memory space and at the same time provides more precision. Such a number requires double the amount of single precision. This means that a double-precision number must be stored in 64 bits. Its value would range from 1.7 x 10-308 to 1.7 x 10308. To declare a variable that must store a large decimal number, use the double keyword. As done for the single-precision variable, when initializing the variable, provide a natural number or a number with a fractional part. This time, you are allowed to provide as long a fractional part as you want. Here is an example: #include <iostream>
using namespace std;
void main()
{
double number = 57.4038;
cout << number;
putchar(L'\n');
}
To support floating-point numbers with double-precision, the Win32 library provides the DOUBLE data type. It can be used wherever a double keyword would.
For an even larger variable, use the 10 Byte real data type identified as a long double data type that ranges from 3.4 x 10-4932 to 1.1 x 104932. Here is an example that uses the long double data type: #include <iostream>
using namespace std;
void main()
{
long double radius = 15.625, Radius = 18.125;
long double area, Area, TotalArea;
Area = Radius * Radius * 3.14159;
area = radius * radius * 3.14159;
TotalArea = Area - area;
cout << "Properties of the plate";
cout << "\nExternal Radius: " << Radius;
cout << "\nInternal Radius: " << radius;
cout << "\nArea: " << TotalArea;
}
We have been introduced to declaring variables using specific data types. After declaring a value and initializing it, you may want the value to change type without redefining it. This is required in some cases where you already have a value, probably produced by one variable, while another variable declared with a different data type must use the value. This means that you would need to cast a value from one type into another type. For example, you may have declared a variable using a double data type but you need the value of that variable to be used as an int. Transferring a value from one type to another is referred to as casting. There are two broad types of casting available in C++: C's casting and the C++ standards.
C, the parent of C++ supports value casting by specifying the type of value you want an existing one to have. To do this, you use the following formula: (DataType)Expression Based on this formula, in the parentheses, enter the type of data you want the existing or resulting value to have. The DataType factor can be any of the data types we saw above. The Expression factor can be a constant value. Here is an example: #include <iostream>
using namespace std;
int main()
{
cout << "Number: " << (int)3.14159 << "\n";
return 0;
}
Notice that the value to cast is a floating-point number. If the casting succeeds, the new value would be conform to the type in parentheses. For example, the above code would produce: Number: 3 In the above code, we cast the value directly where it is need. Otherwise, you can first declare the variable that will hold the value. Here is an example: #include <iostream>
using namespace std;
int main()
{
int number;
number = (int)3.14159;
cout << "Number: " << number << "\n";
return 0;
}
The Expression can also be the result of a calculation. In this case, you should include the whole expression is its own parentheses. The Expression factor of our formula can also be the name of a variable that holds a value. Here is an example: #include <iostream>
using namespace std;
int main()
{
double price = 258.85;
int number;
cout << "Price? $" << price << "\n";
number = (int)price;
cout << "Number: " << number << "\n";
return 0;
}
This would produce: Price? $258.85 Number: 258
C++ provides its own support of value casting using various keywords so you can specify the type of casting you want. One of the keywords used is static_cast and the formula is: static_cast<DataType>(Expression) In this formula, the static_cast keyword, the <, the >, and the parentheses are required. The DataType should be an existing data type. The Expression can be a constant value. Here is an example that casts a floating-point number to an integer: #include <iostream>
using namespace std;
int main()
{
cout << "Number: " << static_cast<int>(3.14159) << "\n";
return 0;
}
You can also assign the resulting value to a previously declared variable: #include <iostream>
using namespace std;
int main()
{
int number = static_cast<int>(3.14159);
cout << "Number: " << number << "\n";
return 0;
}
The value to cast can also be the result of a calculation. The value can also be originating from an existing variable whose value you want to convert to a new type. Here is an example: #include <iostream>
using namespace std;
int main()
{
double PI = 3.14159;
int number;
number = static_cast<int>(PI);
cout << "PI = " << PI << endl;
cout << "Number = " << number << "\n";
return 0;
}
This would produce: PI = 3.14159 Number = 3
So far, to declare a variable, we use the body of main. Such a variable could be used only in that body. In some cases, you may want to declare a variable that can be accessed from one section of the code. The section of code in which a variable can be accessed is referred to as its scope.
If you declare a variable inside of curly brackets (an opening curly bracket "{" and a closing curly bracket "}"), that variable can be only in that body. Consider the following example: #include <iostream>
using namespace std;
int main()
{
double number = 3.14159;
cout << "Number = " << number << "\n";
return 0;
}
Such a variable is referred to as local. Everything between these brackets belongs to a local scope. Once you have declared such a variable, you cannot declare another variable in the same scope and that bears the same name. Consider the following example: #include <iostream>
using namespace std;
int main()
{
double number = 3.14159;
cout << "Number = " << number << "\n";
double number = 2.98;
cout << "Number = " << number << "\n";
return 0;
}
This would produce a "redefinition" error and the program would not compile, even if the second declaration uses a different data type. As one type of solution to this kind of problem, C++ allows you to create a "physical" scope. To do this, you can use curly brackets to delimit the scope of a particular variable. Here is an example: #include <iostream>
using namespace std;
int main()
{
{
double number = 3.14159;
cout << "Number = " << number << "\n";
}
double number = 2.98;
cout << "Number = " << number << "\n";
return 0;
}
This would produce: Number = 3.14159 Number = 2.98 In the code, notice that we delimit only the first declaration. Indeed, you can delimit each scope if you want: #include <iostream>
using namespace std;
int main()
{
{
double number = 3.14159;
cout << "Number = " << number << "\n";
}
{
double number = 2.98;
cout << "Number = " << number << "\n";
}
return 0;
}
C++ allows you to declare a variable outside of any "body". Such a variable is called a global variable. Here is an example: #include <iostream>
using namespace std;
double number;
int main()
{
return 0;
}
After declaring the variable, you can initialize it immediately: #include <iostream>
using namespace std;
double number = 3.14159;
int main()
{
cout << "Number = " << number << "\n";
return 0;
}
You can also initialize it inside another "body" that would use it. After declaring and initializing the variable, you can access its value and use it. For example, you can display its value to the user. Here is an example: #include <iostream>
using namespace std;
double number;
int main()
{
number = 3.14159;
cout << "Number = " << number << "\n";
return 0;
}
In C++ (some other languages don't have this situation), the compiler always proceed in a top-down approach. After declaring a variable, only the sections under it can access it. This means that you cannot access a variable above its declaration. Consider the following program where only the area of the declaration has changed: #include <iostream>
using namespace std;
int main()
{
number = 3.14159;
cout << "Number = " << number << "\n";
return 0;
}
double number;
This program would not compile. The reason is that, when accessing it inside of main(), as far as main() is concerned, the variable has never been declared and C++ doesn't allow to use a variable before it has been declared. Therefore, you must always declare a variable before accessing it. |
|
||||||||||||||||||||||||||||||
|
|