Help convert degrees to radians

Hi, I need some help writing this small function. It's supposed to accept a double value as degrees and return the radian equivalent. If the degrees is positive, it will return a value from 0 and 2 * PI, but not including 2 * PI. If the input is negative, it will return a value between 0 and -2 * PI.

It seemed simple but when I tried to add in the negative values I messed it up. Could I have a bit of help? Thank you.


1
2
3
double degreesToRadians(double angle_in_degrees){
	return angle_in_degrees * (PI/180.0);
}
The function is mathematically correct, although it will return a value between positive and negative infinity, not in (-tau;tau).
Works perfectly fine for me -

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>
using namespace std;

#define PI 3.14159265359

double degreesToRadians(double angle_in_degrees){
	return angle_in_degrees * (PI / 180.0);
}

int main()
{

	cout << degreesToRadians(-180) << endl;
	cout << degreesToRadians(-360) << endl;
	cout << degreesToRadians(122) << endl;
	cout << degreesToRadians(-22) << endl;
	
	return 0;
}
Last edited on
When I submit it to my homework website, it fails the automatic tests the site runs. I get the following errors returned:
Running cxxtest tests (<enough tests>)
In MyDeg2Rad::test6:
mylib_test.h:82: Error: Expected (degreesToRadians(0.0 + 360.0) == 0.0) up to 0.01 (0.0100), found (6.2831 != 0.0000)
In MyDeg2Rad::test7:
mylib_test.h:86: Error: Expected (degreesToRadians(45 + 360.0) == PI/4) up to 0.01 (0.0100), found (7.0685 != 0.7853)
In MyDeg2Rad::test8:
mylib_test.h:90: Error: Expected (degreesToRadians(90 + 360.0) == PI/2) up to 0.01 (0.0100), found (7.8539 != 1.5707)
In MyDeg2Rad::test9:
mylib_test.h:94: Error: Expected (degreesToRadians(180 + 360.0) == PI) up to 0.01 (0.0100), found (9.4247 != 3.1415)
In MyDeg2Rad::test10:
mylib_test.h:98: Error: Expected (degreesToRadians(270 + 360.0) == PI*3/2) up to 0.01 (0.0100), found (10.9955 != 4.7123)
In MyDeg2Rad::test11:
mylib_test.h:102: Error: Expected (degreesToRadians(-360.0) == 0.0) up to 0.01 (0.0100), found (-6.2831 != 0.0000)
Topic archived. No new replies allowed.