Bug 15454 - numerical issue in contour.default
numerical issue in contour.default
Status: CLOSED FIXED
Product: R
Classification: Unclassified
Component: Graphics
R 3.0.1
x86_64/x64/amd64 (64-bit) Windows 64-bit
: P5 normal
Assigned To: R-core
Depends on:
Blocks:
  Show dependency treegraph
 
Reported: 2013-09-13 16:09 UTC by Thomas Petzoldt
Modified: 2013-10-20 14:06 UTC (History)
1 user (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Thomas Petzoldt 2013-09-13 16:09:17 UTC
contour() may fail with matrices containing both "usual" and "very small" numbers.

Example:
########

> set.seed(357)
> z <- matrix(c(runif(50, -1, 1), runif(50, -1e-190, 1e-190)), nrow = 10)
> contour(z)
Error in contour.default(z) : k != 2 or 4



The error occurred only on 64bit (Windows and Linux), not on 32bit and I assume that the reason may be in:


// file: src/main/contour-common.h

static int ctr_intersect(double z0, double z1, double zc, double *f)
{
    if ((z0 - zc) * (z1 - zc) < 0.0) {
	*f = (zc - z0) / (z1 -	z0);
	return 1;
    }
    return 0;
}
Comment 1 Duncan Murdoch 2013-10-20 14:06:21 UTC
Yes, you located the problem.  With the very small numbers the test was inconsistent:  the product of two non-zero numbers could give zero.  I've replaced the test

  ((z0 - zc) * (z1 - zc) < 0.0)

with:

  ( (z0 < zc) != (z1 < zc) && z0 != zc && z1 != zc )

I think this one is safe; it works on your example.

I'll commit this to R-devel and R-patched soon.