Math functions

Django ORM PostgreSQL May 22, 2026 python

Numeric functions over one book's price/pages: Abs, Ceil, Floor, Round(x, n), Sign, Mod, Power, Sqrt, Exp, Ln, Log(base, x).

python
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# ======================================================================
# Numeric functions over one book's price/pages: `Abs`, `Ceil`, `Floor`,
# `Round(x, n)`, `Sign`, `Mod`, `Power`, `Sqrt`, `Exp`, `Ln`, `Log(base, x)`.
# All evaluated by PostgreSQL.
# ======================================================================

# ----------------------------------------------------------------------
# numeric functions over the LOTR row
# ----------------------------------------------------------------------

# Django:
Book.objects.filter(title="The Lord of the Rings").annotate(
    absv=Abs("price"), ceil=Ceil("price"), floor=Floor("price"),
    round1=Round("price", 1), sign=Sign("price"), mod=Mod("pages", Value(100)),
    power=Power("price", Value(2)), sqrt=Sqrt("pages"), exp=Exp(Value(1)),
    ln=Ln("pages"), log10=Log(Value(10), "pages"), pi=Pi(),
    sin=Sin(Value(1.0)), cos=Cos(Value(1.0)), tan=Tan(Value(1.0)),
    cot=Cot(Value(1.0)), asin=ASin(Value(0.5)), acos=ACos(Value(0.5)),
    atan=ATan(Value(1.0)), atan2=ATan2(Value(1.0), Value(1.0)),
    degrees=Degrees(Value(3.14159265)), radians=Radians(Value(180.0)),
    rand=Random(),
)

# SQL:
#   SELECT ABS("examples_book"."price") AS "absv", CEILING("examples_book"."price") AS "ceil", FLOOR("examples_book"."price") AS "floor", ROUND("examples_book"."price", 1) AS "round1", SIGN("examples_book"."price") AS "sign", MOD("examples_book"."pages", 100) AS "mod", POWER("examples_book"."price", 2) AS "power", SQRT("examples_book"."pages") AS "sqrt", EXP(1) AS "exp", LN("examples_book"."pages") AS "ln", LOG(10, "examples_book"."pages") AS "log10", PI() AS "pi", SIN(1.0) AS "sin", COS(1.0) AS "cos", TAN(1.0) AS "tan", COT(1.0) AS "cot", ASIN(0.5) AS "asin", ACOS(0.5) AS "acos", ATAN(1.0) AS "atan", ATAN2(1.0, 1.0) AS "atan2", DEGREES(3.14159265) AS "degrees", RADIANS(180.0) AS "radians", RANDOM() AS "rand"
#   FROM "examples_book"
#   WHERE "examples_book"."title" = 'The Lord of the Rings'
#   ORDER BY "examples_book"."id" ASC
#   LIMIT 1

# Result:
#   Abs(price)        -> 29.00
#   Ceil(price)       -> 29
#   Floor(price)      -> 29
#   Round(price, 1)   -> 29.0
#   Sign(price)       -> 1
#   Mod(pages, 100)   -> 78.0
#   Power(price, 2)   -> 841.00000000000000
#   Sqrt(pages)       -> 34.3220
#   Exp(1)            -> 2.7183
#   Ln(pages)         -> 7.0716
#   Log(10, pages)    -> 3.0711
#   Pi()              -> 3.14159
#   Sin(1)            -> 0.8415
#   Cos(1)            -> 0.5403
#   Tan(1)            -> 1.5574
#   Cot(1)            -> 0.6421
#   ASin(0.5)         -> 0.5236
#   ACos(0.5)         -> 1.0472
#   ATan(1)           -> 0.7854  (= π/4)
#   ATan2(1, 1)       -> 0.7854  (= π/4)
#   Degrees(π)        -> 180.00
#   Radians(180)      -> 3.1416  (= π)
#   Random()          -> 0.3992  (0..1)