isclose()#

relationalai.std.math
#isclose(x: Number|Producer, y: Number|Producer, tolerance: Number|Producer = 1e-9) -> Expression

Filters pairs of numbers to those that are approximately equal by comparing their absolute difference to a specified tolerance. If x, y, or tolerance is a Producer, isclose() filters out any non-numeric values from the producer. Must be called in a rule or query context.

Parameters#

NameTypeDescription
xProducer or Python Number objectThe first number to compare.
yProducer or Python Number objectThe second number to compare.
toleranceProducer or Python Number objectThe maximum difference allowed between x and y. (Default: 1e-9)

Returns#

An Expression object.

Example#

Use isclose() to filter pairs of numbers that are approximately equal:

#import relationalai as rai
from relationalai.std import math


# =====
# SETUP
# =====

model = rai.Model("MyModel")
Person = model.Type("Person")

with model.rule():
    Person.add(id=1).set(height_cm=170)
    Person.add(id=2).set(height_cm=180)
    Person.add(id=3).set(height_cm=180.0001)


# =======
# EXAMPLE
# =======

# Find pairs of people with approximately equal heights.
with model.query() as select:
    person1, person2 = Person(), Person()
    person1 < person2  # Ensure unique pairs
    math.isclose(person1.height_cm, person2.height_cm, tolerance=1e-3)
    response = select(person1.id, person2.id)

print(response.results)
#    id1   id2
# 0    2     3

See Also#