trunc_divide()#

relationalai.std.math
#trunc_divide(numerator: Number|Producer, denominator: Number|Producer) -> Expression

Performs truncated division of numerator by denominator, rounding towards zero and returning a value of the same type as numerator. If numerator or denominator is a Producer, trunc_divide() acts as a filter and removes any non-numeric values from the producer. Must be called in a rule or query context.

Parameters#

NameTypeDescription
numeratorProducer or Python Number objectThe numerator of the division.
denominatorProducer or Python Number objectThe denominator of the division.

Returns#

An Expression object.

Example#

Use trunc_divide() to perform division and truncate the result towards zero:

#import relationalai as rai
from relationalai.std import math


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

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

with model.rule():
    Measurement.add(id=1).set(value=170.1, divisor=2)
    Measurement.add(id=2).set(value=-180.5, divisor=2)
    Measurement.add(id=3).set(value="INVALID", divisor=3)  # Non-numeric value


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

# Set a truncated_result property to the result of trunc_divide for each measurement.
with model.rule():
    measurement = Measurement()
    trunc = math.trunc_divide(measurement.value, measurement.divisor)
    measurement.set(truncated_result=trunc)

# Since trunc_divide() filters out non-numeric values, the truncated_result property
# is not set for the measurement with id=3.
with model.query() as select:
    measurement = Measurement()
    response = select(measurement.id, measurement.truncated_result)

print(response.results)
#    id  truncated_result
# 0   1              85.0
# 1   2             -90.0
# 2   3               NaN

See Also#