From dd4b11063a19be076cf9c89000da7d9451dfc2b9 Mon Sep 17 00:00:00 2001 From: John Cupitt Date: Fri, 16 Jul 2021 13:06:15 +0100 Subject: [PATCH] special case for **-1 Since it's very common. See https://github.com/libvips/libvips/discussions/2352 --- libvips/arithmetic/math2.c | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/libvips/arithmetic/math2.c b/libvips/arithmetic/math2.c index dd3e5323..b1d9ca86 100644 --- a/libvips/arithmetic/math2.c +++ b/libvips/arithmetic/math2.c @@ -137,11 +137,16 @@ vips_math2_build( VipsObject *object ) double left = (double) (X); \ double right = (double) (E); \ \ - /* Division by zero! Difficult to report tho' \ + /* Special case for **-1 and **0.5, since they are so common. Also \ + * watch for /0. \ */ \ - (Y) = (right == 0.5) \ - ? sqrt( left ) \ - : (left == 0.0 && right < 0.0) ? 0.0 : pow( left, right ); \ + (Y) = (left == 0.0) \ + ? 0.0 \ + : (right == -1) \ + ? 1.0 / left \ + : (right == 0.5) \ + ? sqrt( left ) \ + : pow( left, right ); \ } #define WOP( Y, X, E ) POW( Y, E, X )