From 1b6c14691b6437bec5522300a4cbb31d8037d441 Mon Sep 17 00:00:00 2001 From: Godzil Date: Wed, 4 Mar 2020 16:15:49 +0000 Subject: [PATCH] fmod is not mathematically valid. Replace it by a correct one. --- source/include/math_helper.h | 5 +++++ source/pattern/checkerspattern.h | 2 +- source/pattern/ringpattern.h | 2 +- source/pattern/strippattern.h | 2 +- source/uvpattern/uv_checkers.h | 2 +- 5 files changed, 9 insertions(+), 4 deletions(-) diff --git a/source/include/math_helper.h b/source/include/math_helper.h index 7318233..cde096c 100644 --- a/source/include/math_helper.h +++ b/source/include/math_helper.h @@ -23,4 +23,9 @@ double max3(double a, double b, double c); double frand(); +static double modulo(double a, double b) +{ + return a - floor(a/b) * b; +} + #endif /* DORAYME_MATH_HELPER_H */ diff --git a/source/pattern/checkerspattern.h b/source/pattern/checkerspattern.h index 1556fe3..d04f9e7 100644 --- a/source/pattern/checkerspattern.h +++ b/source/pattern/checkerspattern.h @@ -20,7 +20,7 @@ public: { double value = floor(point.x) + floor(point.y) + floor(point.z); - return (fmod(value, 2) == 0)?this->a:this->b; + return (modulo(value, 2) == 0)?this->a:this->b; } void dumpMe(FILE *fp) { diff --git a/source/pattern/ringpattern.h b/source/pattern/ringpattern.h index 79fc913..402b87c 100644 --- a/source/pattern/ringpattern.h +++ b/source/pattern/ringpattern.h @@ -22,7 +22,7 @@ public: double value = floor(sqrt(squared)); - return (fmod(value, 2) == 0)?this->a:this->b; + return (modulo(value, 2) == 0)?this->a:this->b; } void dumpMe(FILE *fp) { diff --git a/source/pattern/strippattern.h b/source/pattern/strippattern.h index f7732e9..e705790 100644 --- a/source/pattern/strippattern.h +++ b/source/pattern/strippattern.h @@ -21,7 +21,7 @@ public: Colour patternAt(Tuple point) { - if (fmod(floor(point.x), 2) == 0) + if (modulo(floor(point.x), 2) == 0) { return this->a; } diff --git a/source/uvpattern/uv_checkers.h b/source/uvpattern/uv_checkers.h index c03d686..44bd96a 100644 --- a/source/uvpattern/uv_checkers.h +++ b/source/uvpattern/uv_checkers.h @@ -21,7 +21,7 @@ public: double u2 = floor(u * this->width); double v2 = floor(v * this->height); - if (fmod((u2 + v2), 2) == 0) + if (modulo((u2 + v2), 2) == 0) { return this->a; }