Skip to content

Float comparisons can lead to erroneous exceptions #726

@chanokin

Description

@chanokin

When computing the shape of a Grid2D, the number of x units, nx, comes from a floating-point operation. Checking if it's equal to 0 will often return false (e.g. 1e-100 != 0) even if the value should be interpreted as 0.

PyNN/pyNN/space.py

Lines 245 to 252 in 4feecd7

def calculate_size(self, n):
"""docstring goes here"""
nx = math.sqrt(n * self.aspect_ratio)
if n % nx != 0:
raise Exception("Invalid size: n=%g, nx=%d" % (n, nx))
nx = int(round(nx))
ny = n // nx
return nx, ny

I believe the comparison in line 248 should be changed to not math.isclose(n % nx, 0) with the precision of your choosing or just cast nx to an integer before the modulo operation is performed.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions