From d32e1c12beee8caac3f28cf544bc1de47c40b9ae Mon Sep 17 00:00:00 2001 From: Fabian Homborg Date: Mon, 26 Jul 2021 18:39:09 +0200 Subject: [PATCH] tinyexpr: Check for nan in ncr Turns out this takes ages. Fixes #8170 --- src/tinyexpr.cpp | 2 ++ tests/checks/math.fish | 6 ++++++ 2 files changed, 8 insertions(+) diff --git a/src/tinyexpr.cpp b/src/tinyexpr.cpp index a6ec302bd..fe226781d 100644 --- a/src/tinyexpr.cpp +++ b/src/tinyexpr.cpp @@ -156,6 +156,8 @@ static double fac(double a) { /* simplest version of fac */ } static double ncr(double n, double r) { + // Doing this for NAN takes ages - just return the result right away. + if (std::isnan(n)) return INFINITY; if (n < 0.0 || r < 0.0 || n < r) return NAN; if (n > UINT_MAX || r > UINT_MAX) return INFINITY; unsigned long int un = static_cast(n), ur = static_cast(r), i; diff --git a/tests/checks/math.fish b/tests/checks/math.fish index b87a643e0..5ca712fea 100644 --- a/tests/checks/math.fish +++ b/tests/checks/math.fish @@ -219,3 +219,9 @@ math pow 2, cos -pi # i.e. 4 math pow 2 x cos'(-pi)', 2 # CHECK: 4 + +# This used to take ages, see #8170. +# If this test hangs, that's reintroduced! +math 'ncr(0/0, 1)' +# CHECKERR: math: Error: Result is infinite +# CHECKERR: 'ncr(0/0, 1)'