I need to write the code for solving a simple equation, which should contain arcsin and arccos.
Formulas
F = cos^2(p(x)) + sin^2(p(y) + p(z)
p(k) = arccos(a * k) + arcsin(k)
Here is the code I wrote for solving it, but it gives -nan Ind as output. However, when I fix acos and asin to just cos and sin, everything works.
Expected input:
x = 1.20
y = 3.05
z = 2.00
Code
#include <iostream>
#include <math.h>
using namespace std;
const double a = 0.01;
double p(double k) {
double p1;
p1 = acos(a * k) + asin(k);
return (p1);
}
int main() {
double x;
double y;
double z;
double F;
cout << "x = "; cin >> x;
cout << "y = "; cin >> y;
cout << "z = "; cin >> z;
F = pow(cos(p(x)), 2) + pow(sin(p(y)), 2) + p(z);
cout << "F = " << F << endl;
return 0;
}
The OUTPUT range of SIN and COS is [-1, +1]. That means the INPUT range of ARC (inverse) sin is [-1, +1]. You are trying to do asin(1.2), which probably yields NaN.
Related
Im trying to make a simple converter using reference to convert between cartesian and polar the problem is it gives me wrong answers and sometimes 0 ,0 . I want to know what's the problem and how can i fix it .
this is the code :
#include <iostream>
#include <cmath>
using namespace std;
void cartesianToPolar(int x,int y,float &r,float &q ) {
r = sqrt(x * x + y * y); q = atan(y / x);
}
void polarToCartesian(float r, float q, int &x, int &y) {
x = r * cos(q); y = r * sin(q);
}
int main() {
int cevap ;
int x = 0 , y = 0 ,xx = 0 , yy = 0;
float r = 0 , q = 0 , rr = 0 , qq = 0 ;
cout << "Choose please....." << endl;
cout << "1-Cartesian -> polar "<<endl;
cout << "2-polar ->Cartesian " << endl;
cin >> cevap;
if(cevap==1){
cout << "enter x value: " ;
cin >> x;
cout << "enter y value: " ;
cin >> y;
cartesianToPolar(x,y,rr,qq);
cout << "r: " << rr << " " << "Q: " << qq << endl;
}
else if (cevap==2)
{
cout << "enter r value : ";
cin >> rr;
cout << "enter Q value: ";
cin >> qq;
polarToCartesian(r, q, xx, yy);
cout << "x: " << xx << " " << "y: " << yy << endl;
}
return 0;
}
Results of both of your functions should be floats, not ints:
void cartesianToPolar(float x, float y, float &r, float &q ) {
r = sqrt(x * x + y * y); q = atan(y / x);
}
void polarToCartesian(float r, float q, float &x, float &y) {
x = r * cos(q); y = r * sin(q);
}
The values you were computing were correct, but the result was converted to integers afterwards. Conversion to int happens by trunctation, that is, 0.1 and 0.9 all become just 0.
You also had a typo in your polar to cartesian conversion in the main. Used the wrong variable. Correct is:
polarToCartesian(rr, qq, xx, yy);
Following #Yunnosch comment, you should use atan2() rather than atan(). Detailed explanation can be found here
Iam Beginner in programming and i have a question.
how make algorithm for cube root finding in C++ without using functions like pow().
The user enters the number and the number of decimal places.
My code:
My do while isn't working
double number;
cout << "Enter number = ";
cin >> number;
int x;
int y = number / 3.;
int i;
cout << "Enter i = ";
cin >> i;
do {
x = y;
y = (2. * x + number / (x * x)) / 3.;
} while (abs(x - y) >= i);
Your algorithm is nearly fine. You just need to change your variables to float/double. Here is the edited code :
#include <iomanip>
#include <iostream>
#include <limits>
using namespace std;
int main() {
double number;
cout << "Enter number = ";
cin >> number;
double x;
double y = number / 3.;
double i;
cout << "Enter i = ";
cin >> i;
do {
x = y;
y = (2. * x + number / (x * x)) / 3.;
} while (abs(x - y) >= numeric_limits<double>::epsilon());
cout << fixed << setprecision(i) << y;
}
Sample Run :
Enter number = 10
Enter i = 2
2.15
A little add up :
As pointed out by chux - Reinstate Monica, abs(x - y) >= numeric_limits<double>::epsilon() is not a good condition to check equality. You can go through this thread for more knowledge : What is the most effective way for float and double comparison?
Another one : Why is “using namespace std;” considered bad practice?
I am trying to translate this equation to c++ code:
x = (10π)/(a+b)*sinC^3+3(ln a)(tan C)
Here is my attempt:
#include <iostream>
#include <iomanip>
#include <math.h> using namespace std;
int main()
{
float x, y, z, a, b, C, PI;
cout << endl << "Enter value a=";
cin >> a;
cout << "Enter value b=";
cin >> b;
cout << "Enter angle C in degrees=";
cin >> C;
PI = 3.1416;
C = C * PI / 180;
x = ((10 * PI) / (a + b)) * pow(sin(C), 3);
+3 * (log(a)) * (tan(C));
y = 0;
z = 0;
cout << fixed << setprecision(4);
cout << endl << "x = " << x;
cout << endl << "y = " << y;
cout << endl << "z = " << z;
}
Pi is defined in math.h, as M_PI
Sine is defined in math.h as double sin(double)
Tangent is defined in math.h as double tan(double)
Natural Log is defined in math.h as double ln(double)
Power is defined in math.h as double pow(double,double)
You wrote:
x = ((10 * PI) / (a + b)) * pow(sin(C), 3);
+3 * (log(a)) * (tan(C));
The second 1/2 of that (starting at +3) is not part of the original expression, which ended with a semi-colon.
Try:
x = ((10 * PI) / (a + b)) * pow(sin(C), 3) + 3 * (log(a)) * (tan(C));
Also, be sure you understand the difference between log(base10) and ln(natural log).
I am learning C++ and came upon this problem while trying to use a formula to calculate the current.
And I got: 0.628818 where the answer should be:
f=200 Hz
R=15 Ohms
C=0.0001 (100µF)
L=0.01476 (14.76mH)
E = 15 V
Answer: I = 0.816918A (calculated)
Below is my code:
#include <iostream>
#include <cmath>
int main()
{
const double PI = 3.14159;
double r = 15;
double f = 200;
double c = 0.0001;
double l = 0.01476;
double e = 15;
double ans = e / std::sqrt(std::pow(r, 2) + (std::pow(2 * PI*f*l - (1.0 / 2.0 * PI*f*c), 2)));
std::cout << "I = " << ans << "A" << std::endl;
}
I have read about truncation errors and tried to use 1.0/2.0 but doesn't seem to work either.
Truncation error refers to using only the first N terms of an infinite series to estimate a value. So the answer to your question is "No." You might find the following to be of some interest however....
#include <iostream>
#include <cmath>
#include <iomanip>
using namespace std;
template<typename T>
T fsqr(T x) { return x * x; }
// Numerically stable and non-blowuppy way to calculate
// sqrt(a*a+b*b)
template<typename T>
T pythag(T a, T b) {
T absA = fabs(a);
T absB = fabs(b);
if (absA > absB)
{
return absA*sqrt(1.0 + fsqr(absB / absA));
} else if (0 == absB) {
return 0;
} else {
return absB*sqrt(1.0 + fsqr(absA / absB));
}
}
int main () {
double e, r, f, l, c, ans;
const double PI = 3.14159265358972384626433832795028841971693993751058209749445923078164062862089986280348253421170;
cout << "Insert value for resistance: " << endl;
cin >> r ;
cout << "Insert value for frequency: " << endl;
cin >> f;
cout << "Insert value for capacitance: " << endl;
cin >> c;
cout << "Insert value for inductance: " << endl;
cin >> l;
cout << "Insert value for electromotive force (voltage): " << endl;
cin >> e;
ans = e / pythag(r, 2*PI*f*l - (1/(2*PI*f*c)) );
cout << "I = " << ans << "A" << endl;
system("pause");
return 0;
}
Just kidding about all that PI.
The main problem is multiplying ½ by πfC instead of dividing, here:
(1.0 / 2.0 * PI*f*c)
This sort of problem is best avoided by using suitable named values (that also allows you to use faster and more precise x*x instead of std::pow(x,2)).
You can also remove some of that arithmetic by using the standard hypotenuse function instead of squaring and sqrting inline:
double ans = e / std::hypot(r, (2*PI*f*l - 0.5/PI/f/c));
#include <iostream>
#include <cmath>
int main()
{
static constexpr double PI = 4 * std::atan(1);
double r = 15; // ohm
double f = 200; // hertz
double c = 0.0001; // farad
double l = 0.01476; // henry
double e = 15; // volt
double current = e / std::hypot(r, (2 * PI*f*l - 0.5/PI/f/c));
std::cout << "I = " << current << "A" << std::endl;
}
Is there any command in C++ to make,
1.354322e-23
into
0
This is my (simple) Program
#include "stdafx.h"
#include <iostream>
#include<iomanip>
int main()
{
float x;
std::cin >> x;
std::cout << x << std::endl;
return 0;
}
When I type values like,
2.2356e-17
It gives,
2.2356e-017
std::setprecision won't work either...
Edit:
OK this is my problem.
I created a program that can give sin cos and and tan values.
For cos 90, I want it to be 0 instead of -4.311e-008
Heres my real program
#include "stdafx.h"
#include <iostream>
#include<iomanip>
float Pi()
{
float pi = (atan(1) * 4);
return pi;
}
int Selector()
{
using namespace std;
cout << "Type:\t1 for Degrees\n\t2 for Radians\n\t3 for Gradians\n\nYour Choice : ";
int x;
cin >> x;
return x;
}
float D_R(float a)
{
float q = (a / 180);
float r = q*Pi();
return r;
}
float G_R(float a)
{
float q = (a / 200);
float r = q*Pi();
return r;
}
float All(float a, float o)
{
using namespace std;
std::cout << setprecision(5) << "sin(" << o << ") = " << sin(a) << std::endl;
std::cout << setprecision(5) << "cos(" << o << ") = " << cos(a) << std::endl;
std::cout << setprecision(5) << "tan(" << o << ") = " << tan(a) << std::endl;
return 0;
}
int main()
{
using namespace std;
int x = Selector();
cout << "Enter your angle : ";
float o;
cin >> o;
float d = D_R(o);
float g = G_R(o);
if (x == 1)
All(d, o);
else if (x == 2)
All(o, o);
else if (x == 3)
All(g, o);
return 0;
}
Edit:
Ok I came up with inserting
if (std::abs(sin(a)) < 0.0001) a = 0;
if (std::abs(cos(a)) < 0.0001) a = 0;
if (std::abs(tan(a)) < 0.0001) a = 0;
before my All() function
And that solved my problem
C++ can't arbitrarily round numbers down to 0 for you, it's up to you to define what a "very small number" is for your purposes.
Once you've determined the threshold, you simply need
if (std::abs(number) < THRESHOLD) number = 0;
RE: Your edits
For cos 90, I want it to be 0 instead of -4.311e-008
Again, it's up to you to define what the threshold is. Do you want 0.00000001 to be rounded to 0? What about 0.0001? What about 0.1? You need to define the line where rounding occurs.
Probably trunc() does what you want.
#include <cmath>
cout << roundf(2.2356e-17) << " " << trunc(2.2356e-17) << endl;
Output
0 0
See Also: round(), trunc(), nearbyint().