0

I am working on a trigonometric functions calculator. The user would choose a function for example sin() then input a constant for example 1, then a lower boundary and an upper boundary.

The calculator should then give all values of theta between the lower boundary and the upper boundary that would give the constant when input into the sine function.

For some reason, however, not all the theta values would be printed out and sometimes, especially with the tan function, no values of theta would be printed out.

For example, when I choose the tan function and input the constant as 1, then choose a lower boundary of 0 and an upper boundary of 1000. No theta values are printed out.

I can't find the error in my program. please help me.

#include <iostream>
#include <string>
#define _USE_MATH_DEFINES
#include <cmath>
int operation; //This is the variable to store the number of the trig. function the user choose
double constant;
double lower_boundary;
double upper_boundary;
double a; //This variable is for holding the converted degrees to radians

//This is the function for getting the information needed to start giving out the possible theta values
void get_info(){
        std::cout<<"Insert the value of the constant: ";
        std::cin>>constant;
        std::cout<<"Insert the value of the lower boundary in degrees\n( this is from where you will start getting possible values of theta ): ";
        std::cin>>lower_boundary;
        std::cout<<"Insert the value of the upper boundary in degrees: ";
        std::cin>>upper_boundary;
        std::cout<<"\nHere are your possible values for theta :\n";
}

void decide (){
    std::cout<<"\nInsert the number of the trigonometric function you want:\n";
    std::cout<<"1. Sin()\n";
    std::cout<<"2. Cos()\n";
    std::cout<<"3. Tan()\n";
    std::cin>>operation;

    if (operation==1){
        get_info();
        for (double i = lower_boundary; i<=upper_boundary; i++)
        {   a = i*(M_PI/180);
            if (sin(a)==constant){std::cout<<i<<"\n";}
            }
    }

    else if (operation==2){
        get_info();
        for (double i = lower_boundary; i<=upper_boundary; i++)
        {   a = i*(M_PI/180);
            if (cos(a)==constant){std::cout<<i<<"\n";}
            }
    }

    else if (operation==3){
        get_info();
        for (double i = lower_boundary; i<=upper_boundary; i++)
        {   a = i*(M_PI/180);
            if (tan(a)==constant){std::cout<<i<<"\n";}
            }
    }

    else {std::cout<<"INVALID INPUT ! Please enter the number of an existing operation.\n";}
}

int main() {
    std::cout<<"----------------------------------------------------------------------------\n";
    std::cout<<"TRIGONOMETRIC EQUATIONS CALCULATOR\n";
    std::cout<<"----------------------------------------------------------------------------\n\n";
    while (true){decide();}
}
saiffarid
  • 43
  • 1
  • 2
  • 9
  • Please [edit] your question and add a simple example of input that triggers the problem. Also show the expected and the actual output. – Jabberwocky Feb 13 '20 at 15:21
  • 4
    [Comparing Floating-Point Numbers Is Tricky](https://bitbashing.io/comparing-floats.html) – Ted Lyngmo Feb 13 '20 at 15:21
  • That's floating point for you! – Acorn Feb 13 '20 at 15:22
  • 4
    [This is a must read if you are new to floating point math](https://stackoverflow.com/questions/588004/is-floating-point-math-broken) – NathanOliver Feb 13 '20 at 15:22
  • 4
    [What Every Computer Scientist Should Know About Floating-Point Arithmetic](https://docs.oracle.com/cd/E19957-01/806-3568/ncg_goldberg.html) – Jesper Juhl Feb 13 '20 at 15:24
  • IMHO, you should be using `switch` and `case` to handle menu selections. – Thomas Matthews Feb 13 '20 at 15:56
  • How does your program terminate? You should provide a menu option for exiting the program. – Thomas Matthews Feb 13 '20 at 15:58
  • @NathanOliver I have tried changing the conditional statements to the way they were in the article you mentioned. For example, for the tan function, I made it `if (abs(tan(a)-constant)<0.0001){std::cout< – saiffarid Feb 15 '20 at 16:12

0 Answers0