简体   繁体   中英

How to express large numbers to two decimal places in C++ Calculator

I am trying to write a calculator in C++ that does the basic functions of /, *, -, or + and shows the answer to two decimal places (with 0.01 precision).

For example 100.1 * 100.1 should print the result as 10020.01 but instead I get -4e-171 . From my understanding this is from overflow, but that's why I chose long double in the first place!

#include <iostream>
#include <iomanip>
using namespace std;

long double getUserInput()
{
    cout << "Please enter a number: \n";
    long double x;
    cin >> x;
    return x;
}

char getMathematicalOperation()
{
    cout << "Please enter which operator you want "
            "(add +, subtract -, multiply *, or divide /): \n";
    char o;
    cin >> o;
    return o;
}

long double calculateResult(long double nX, char o, long double nY)
{
// note: we use the == operator to compare two values to see if they are equal
// we need to use if statements here because there's no direct way 
// to convert chOperation into the appropriate operator

if (o == '+') // if user chose addition
    return nX + nY; // execute this line
if (o == '-') // if user chose subtraction
    return nX - nY; // execute this line
if (o == '*') // if user chose multiplication
    return nX * nY; // execute this line
if (o == '/') // if user chose division
    return nX / nY; // execute this line
return -1; // default "error" value in case user passed in an invalid chOperation
}

void printResult(long double x)
{
    cout << "The answer is: " << setprecision(0.01) << x << "\n";
}

long double calc()
{
// Get first number from user
    long double nInput1 = getUserInput();

// Get mathematical operations from user
    char o = getMathematicalOperation();

// Get second number from user
    long double nInput2 = getUserInput();

// Calculate result and store in temporary variable (for readability/debug-ability)
    long double nResult = calculateResult(nInput1, o, nInput2);

// Print result
    printResult(nResult);
    return 0;
}

setprecision tells it how many decimal places you want as an int so you're actually setting it to setprecision(0) since 0.01 get truncated. In your case you want it set to 2. You should also use std::fixed or you'll get scientific numbers.

void printResult(long double x)
{
    cout << "The answer is: " << std::fixed << setprecision(2) << x << "\n";
}

working example

It is not due to overflow you get the strange result. Doubles can easily hold numbers in the range you are showing.

Try to print the result without setprecision.

EDIT: After trying

long double x = 100.1;
cout << x << endl;

I see that it doesn't work on my Windows system.

So I searched a little and found:

print long double on windows

maybe that is the explanation.

So I tried

long double x = 100.1;
cout << (double)x << endl;

which worked fine.

2nd EDIT:

Also see this link provided by Raphael

http://oldwiki.mingw.org/index.php/long%20double

The default floating point presentation switches automatically between presentation like 314.15 and 3.1e2 , depending on the size of the number and the maximum number of digits it can use. With this presentation the precision is the maximum number of digits. By default it's 6.

You can either increase the maximum number of digits so that your result can be presented like 314.15 , or you can force such fixed point notation by using the std::fixed manipulator. With std::fixed the precision is the number of decimals.

However, with std::fixed very large and very small numbers may be pretty unreadable.

The setprecision() manipulator specifies the number of digits after the decimal point. So, if you want 100.01 to be printed, use setprecision(2) .

When you use setprecision(0.01) , the value 0.01 is being converted to int , which will have a value of 0 .

It wouldn't have hurt if you had actually read the documentation for setprecision() - that clearly specifies an int argument, not a floating point one.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM