### Aligning Numbers

I am converting a number given by the user to hyperbolic functions, I have the code written I just need help aligning the numbers. Everything is fine until I enter a negative number, or .01

What would I have to add to these cout statements?

double x;
cout << endl;
cout << "Enter a value for x" << endl;
cin >> x;
cout << endl;
cout << "Converting: " << x << endl;
cout << endl;

double e = 2.71828182846;
double minus = pow(e, x) - pow(e, -x);
double plus = pow(e, x) + pow(e, -x);

cout << setprecision(10) << "sinh(" << x << ") = " << minus / 2 << endl;
cout << setprecision(10) << "cosh(" << x << ") = " << plus / 2 << endl;
cout << setprecision(10) << "tanh(" << x << ") = " << minus / plus << endl;
cout << setprecision(10) << "coth(" << x << ") = " << plus / minus << endl;
cout << setprecision(10) << "sech(" << x << ") = " << 2 / plus << endl;
cout << setprecision(10) << "csch(" << x << ") = " << 2 / minus << endl;
 ``1234567891011121314151617181920212223242526272829303132`` ``````#include #include #include #include using namespace std; void output( string func, double x, double result ) { cout << func << "(" << x << ") = " << fixed << setprecision( 10 ) << setw( 16 ) << result << endl; cout.unsetf( ios::fixed ); } int main() { double x; cout << "Enter a value for x: "; cin >> x; cout << "Converting: " << x << "\n\n"; double e = 2.71828182846; double expx = pow( e, x ); // or just use: expx = exp( x ); double minus = expx - 1.0 / expx; double plus = expx + 1.0 / expx; output( "sinh", x, minus / 2.0 ); output( "cosh", x, plus / 2.0 ); output( "tanh", x, minus / plus ); output( "coth", x, plus / minus ); output( "sech", x, 2.0 / plus ); output( "csch", x, 2.0 / minus ); }``````

 ```Enter a value for x: -1 Converting: -1 sinh(-1) = -1.1752011936 cosh(-1) = 1.5430806348 tanh(-1) = -0.7615941560 coth(-1) = -1.3130352855 sech(-1) = 0.6480542737 csch(-1) = -0.8509181282```

 ```Enter a value for x: 0.01 Converting: 0.01 sinh(0.01) = 0.0100001667 cosh(0.01) = 1.0000500004 tanh(0.01) = 0.0099996667 coth(0.01) = 100.0033333111 sech(0.01) = 0.9999500021 csch(0.01) = 99.9983333527```

At some point you will need to use "scientific" rather than "fixed" and adjust width accordingly. These exponential-based functions have a huge range.
Last edited on
Topic archived. No new replies allowed.