How to wrap a float to the interval [-pi, pi)

Modulo function updated to handle boundary cases as noted by aka.nice and arr_sea: static const double _PI= 3.1415926535897932384626433832795028841971693993751058209749445923078164062862089986280348; static const double _TWO_PI= 6.2831853071795864769252867665590057683943387987502116419498891846156328125724179972560696; // Floating-point modulo // The result (the remainder) has same sign as the divisor. // Similar to matlab’s mod(); Not similar to fmod() – Mod(-3,4)= 1 fmod(-3,4)= -3 template<typename T> T Mod(T … Read more

How do you compare float and double while accounting for precision loss?

Be extremely careful using any of the other suggestions. It all depends on context. I have spent a long time tracing bugs in a system that presumed a==b if |a-b|<epsilon. The underlying problems were: The implicit presumption in an algorithm that if a==b and b==c then a==c. Using the same epsilon for lines measured in … Read more

Is floating point precision mutable or invariant?

The precision is fixed, which is exactly 53 binary digits for double-precision (or 52 if we exclude the implicit leading 1). This comes out to about 15 decimal digits. The OP asked me to elaborate on why having exactly 53 binary digits means “about” 15 decimal digits. To understand this intuitively, let’s consider a less-precise … Read more