#include <iostream>
#include <math.h>
#include <cstdlib>
using namespace std;
double cosin_value( double value );
double sin_value( double value );
double big_degree( double value );
double big_radian( double value );
double x;
double value;
double degree;
double radian;
const double PI = 3.14159;
char choice;
char yes;
int main()
{
cout << "Please enter an angle value => ";
cin >> value;
cout << "Is the angle in Degree or Radian?" << endl;
cout << "\t" << "Type D if it is in Degree" << endl;
cout << "\t" << "Type R if it is in Radian" << endl;
cout << "Your response => ";
cin >> choice; //degree or radian?
cout.setf(ios::fixed);
cout.setf(ios::showpoint);
cout.precision(10);
if (choice == 'D' || choice == 'd')
{
big_degree (value);
cout << " " << "sin(x) = " << "\t" << sin_value(degree) << endl;
cout << " " << "cos(x) = " << "\t" << cosin_value(degree) << endl;
cout << " " << "tan(x) = " << "\t" << sin_value(degree) / cosin_value(degree) << endl;
}
else if (choice == 'R' || choice == 'r')
{
big_radian (value);
cout << " " << "sin(x) = " << "\t" << sin_value(radian) << endl;
cout << " " << "cos(x) = " << "\t" << cosin_value(radian) << endl;
cout << " " << "tan(x) = " << "\t" << sin_value(radian) / cosin_value(radian) << endl;
}
return 0;
}
// Sine,cosine functions
// angle -360<value<360
double sin_value( double value )
{
int count=1;
double sine, num, dem, sign, term;
sine = 0;
sign = 1;
num = value;
dem = count;
while ( count <= 20 )
{
term = ( num / dem );
sine = sine + term * sign;
num = num * value * value;
count = count + 2;
dem = dem * count * ( count - 1 );
sign = -sign;
}
return (sine);
}
double cosin_value( double value )
{
int count = 0;
double cosine, num, dem, sign, term;
cosine = 0;
sign = 1;
num = 1;
dem = 1;
while ( count <= 20 )
{
term = ( num / dem );
cosine = cosine + term * sign;
num = num * value * value;
count = count + 2;
dem = dem * count * ( count - 1 );
sign = -sign;
}
return (cosine);
}
double big_degree( double value )
{
int result;
const int angle = 360;
if (value >= 360 || value <= -360)
{
result = value / angle;
degree = ( value - ( result * angle ) ) * PI / 180;
}
else
{
degree = ( value * PI ) / 180;
}
return (degree);
}
double big_radian( double value )
{
int result;
if (value >= 2 * PI || value <= -2 * PI)
{
result = value / ( 2 * PI );
radian = ( value - ( result* 2 * PI ) );
}
else
{
radian = value;
}
return (radian);
}
I have few problems here:
How can the program shows tan(x) is infinity when I input a value 90 degree or 1.5708 radian? When I input 90 degree, it gave me an output of 0.0000013268 instead of 0 for cos(x).
I tried to put in this command in my cosin function where If cos(x)<0.00001, set it to zero, it worked for 90 degree but for other values like 2.3145 radian, cos(x) value is 0 instead of -0.677013.
I appreciate your guides!
Use epsilon value just like you mentioned in question #2.
Use an absolute value of cos(x) like abs(cos(x)) in your if statement. .
You can also represent infinity with double or float. Check this link.
http://www.gnu.org/software/libc/manual/html_node/Infinity-and-NaN.html
More importantly, you might want to read this article called "What Every Computer Scientist Should Know About Floating-Point Arithmetic"
http://docs.oracle.com/cd/E19957-01/806-3568/ncg_goldberg.html
You will notice that each step of your floating point operations will accumulate errors in calculation.
Related
#include <iostream>
#include <iomanip>
using namespace std;
//Power function
float power (float base, int exp)
{
if (exp < 0)
{
if (base == 0)
{
cout << "Base cannot be 0.";
return -1;
}
}
if (exp == 0)
return 1;
if (exp == 1)
return base;
return base * power (base, exp - 1);
}
//Factorial function
int facto (int n)
{
return n <= 0 ? 1 : n * facto (n - 1);
}
//Cos function
float cosCalc (float rad)
{
float cos = 0;
int x;
for (x = 0; x < 10; x++)
{
cos += power (-1, x) * power (rad, 2 * x) / facto (2 * x);
}
return cos;
}
//Sin function
float sinCalc (float rad)
{
float sin = 0;
int x;
for (x = 0; x < 10; x++)
{
sin += power (-1, x) * power (rad, 2 * x + 1) / facto (2 * x + 1);
}
return sin;
}
//Main function
int main()
{
int choice;
//Title and Menu
cout << endl << "==============" << endl << " TRIGONOMETRY " << endl << "==============";
cout << endl << "Select:";
cout << endl << "1. Calculate Cos and Sin";
cout << endl << "9. Exit";
while (true)
{
//User Prompt
cout << endl << endl << "Please enter your choice. => ";
cin >> choice;
if (choice == 1)
{
int angle, anglePh;
float rad;
float pi = 3.14159265358979323846264338327950288419716;
char angleType;
float cos = 0;
float sin = 0;
cout << endl << "Please enter an angle. => ";
cin >> angle;
anglePh = angle;
angle %= 360;
rad = angle * pi / 180;
cout << anglePh << " degrees = " << rad << " radian";
cout << endl << "Calculating Cos...";
cosCalc (rad);
cout << endl << "Cos = " << fixed << cos;
cout << endl << "Calculating Sin...";
sinCalc (rad);
cout << endl << "Sin = " << fixed << sin;
}
if (choice == 9)
{
break;
}
}
}
I am building a program that calculates Sin and Cos off an angle input, and when I run it, it outputs 0.000000 for both Sin and Cos. I suspect there is something to do with me declaring float cos = 0 and float sin = 0 in the if loop for choice == 1, and I tried messing around with it but it either results in the program straight out giving me errors on launch, or I get the same outputs.
Any idea where I went wrong?
Thanks for your insight in advance, cheers!
Your cosin and sine function return a float, but in order to get that result you still have to store it in a variable.
So instead of:
cosCalc (rad);
Do:
rad = cosCalc (rad);
and the same for your sine function.
the code works but the result keeps getting bigger whenever i put 2 or a higher value in it
#include <iostream>
using namespace std;
int main() {
double pi = 0;
long i;
long n;
cout << "Enter the value of n: ";
cin >> n;
cout << endl;
for (i = 0; i < n; i++)
{
if (i % 2 == 0)
pi = pi + (1 / (2 * i + 1));}
else
pi = pi - (1 / (2 * i + 1));}
pi = 4 * pi;
}
cout << endl << "pi = " << pi << endl;
return 0;
}
Because your { and } is wrong. I think brackets will be as given below
If the formula is PI = 4/1 - 4/3 + 4/5 - 4/7 + ... ( Leibniz's Series ) then you can formalate as given below
#include <iostream>
using namespace std;
int main() {
double n, i; // Number of iterations and control variable
double s = 1; //Signal for the next iteration
double pi = 0.0;
cout << "Enter the value of n: ";
cin >> n;
cout << endl;
cout << "Approximation of the number PI through the Leibniz's series\n";
for(i = 1; i <= (n * 2); i += 2){
pi = pi + s * (4 / i);
s = -s;
cout << "Step (" << (i-1)/2 << "):" << pi << endl;
}
cout << endl << "pi = " << pi << endl;
return 0;
}
Then result will be as if n = 13
Approximation of the number PI through the Leibniz's series
Step (0):4
Step (1):2.66667
Step (2):3.46667
Step (3):2.89524
Step (4):3.33968
Step (5):2.97605
Step (6):3.28374
Step (7):3.01707
Step (8):3.25237
Step (9):3.04184
Step (10):3.23232
Step (11):3.0584
Step (12):3.2184
pi = 3.2184
For Wallis's Series found in Europe by John Wallis in 1655 ( PI = 2/1 x 2/3 x 4/3 x 4/5 x ....) then code will be as
#include <iostream>
using namespace std;
int main()
{
double n, i = 0 ; // Number of iterations and control variable
double pi = 4.;
cout << "Approximation of the number pi through the Wallis's series\n";
cin >> n;
cout << endl;
for(i = 3; i <= (n + 2); i+=2) {
pi = pi * ((i - 1) / i) * (( i + 1) / i);
cout << "Step(" << (i-3)/2 << "):" << pi << endl;
}
cout << "\nAproximated value of PI = " << pi << endl;
}
then result will be
Approximation of the number pi through the Wallis's series
Step(0):3.55556
Step(1):3.41333
Step(2):3.34367
Step(3):3.30239
Step(4):3.2751
Step(5):3.25572
Step(6):3.24125
Step(7):3.23004
Step(8):3.22109
Step(9):3.21378
Step(10):3.20771
Step(11):3.20258
Step(12):3.19818
Step(13):3.19438
Step(14):3.19106
Step(15):3.18813
Step(16):3.18552
Step(17):3.1832
Step(18):3.1811
Step(19):3.17921
Aproximated value of PI = 3.17921
For Nilakantha's Series PI = 3 + 4/(2x3x4) - 4/(4x5x6) + 4/(6x7x8) - ... then code will be as given
#include <iostream>
using namespace std;
int main()
{
double n, i; // Number of iterations and control variable
double s = 1; //Signal for the next operation
double pi = 3;
cout << "Approximation of the number PI through the sequence of the Nilakantha's series\n" ;
cin >> n;
cout << endl;
for(i = 2; i <= n*2; i += 2){
pi = pi + s * (4 / (i * (i + 1) * (i + 2)));
s = -s;
cout << "Step(" << (i-2)/2 << "):" << pi << endl;
}
cout << "\nAproximated value of PI = " << pi << endl;
}
Then result will be as
Approximation of the number PI through the sequence of the Nilakantha's series
Step(0):3.16667
Step(1):3.13333
Step(2):3.14524
Step(3):3.13968
Step(4):3.14271
Step(5):3.14088
Step(6):3.14207
Step(7):3.14125
Step(8):3.14184
Step(9):3.14141
Step(10):3.14174
Step(11):3.14148
Step(12):3.14168
Step(13):3.14152
Step(14):3.14165
Step(15):3.14154
Step(16):3.14164
Step(17):3.14156
Step(18):3.14162
Step(19):3.14157
Aproximated value of PI = 3.14157
Another implementation of the Leibniz's Series:
double pi = 0.0;
double first_denominator = 1.0;
double second_denominator = 3.0;
for (i = 0; i < N; ++i)
{
pi += 4.0 / first_denominator;
pi -= 4.0 / second_denominator;
first_denominator += 2;
second_denominator += 2;
}
By adding in pairs of terms, the need to switch signs has been removed.
I am still trying to learn algorithms, I have a homework. I must make an output
Sum of : 1/2 + 1/4 + 1/6 - 1/8 + 1/10 + 1/12
Result : 0.975
But output of my program
Sum of : 1/2 + 1/4 + 1/6-1/8 + 1/10 + 1/12
Result : 0.975
I dont know how to make space negative sign, if i use cout there will show twice negative sign.
my program
#include <iostream>
#include <math.h>
using namespace std;
int main ()
{
int i ,sign, p, q, n;
double x , S;
S=0;
cout << "Sum of :";
for (i=1; i <= 6; i++)
{
if ( (i % 4 == 0) && ( i > 1 ) ) // to make condition where the number become negative
{
sign = -1;
}
if ( ( i % 4 != 0 ) && ( i > 1 ) ) // to make condition where the number become positive
{
sign = 1;
cout << " + ";
}
if ( i == 1 ) // to prevent 1st number not show " + " symbol
{
sign =1;
}
p = sign*1;
q = ( 2 * ( i - 1 ) ) + 2;
cout << p << "/" << q;
x = ( 1.0 * p / q );
S = S + x;
}
cout << "\n" << S;
}
I realise that my program has too many operations which may be avoided, could u help me make it more effecient ?
So your
cout << p << "/" << q;
will always have that format if p is negative.
Instead (This workaround is intended to be simple)
if(p < 0) {
cout << " - " << p*-1 << "/" << q;
} else {
cout << p << "/" << q;
}
That should do it.
If you are looking to use recursion as you have indicated in the subject, then here is what you can also refer.
static void recurse(int i, int limit){
int sign = 0, p, q, n;
double x, S;
S =0;
if (i == 1) // to prevent 1st number not show " + " symbol
{
sign = 1;
cout << "Sum of : ";
}
else if (i< 1 || i> limit){
return ;
}
else {
sign = (i % 4 == 0) ? -1 : 1;
if (sign > 0){
cout << " + ";
}
else {
cout << " - ";
}
}
p = 1;
q = ( 2 * ( i - 1 ) ) + 2;
cout << p << "/" << q;
x = ( 1.0 * p / q );
S = S + x;
recurse(i+1, limit);
}
Call using:
int main ()
{
recurse(1, 6);
cout << "\n";
}
I am trying to create a c++ program that when I input two numbers (num1, combinationNum), it finds two numbers that multiply together to equal num1, but add together to equal combinationNum. It currently works for positive integers, but not negative. How do I make it work with negative integers? Also, If the equation isn't solvable, I would like it to print an error of some sort. Thanks!
Code:
//
// main.cpp
// Factor
//
// Created by Dani Smith on 2/13/14.
// Copyright (c) 2014 Dani Smith Productions. All rights reserved.
//
#include <iostream>
#include <cmath>
using namespace std;
void factors(int num, int comNum){
int a, b;
cout<<"The factors are ";
bool isPrime = true;
int root = (int)sqrt((double)num);
for(int i = 2; i <= root; i++){
if(num % i == 0 ){
isPrime = false;
//cout<<i<<",";
for(int x = 0; x<3; x++){
if(x==1){
a = i;
}
else if(x == 2){
b = i;
}
if(a + b == comNum){
cout << a << ", and " << b << ".";
}
}
}
}
//----------------------------------------
if(isPrime)cout<<"1 ";
cout<<endl;
}
int main(int argc, const char * argv[])
{
int num1 = 0, num2 = 0, multiple = 0, combinationNum = 0, output1 = 0, output2 = 0;
cout << "What number do you want to factor?\n";
cin >> num1;
cout << "What do you want them to add to?\n";
cin >> combinationNum;
factors(num1, combinationNum);
return 0;
}
To solve:
x + y == a
x * y == b
You have to solve
y == a - x
x * x - a * x + b == 0
So with delta == a * a - 4 * b, if delta positive, the solutions are
x1 = (a + sqrt(delta)) / 2
x2 = (a + sqrt(delta)) / 2
The code : (https://ideone.com/qwrSwa)
void solve(int sum, int mul)
{
std::cout << "solution for x + y = " << sum << std::endl
<< " x * y = " << mul << std::endl;
const int delta = sum * sum - 4 * mul;
if (delta < 0) {
std::cout << "No solution" << std::endl;
return;
}
const float sqrtdelta = sqrtf(delta);
const float x1 = (sum + sqrtdelta) / 2.f;
const float x2 = (sum - sqrtdelta) / 2.f;
std::cout << "x = " << x1 << ", y = " << sum - x1 << std::endl;
if (delta != 0) {
std::cout << "x = " << x2 << ", y = " << sum - x2 << std::endl;
}
}
# include <iostream>
# include <math.h>
# include <cstdlib>
using namespace std;
double cosin_value( double value);
double sin_value( double value);
double big_degree (double value);
double big_radian (double value);
double x;
double value;
double degree;
double radian;
const double PI = 3.14159;
char choice;
char yes ;
int main()
{
cout << "Please enter an angle value => ";
cin >> value;
cout << "Is the angle in Degree or Radian?" << endl;
cout << "\t" << "Type D if it is in Degree" << endl;
cout << "\t" << "Type R if it is in Radian" << endl;
cout << "Your response => ";
cin >> choice; //degree or radian?
cout.setf(ios::fixed);
cout.setf(ios::showpoint);
cout.precision(10);
if (choice == 'D' || choice == 'd')
{
big_degree (value);
cout << " " << "sin(x) = " << "\t" << sin_value(degree) << endl;
cout << " " << "cos(x) = " << "\t" << cosin_value(degree) << endl;
cout << " " << "tan(x) = " << "\t" << sin_value(degree)/cosin_value(degree) << endl;
}
else if (choice == 'R' || choice == 'r')
{
cout << " " << "sin(x) = " << "\t" << sin_value(radian) << endl;
cout << " " << "cos(x) = " << "\t" << cosin_value(radian) << endl;
cout << " " << "tan(x) = " << "\t" << sin_value(radian)/cosin_value(radian) << endl;
}
return 0;
}
// Sine,cosine functions
// angle -360<value<360
double sin_value( double value)
{
int count=1;
double sine, num, dem, sign, term;
sine=0;
sign = 1;
num = value;
dem = count;
while ( count <= 20 )
{
term = (num/dem);
sine = sine + term*sign;
num = num*value*value;
count = count + 2;
dem = dem * count * (count-1);
sign = -sign;
}
return (sine);
}
double cosin_value( double value)
{
int count=0;
double cosine, num, dem, sign, term;
cosine=0;
sign = 1;
num = 1;
dem = 1;
while ( count <= 20 )
{
term = (num/dem);
cosine = cosine + term*sign;
num = num*value*value;
count = count + 2;
dem = dem * count * (count-1);
sign = -sign;
}
return (cosine);
}
double big_degree (double value)
{
int result;
const int angle=360;
if (value >= 360 || value <= -360)
{
result=value/angle;
degree=(value-(result* angle))*PI/180;
}
else
{
degree = (value*PI)/180;
}
return (degree);
}
double big_radian (double value)
{
int result;
if (value >= 2*PI || value <= -2*PI)
{
result=value/(2*PI);
radian=(value-(result* 2*PI));
}
else
{
radian = value;
}
return (radian);
}
Hi, this is basically the whole program I wrote for calculating trigonometric value using the extent knowledge I knew in C++ as a beginner. For a better view, you can refer to this link regarding my code above :codepad.org
the line starting from line 114 onwards are the function that I created. There's a problem there where how can I compute my cosx to be 0 when the value is 90 degree or pi/2 radian?
since the program will still calculate tanx for me even the value is 90 degree.
Let's say by giving value 90 degree to the program, it will give me the value of 0.0000013268 instead of 0.000000
sorry, since I'm just a beginner, the code will look weird for you guys.
I appreciate your guides!
double big_degree(double value) means when the value is >= 360 or <= -360*
I do not allocate any heap space in my brain for digits of pi, but I do remember that atan(1) == pi / 4.
Change your PI constant like so:
const double PI = atan(1) * 4;
Taking your code, making that change, I get
Please enter an angle value => 90
Is the angle in Degree or Radian?
Type D if it is in Degree
Type R if it is in Radian
Your response => d
sin(x) = 1.0000000000
cos(x) = 0.0000000000
tan(x) = 15555226593901466.0000000000
const double PI = 3.14159;
The more precise you make this definition, the more close to 0 will the value of cos PI/2 get!
If you get the input itself in radians, there also the same criteria applies.
The problem isn't your code. The input you have given is not sufficiently accurate. Calculate the proper value of pi/2, and you will get a sufficiently accurate value.Also, if you want to round off the values you can use#rounded off value=Math.Round(#old value, 4)
My soulution:
double mySin(double x)
{
if (fmod(x, std::numbers::pi) == 0)
return 0;
return sin(fmod(x, std::numbers::pi * 2.0));
}
double myCos(double x) { return mySin(x + std::numbers::pi / 2); }
myCos(std::numbers::pi / 2) == 0 //True
myCos(std::numbers::pi) == -1 //True
myCos(std::numbers::pi * 2) == 1 //True