sqrt()#

relationalai.std.math
#sqrt(value: Number|Producer) -> Expression

Calculates the square root of value. If value is a Producer, sqrt() acts as a filter and removes any non-numeric or negative values from the producer. Must be called in a rule or query context.

Parameters#

NameTypeDescription
valueProducer or Python Number objectThe number to take the square root of.

Returns#

An Expression object.

Example#

Use sqrt() to calculate the square root of a number:

#import relationalai as rai
from relationalai.std import math


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

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

with model.rule():
    Account.add(id=1).set(balance=100.00)
    Account.add(id=2).set(balance=-25.00)  # Negative balance
    Account.add(id=3).set(balance="INVALID")  # Non-numeric balance


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

# Set a balance_sqrt property to the square root of each account's balance.
with model.rule():
    account = Account()
    account.set(balance_sqrt=math.sqrt(account.balance))

# Since sqrt() filters out non-numeric or negative values, the balance_sqrt
# property is not set for accounts with IDs 2 and 3.
with model.query() as select:
    account = Account()
    response = select(account.id, account.balance_sqrt)

print(response.results)
#    id  balance_sqrt
# 0   1          10.0
# 1   3           NaN
# 2   4           NaN

See Also#