Skip to content

relationalai.std.math.log2()

log2(value: Number|Producer) -> Expression

Calculates the base-2 logarithm of value. If value is a Producer, log2() filters out any non-numeric or negative values from the producer. Must be called in a rule or query context.

NameTypeDescription
valueProducer or Python Number objectThe number to take the base-2 logarithm of.

An Expression object.

Use log2() to calculate the base-2 logarithm 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(name="Alice", age=16)
Person.add(id=2).set(name="Bob", age=-8) # Negative age
Person.add(id=3).set(name="Carol", age="INVALID") # Non-numeric age
# =======
# EXAMPLE
# =======
# Set an age_log2 property to the base-2 logarithm of each person’s age.
with model.rule():
person = Person()
person.set(age_log2=math.log2(person.age))
# Since log2() filters out negative and non-numeric values, the age_log2
# property is not set for Bob or Carol.
with model.query() as select:
person = Person()
response = select(person.id, person.name, person.age_log2)
print(response.results)
# id name age_log2
# 0 1 Alice 4.0
# 1 2 Bob NaN
# 2 3 Carol NaN