-1

i know this sounds stupid but how can i make a program that will give the value of a trignometric function like sin30, sin45 using int function.I had this idea and i dont know if its possible or not.If anyone can try it , it would really help.I currently have tried something like this....and i certainly know its horribly wrong.i am new to this coding so dont have any idea of what to do enter image description here

  • 1
    Turbo C++ was awesome when it first came out but it's been superceded by other tools on Windows such as MSVC. Yes it is possible to build your own trigonometric functions, although to match the production ones is a far from trivial task. – Bathsheba Apr 01 '20 at 12:21
  • If you want to use int then you need to multiply with some precision, for int 1/2 is zero, With 3 decimals 1000/2 is 500. – Surt Apr 01 '20 at 12:35
  • google: fixed point precision, CORDIC , Taylor series, Chebyshev series. Fixed point convert your non integer values into integer ones (if you insist on integers) and the rest provides you with algorithms and equations to compute basic trig functions using only basic operators like `+,-,*,/`. I recomend to start with taylor series for `sin(x)` ... Here example [Algorithm for calculating trigonometry, logarithms or something like that. ONLY addition-subtraction](https://stackoverflow.com/a/55413815/2521214) but it might be too much for a rookie to grasp – Spektre Apr 08 '20 at 13:12
  • Also do not post image of source code! Post the code itself (as code block text ) so anyone can copy use/test it directly and not retyping it .... – Spektre Apr 08 '20 at 13:16

1 Answers1

0

Use a lookup table:

#include <iostream.h>

void main() {
    const double sin30 = 0.5;
    const double sin45 = 0.707107;
    const double sin60 = 0.866025;
    const double sin75 = 0.965926;
    const double sin90 = 1;

/*
    const int sin30 = 0;
    const int sin45 = 0;
    const int sin60 = 0;
    const int sin75 = 0;
    const int sin90 = 1;
*/

    cout << "sin 30: " << sin30
         << "\nsin 45: " << sin45
         << "\nsin 60: " << sin60
         << "\nsin 75: " << sin75
         << "\nsin 90: " << sin90 << "\n";
    return 0;
}

I used double because all values but sin90 are 0 as int.

Thomas Sablik
  • 16,127
  • 7
  • 34
  • 62
  • hey i tried to compile this program but it shows an error like this "$g++ -o main *.cpp main.cpp: In function ‘int main()’: main.cpp:10:5: error: ‘cout’ was not declared in this scope cout << "sin 30: " << sin30 ^~~~" – Aditya Borkar Apr 01 '20 at 15:36
  • @AdityaBorkar this is an answer for Turbo C++ because you asked about Turbo C++. For gcc you have to use `std::cout` instead and you have to replace `iostream.h` by `iostream`. `void main` is not allowed in modern (after 1998) C++. It has to be `int main`. – Thomas Sablik Apr 01 '20 at 19:40