0

I am a beginner with c++, I don't understand why I don't get the same result as when I use my calculator.

eg. radius=2, a(angle)=60, according to my calculator it should be 118.2679496, with the program I get 120.61.

The formula for the area of circular segment is r^2/2*(a-sin(a))

Thank you for the help!

#include <iostream>
#include <cmath>
using namespace std;
int main(){
    double r, a, area;
    cout<< "fill the radius: "<<"\n";
    cin >> r;
    cout <<"fill the angle: "<<"\n";
    cin >> a;
    area = r*r/2*(a-sin(a));
    cout <<"The area is "<< area <<"."<<"\n";
    return 0;
}
Rodomo
  • 5
  • 2
  • 5
    `sin()` takes its argument in radians. – Sam Varshavchik Oct 06 '18 at 00:18
  • 1
    Possible duplicate of [c++ Sin and Cos](https://stackoverflow.com/questions/31502120/c-sin-and-cos) – Nicholas Pipitone Oct 06 '18 at 00:41
  • @NicholasPipitone - that's not quite a duplicate (although it's very useful, all the same). This question is due to a problem of forgetting to convert degrees to radians. The question you linked was about loss of precision/accuracy when doing that conversion. – Peter Oct 06 '18 at 01:00
  • For area of segment, use `area = r*r * (a*3.14/360 - sin(a* 3.14/180)/2);` if angle is in degrees. `sin()` function expects angle in radian, so convert it. Are you sure the calculator result is correct? – seccpur Oct 06 '18 at 01:02
  • @Peter I fixed the duplicate's title to be more telling. Not it's clear that though the problem is not quite the same (in that case, the OP did convert units), that answer still applies. – ivan_pozdeev Oct 06 '18 at 04:26
  • Your calculator will probably compute the same result if you switch its mode to `Rad`. ;-) I assume that a calculator with `sin` and `cos` provides also a choice of angle units. – Scheff's Cat Oct 06 '18 at 05:56
  • Possible duplicate of [How to find the area of a segment of a circle if we know the radius and an angle?](https://stackoverflow.com/q/35807728/608639) – jww Oct 06 '18 at 07:31
  • Hello, thank you all, I have resolved now by using the right formula, i get the wrong result because I didn't know a ist for radian, thought it is the angle. – Rodomo Oct 07 '18 at 00:00

0 Answers0