Home /
Expert Answers /
Other /
c-programming-decimal-value-data-types-such-as-float-and-double-represent-the-decimal-number-as-an-a
(Answered): c++ programming. Decimal value data types such as float and double represent the decimal number as a ...
c++ programming.
Decimal value data types such as float and double represent the decimal number as an approximation. In other words, float or double arithmetic do not give exact answer but near approximation to the answer. As an example, run the following program and check its result: #include <iostream> using namespace std; int main() { float x= 0.1 * 7; if (x == 0.7) cout<< "TRUE. n"; else cout<< "FALSE. n"; return ; } In some situations, we need our programs to give exact solutions instead of near to exact answers. Some programming languages provide a special data type called Decimal that represents decimal numbers without the use of approximation. Write a program in C++ that implements the Decimal data type using a class called BigDecimal. This class will allow users to create decimal values and perform several operations on these values. The class should use two member variables: the integer part saved as a string the decimal part saved as a string For example, the following instance BigDecimal d("6.45678"); will store "6" in the first variable and "45678" in the second variable The class should have the following functionalities: Member Function Description BigDecimal() The default constructor. Creates the number 0.0 BigDecimal(string) A constructor that accepts a string representing the numeric value with the decimal point. == operator Accepts two BigDecimal values and compares their values. If the two decimal values are equal then the method should return true otherwise it should return false. != operator Accepts two BigDecimal values and compares their values. If the two decimal values are equal then the method should return false otherwise it should return true. ++ operator (prefix) ++ operator (postfix) This operator should increment the integer part of BigDecimal object by one. The overloaded ++ operator should return the contents of the object (using this pointer) after it is incremented. This operator should increment the integer part of BigDecimal object by one. The overloaded ++ operator should return the contents of the object before it is incremented. Displays the numeric value of BigDecimal to the standard output Reads a string value from the standard input and stores it in the BigDecimal object. Converts the BigDecimal value to a double. << operator >> operator double toDouble() You can assume that all the values of BigDecimal are nonnegative. Here is an example of how BigDecimal can be used: BigDecimal x("45.67"); BigDecimal y("2.5"); //Should print 2.5 cout << X++ << endl; //Should print 4.5 cout << ++X << endl; //Should print false cout << (x==y) << endl;