sign()#

relationalai.std.math
#sign(number: Number|Producer) -> Expression

Calculates the sign of a number, returning 1 if the number is positive, -1 if the number is negative, and 0 if the number is zero. If number is a Producer, sign() acts as a filter and removes non-numeric values from the producer. Must be called in a rule or query context.

Parameters#

NameTypeDescription
numberProducer or Python Number objectThe number to determine the sign of.

Returns#

An Expression object.

Example#

Use sign() to determine the sign of a number.

#import relationalai as rai
from relationalai.std import math


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

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

with model.rule():
    Transaction.add(id=1).set(amount=50.0)
    Transaction.add(id=2).set(amount=-25.0)
    Transaction.add(id=3).set(amount="INVALID")  # Non-numeric amount


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

# Set a sign_amount property to the sign of each transaction's amount.
with model.rule():
    transaction = Transaction()
    transaction.set(sign_amount=math.sign(transaction.amount))

# Since sign() filters out non-numeric values, the sign_amount property is not
# set for the transaction with id=3.
with model.query() as select:
    transaction = Transaction()
    response = select(transaction.id, transaction.sign_amount)

print(response.results)
#    id  sign_amount
# 0   1            1
# 1   2           -1
# 2   3          NaN

See Also#