Skip to content

Fixed FT::atan2 and FTA::atan2 when x<=y#2

Open
Sonotsugipaa wants to merge 1 commit intodivideconcept:masterfrom
Sonotsugipaa:master
Open

Fixed FT::atan2 and FTA::atan2 when x<=y#2
Sonotsugipaa wants to merge 1 commit intodivideconcept:masterfrom
Sonotsugipaa:master

Conversation

@Sonotsugipaa
Copy link
Copy Markdown

@Sonotsugipaa Sonotsugipaa commented May 11, 2022

Before this change, FT::atan2(0.3, -0.05) would return 80.4648, while std::atan2(0.3, -0.05) returns ~99.4623, the correct result; FTA::atan2 behaved the same way.

In order to test the accuracy of these function, I have written and used the following single-file program:

#include <iostream>

#include "fasttrigo.h"


constexpr float pi = 3.141592653589793;

constexpr float rad(float deg) { return deg * pi / 180.0f; }

constexpr float deg(float rad) { return rad * 180.0f / pi; }

constexpr float roundErr(float x) {
	constexpr float prec = 1000000.0f;
	return std::floor(x * prec) / prec;
}


int main(int, char**) {
	static constexpr size_t steps = 720*2;
	static constexpr float low = rad(-360.0f);
	static constexpr float high = rad(+360.0f);
	static constexpr float step = (high - low) / float(steps);
	for(float angle = low; angle <= high; angle += step) {
		float x = std::cos(angle) * 4.0f;
		float y = std::sin(angle) * 4.0f;
		float ftAtan2 = FT::atan2(y,x);
		float stdAtan2 = std::atan2(y,x);
		float error = std::abs(ftAtan2 - stdAtan2);
		if(error > 0.0017) std::cout << "\033[1;31m";
		std::cout
			<< "Angle " << deg(angle)
			<< "   Atan2 " << ftAtan2 << " | " << stdAtan2
			<< "   Error " << roundErr(error) << "\033[m\n";
	}
}

@Sonotsugipaa Sonotsugipaa marked this pull request as ready for review May 12, 2022 10:54
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant