floor()#
relationalai.std.math
#floor(number: Number|Producer) -> Expression
Calculates the floor of a number, which is the largest whole number less than or equal to the number.
If number
is a Producer
, floor()
acts as a filter and removes any non-numeric values from the producer.
Must be called in a rule or query context.
Parameters#
Name | Type | Description |
---|---|---|
number | Producer or Python Number object | The number to calculate the floor of. |
Returns#
An Expression
object.
Example#
Use floor()
to calculate the floor of a number:
#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.1)
Person.add(id=2).set(height_cm=180.9)
Person.add(id=3).set(height_cm="INVALID") # Non-numeric height
# =======
# EXAMPLE
# =======
# Set a height_rounded property to the floor of each person’s height.
with model.rule():
person = Person()
person.set(height_rounded=math.floor(person.height_cm))
# Since floor() filters out non-numeric values, the height_rounded property is not
# set for the person with id=3.
with model.query() as select:
person = Person()
response = select(person.id, person.height_rounded)
print(response.results)
# id height_rounded
# 0 1 170.0
# 1 2 180.0
# 2 3 NaN
For negative numbers, floor()
rounds towards negative infinity:
#with model.query() as select:
response = select(math.floor(-5.5))
print(response.results)
# v
# 0 -6.0