# lv_math.h (/api/misc/lv_math_h)



<ApiSummary functions="12" structs="1" macros="20" />

Functions [#functions]

<ApiTabs items="[&#x22;Setters (1)&#x22;,&#x22;Other (11)&#x22;]">
  <ApiTab value="Setters (1)">
    <ApiMember kind="function" name="lv_rand_set_seed" file="misc/lv_math.h" line="139" url="https://github.com/lvgl/lvgl/tree/a7b95c5b0839ce901c09c205610bc2c77cc3345d/src/misc/lv_math.h#L139">
      lv_rand_set_seed [#lv_rand_set_seed]

      Set the seed of the pseudo random number generator

      ```c title=" " lineNumbers=1
      void lv_rand_set_seed(uint32_t seed)
      ```

      <span className="sr-only">
        Parameters
      </span>

      | Name   | Type                        | Description                                 |
      | ------ | --------------------------- | ------------------------------------------- |
      | `seed` | <ApiLink name="uint32_t" /> | a number to initialize the random generator |
    </ApiMember>
  </ApiTab>

  <ApiTab value="Other (11)">
    <ApiMember kind="function" name="lv_trigo_sin" file="misc/lv_math.h" line="53" url="https://github.com/lvgl/lvgl/tree/a7b95c5b0839ce901c09c205610bc2c77cc3345d/src/misc/lv_math.h#L53">
      lv_trigo_sin [#lv_trigo_sin]

      Return with sinus of an angle

      ```c title=" " lineNumbers=1
      int32_t lv_trigo_sin(int16_t angle)
      ```

      <span className="sr-only">
        Parameters
      </span>

      | Name    | Type                       |
      | ------- | -------------------------- |
      | `angle` | <ApiLink name="int16_t" /> |

      **Returns:** <ApiLink name="int32_t" /> — sinus of 'angle'. sin(-90) = -32767, sin(90) = 32767
    </ApiMember>

    <ApiMember kind="function" name="lv_trigo_cos" file="misc/lv_math.h" line="55" url="https://github.com/lvgl/lvgl/tree/a7b95c5b0839ce901c09c205610bc2c77cc3345d/src/misc/lv_math.h#L55">
      lv_trigo_cos [#lv_trigo_cos]

      ```c title=" " lineNumbers=1
      int32_t lv_trigo_cos(int16_t angle)
      ```

      <span className="sr-only">
        Parameters
      </span>

      | Name    | Type                       |
      | ------- | -------------------------- |
      | `angle` | <ApiLink name="int16_t" /> |
    </ApiMember>

    <ApiMember kind="function" name="lv_cubic_bezier" file="misc/lv_math.h" line="66" url="https://github.com/lvgl/lvgl/tree/a7b95c5b0839ce901c09c205610bc2c77cc3345d/src/misc/lv_math.h#L66">
      lv_cubic_bezier [#lv_cubic_bezier]

      Calculate the y value of cubic-bezier(x1, y1, x2, y2) function as specified x.

      ```c title=" " lineNumbers=1
      int32_t lv_cubic_bezier(int32_t x, int32_t x1, int32_t y1, int32_t x2, int32_t y2)
      ```

      <span className="sr-only">
        Parameters
      </span>

      | Name | Type                       | Description                                                 |
      | ---- | -------------------------- | ----------------------------------------------------------- |
      | `x`  | <ApiLink name="int32_t" /> | time in range of \[0..LV\_BEZIER\_VAL\_MAX]                 |
      | `x1` | <ApiLink name="int32_t" /> | x of control point 1 in range of \[0..LV\_BEZIER\_VAL\_MAX] |
      | `y1` | <ApiLink name="int32_t" /> | y of control point 1 in range of \[0..LV\_BEZIER\_VAL\_MAX] |
      | `x2` | <ApiLink name="int32_t" /> | x of control point 2 in range of \[0..LV\_BEZIER\_VAL\_MAX] |
      | `y2` | <ApiLink name="int32_t" /> | y of control point 2 in range of \[0..LV\_BEZIER\_VAL\_MAX] |

      **Returns:** <ApiLink name="int32_t" /> — the value calculated
    </ApiMember>

    <ApiMember kind="function" name="lv_bezier3" file="misc/lv_math.h" line="77" url="https://github.com/lvgl/lvgl/tree/a7b95c5b0839ce901c09c205610bc2c77cc3345d/src/misc/lv_math.h#L77">
      lv_bezier3 [#lv_bezier3]

      Calculate a value of a Cubic Bezier function.

      ```c title=" " lineNumbers=1
      int32_t lv_bezier3(int32_t t, int32_t u0, uint32_t u1, int32_t u2, int32_t u3)
      ```

      <span className="sr-only">
        Parameters
      </span>

      | Name | Type                        | Description                                                   |
      | ---- | --------------------------- | ------------------------------------------------------------- |
      | `t`  | <ApiLink name="int32_t" />  | time in range of \[0..LV\_BEZIER\_VAL\_MAX]                   |
      | `u0` | <ApiLink name="int32_t" />  | must be 0                                                     |
      | `u1` | <ApiLink name="uint32_t" /> | control value 1 values in range of \[0..LV\_BEZIER\_VAL\_MAX] |
      | `u2` | <ApiLink name="int32_t" />  | control value 2 in range of \[0..LV\_BEZIER\_VAL\_MAX]        |
      | `u3` | <ApiLink name="int32_t" />  | must be LV\_BEZIER\_VAL\_MAX                                  |

      **Returns:** <ApiLink name="int32_t" /> — the value calculated from the given parameters in range of \[0..LV\_BEZIER\_VAL\_MAX]
    </ApiMember>

    <ApiMember kind="function" name="lv_atan2" file="misc/lv_math.h" line="86" url="https://github.com/lvgl/lvgl/tree/a7b95c5b0839ce901c09c205610bc2c77cc3345d/src/misc/lv_math.h#L86">
      lv_atan2 [#lv_atan2]

      Calculate the atan2 of a vector.

      ```c title=" " lineNumbers=1
      uint16_t lv_atan2(int x, int y)
      ```

      <span className="sr-only">
        Parameters
      </span>

      | Name | Type  |
      | ---- | ----- |
      | `x`  | `int` |
      | `y`  | `int` |

      **Returns:** <ApiLink name="uint16_t" /> — the angle in degree calculated from the given parameters in range of \[0..360]
    </ApiMember>

    <ApiMember kind="function" name="lv_sqrt" file="misc/lv_math.h" line="98" url="https://github.com/lvgl/lvgl/tree/a7b95c5b0839ce901c09c205610bc2c77cc3345d/src/misc/lv_math.h#L98">
      lv_sqrt [#lv_sqrt]

      Get the square root of a number

      ```c title=" " lineNumbers=1
      void lv_sqrt(uint32_t x, lv_sqrt_res_t *q, uint32_t mask)
      ```

      <span className="sr-only">
        Parameters
      </span>

      | Name   | Type                                                       | Description                                                                                                                                                                   |
      | ------ | ---------------------------------------------------------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
      | `x`    | <ApiLink name="uint32_t" />                                | integer which square root should be calculated                                                                                                                                |
      | `q`    | <ApiLink name="lv_sqrt_res_t" display="lv_sqrt_res_t *" /> | store the result here. q->i: integer part, q->f: fractional part in 1/256 unit                                                                                                |
      | `mask` | <ApiLink name="uint32_t" />                                | optional to skip some iterations if the magnitude of the root is known. Set to 0x8000 by default. If root \< 16: mask = 0x80 If root \< 256: mask = 0x800 Else: mask = 0x8000 |
    </ApiMember>

    <ApiMember kind="function" name="lv_sqrt32" file="misc/lv_math.h" line="104" url="https://github.com/lvgl/lvgl/tree/a7b95c5b0839ce901c09c205610bc2c77cc3345d/src/misc/lv_math.h#L104">
      lv_sqrt32 [#lv_sqrt32]

      Alternative (fast, approximate) implementation for getting the square root of an integer.

      ```c title=" " lineNumbers=1
      int32_t lv_sqrt32(uint32_t x)
      ```

      <span className="sr-only">
        Parameters
      </span>

      | Name | Type                        | Description                                    |
      | ---- | --------------------------- | ---------------------------------------------- |
      | `x`  | <ApiLink name="uint32_t" /> | integer which square root should be calculated |
    </ApiMember>

    <ApiMember kind="function" name="lv_sqr" file="misc/lv_math.h" line="111" url="https://github.com/lvgl/lvgl/tree/a7b95c5b0839ce901c09c205610bc2c77cc3345d/src/misc/lv_math.h#L111">
      lv_sqr [#lv_sqr]

      Calculate the square of an integer (input range is 0..32767).

      ```c title=" " lineNumbers=1
      static int32_t lv_sqr(int32_t x)
      ```

      <span className="sr-only">
        Parameters
      </span>

      | Name | Type                       | Description |
      | ---- | -------------------------- | ----------- |
      | `x`  | <ApiLink name="int32_t" /> | input       |

      **Returns:** <ApiLink name="int32_t" /> — square
    </ApiMember>

    <ApiMember kind="function" name="lv_pow" file="misc/lv_math.h" line="122" url="https://github.com/lvgl/lvgl/tree/a7b95c5b0839ce901c09c205610bc2c77cc3345d/src/misc/lv_math.h#L122">
      lv_pow [#lv_pow]

      Calculate the integer exponents.

      ```c title=" " lineNumbers=1
      int64_t lv_pow(int64_t base, int8_t exp)
      ```

      <span className="sr-only">
        Parameters
      </span>

      | Name   | Type                       |
      | ------ | -------------------------- |
      | `base` | <ApiLink name="int64_t" /> |
      | `exp`  | <ApiLink name="int8_t" />  |

      **Returns:** <ApiLink name="int64_t" /> — base raised to the power exponent
    </ApiMember>

    <ApiMember kind="function" name="lv_map" file="misc/lv_math.h" line="133" url="https://github.com/lvgl/lvgl/tree/a7b95c5b0839ce901c09c205610bc2c77cc3345d/src/misc/lv_math.h#L133">
      lv_map [#lv_map]

      Get the mapped of a number given an input and output range

      ```c title=" " lineNumbers=1
      int32_t lv_map(int32_t x, int32_t min_in, int32_t max_in, int32_t min_out, int32_t max_out)
      ```

      <span className="sr-only">
        Parameters
      </span>

      | Name      | Type                       | Description                                     |
      | --------- | -------------------------- | ----------------------------------------------- |
      | `x`       | <ApiLink name="int32_t" /> | integer which mapped value should be calculated |
      | `min_in`  | <ApiLink name="int32_t" /> | min input range                                 |
      | `max_in`  | <ApiLink name="int32_t" /> | max input range                                 |
      | `min_out` | <ApiLink name="int32_t" /> | max output range                                |
      | `max_out` | <ApiLink name="int32_t" /> | max output range                                |

      **Returns:** <ApiLink name="int32_t" /> — the mapped number
    </ApiMember>

    <ApiMember kind="function" name="lv_rand" file="misc/lv_math.h" line="147" url="https://github.com/lvgl/lvgl/tree/a7b95c5b0839ce901c09c205610bc2c77cc3345d/src/misc/lv_math.h#L147">
      lv_rand [#lv_rand]

      Get a pseudo random number in the given range

      ```c title=" " lineNumbers=1
      uint32_t lv_rand(uint32_t min, uint32_t max)
      ```

      <span className="sr-only">
        Parameters
      </span>

      | Name  | Type                        | Description       |
      | ----- | --------------------------- | ----------------- |
      | `min` | <ApiLink name="uint32_t" /> | the minimum value |
      | `max` | <ApiLink name="uint32_t" /> | the maximum value |

      **Returns:** <ApiLink name="uint32_t" /> — return the random number. min \<= return\_value \<= max
    </ApiMember>
  </ApiTab>
</ApiTabs>

Structs [#structs]

<ApiMember kind="struct" name="lv_sqrt_res_t">
  lv_sqrt_res_t [#lv_sqrt_res_t]

  | Member | Type                        | Description |
  | ------ | --------------------------- | ----------- |
  | `i`    | <ApiLink name="uint16_t" /> |             |
  | `f`    | <ApiLink name="uint16_t" /> |             |
</ApiMember>

<TypeUsedBy name="lv_sqrt_res_t" count="1">
  * `lv_sqrt` — param `q`
</TypeUsedBy>

Macros [#macros]

<ApiMember kind="macro" name="LV_TRIGO_SIN_MAX" file="misc/lv_math.h" line="22" url="https://github.com/lvgl/lvgl/tree/a7b95c5b0839ce901c09c205610bc2c77cc3345d/src/misc/lv_math.h#L22">
  LV_TRIGO_SIN_MAX [#lv_trigo_sin_max]

  ```c title=" " lineNumbers=1
  #define LV_TRIGO_SIN_MAX 32768
  ```
</ApiMember>

<ApiMember kind="macro" name="LV_TRIGO_SHIFT" file="misc/lv_math.h" line="23" url="https://github.com/lvgl/lvgl/tree/a7b95c5b0839ce901c09c205610bc2c77cc3345d/src/misc/lv_math.h#L23">
  LV_TRIGO_SHIFT [#lv_trigo_shift]

  ```c title=" " lineNumbers=1
  #define LV_TRIGO_SHIFT 15
  ```

  \> > ‍LV\_TRIGO\_SHIFT to normalize
</ApiMember>

<ApiMember kind="macro" name="LV_BEZIER_VAL_SHIFT" file="misc/lv_math.h" line="25" url="https://github.com/lvgl/lvgl/tree/a7b95c5b0839ce901c09c205610bc2c77cc3345d/src/misc/lv_math.h#L25">
  LV_BEZIER_VAL_SHIFT [#lv_bezier_val_shift]

  ```c title=" " lineNumbers=1
  #define LV_BEZIER_VAL_SHIFT 10
  ```

  log2(LV\_BEZIER\_VAL\_MAX): used to normalize up scaled values
</ApiMember>

<ApiMember kind="macro" name="LV_BEZIER_VAL_MAX" file="misc/lv_math.h" line="26" url="https://github.com/lvgl/lvgl/tree/a7b95c5b0839ce901c09c205610bc2c77cc3345d/src/misc/lv_math.h#L26">
  LV_BEZIER_VAL_MAX [#lv_bezier_val_max]

  ```c title=" " lineNumbers=1
  #define LV_BEZIER_VAL_MAX (1L << LV_BEZIER_VAL_SHIFT)
  ```

  Max time in Bezier functions (not \[0..1] to use integers)
</ApiMember>

<ApiMember kind="macro" name="LV_BEZIER_VAL_FLOAT" file="misc/lv_math.h" line="27" url="https://github.com/lvgl/lvgl/tree/a7b95c5b0839ce901c09c205610bc2c77cc3345d/src/misc/lv_math.h#L27">
  LV_BEZIER_VAL_FLOAT [#lv_bezier_val_float]

  ```c title=" " lineNumbers=1
  #define LV_BEZIER_VAL_FLOAT(f) \
      ((int32_t)((f) * LV_BEZIER_VAL_MAX))
  ```

  Convert const float number cubic-bezier values to fix-point value
</ApiMember>

<ApiMember kind="macro" name="LV_ALIGN_UP" file="misc/lv_math.h" line="30" url="https://github.com/lvgl/lvgl/tree/a7b95c5b0839ce901c09c205610bc2c77cc3345d/src/misc/lv_math.h#L30">
  LV_ALIGN_UP [#lv_align_up]

  ```c title=" " lineNumbers=1
  #define LV_ALIGN_UP(x, align) \
      (((x) + ((align) - 1)) & ~((align) - 1))
  ```

  Align up value x to align, align must be a power of two
</ApiMember>

<ApiMember kind="macro" name="LV_ROUND_UP" file="misc/lv_math.h" line="33" url="https://github.com/lvgl/lvgl/tree/a7b95c5b0839ce901c09c205610bc2c77cc3345d/src/misc/lv_math.h#L33">
  LV_ROUND_UP [#lv_round_up]

  ```c title=" " lineNumbers=1
  #define LV_ROUND_UP(x, round) \
      ((((x) + ((round) - 1)) / (round)) * (round))
  ```

  Round up value x to round, round can be any integer number
</ApiMember>

<ApiMember kind="macro" name="LV_MIN" file="misc/lv_math.h" line="152" url="https://github.com/lvgl/lvgl/tree/a7b95c5b0839ce901c09c205610bc2c77cc3345d/src/misc/lv_math.h#L152">
  LV_MIN [#lv_min]

  ```c title=" " lineNumbers=1
  #define LV_MIN(a, b) \
      ((a) < (b) ? (a) : (b))
  ```
</ApiMember>

<ApiMember kind="macro" name="LV_MIN3" file="misc/lv_math.h" line="153" url="https://github.com/lvgl/lvgl/tree/a7b95c5b0839ce901c09c205610bc2c77cc3345d/src/misc/lv_math.h#L153">
  LV_MIN3 [#lv_min3]

  ```c title=" " lineNumbers=1
  #define LV_MIN3(a, b, c) \
      (LV_MIN(LV_MIN(a,b), c))
  ```
</ApiMember>

<ApiMember kind="macro" name="LV_MIN4" file="misc/lv_math.h" line="154" url="https://github.com/lvgl/lvgl/tree/a7b95c5b0839ce901c09c205610bc2c77cc3345d/src/misc/lv_math.h#L154">
  LV_MIN4 [#lv_min4]

  ```c title=" " lineNumbers=1
  #define LV_MIN4(a, b, c, d) \
      (LV_MIN(LV_MIN(a,b), LV_MIN(c,d)))
  ```
</ApiMember>

<ApiMember kind="macro" name="LV_MAX" file="misc/lv_math.h" line="156" url="https://github.com/lvgl/lvgl/tree/a7b95c5b0839ce901c09c205610bc2c77cc3345d/src/misc/lv_math.h#L156">
  LV_MAX [#lv_max]

  ```c title=" " lineNumbers=1
  #define LV_MAX(a, b) \
      ((a) > (b) ? (a) : (b))
  ```
</ApiMember>

<ApiMember kind="macro" name="LV_MAX3" file="misc/lv_math.h" line="157" url="https://github.com/lvgl/lvgl/tree/a7b95c5b0839ce901c09c205610bc2c77cc3345d/src/misc/lv_math.h#L157">
  LV_MAX3 [#lv_max3]

  ```c title=" " lineNumbers=1
  #define LV_MAX3(a, b, c) \
      (LV_MAX(LV_MAX(a,b), c))
  ```
</ApiMember>

<ApiMember kind="macro" name="LV_MAX4" file="misc/lv_math.h" line="158" url="https://github.com/lvgl/lvgl/tree/a7b95c5b0839ce901c09c205610bc2c77cc3345d/src/misc/lv_math.h#L158">
  LV_MAX4 [#lv_max4]

  ```c title=" " lineNumbers=1
  #define LV_MAX4(a, b, c, d) \
      (LV_MAX(LV_MAX(a,b), LV_MAX(c,d)))
  ```
</ApiMember>

<ApiMember kind="macro" name="LV_CLAMP" file="misc/lv_math.h" line="160" url="https://github.com/lvgl/lvgl/tree/a7b95c5b0839ce901c09c205610bc2c77cc3345d/src/misc/lv_math.h#L160">
  LV_CLAMP [#lv_clamp]

  ```c title=" " lineNumbers=1
  #define LV_CLAMP(min, val, max) \
      (LV_MAX(min, (LV_MIN(val, max))))
  ```
</ApiMember>

<ApiMember kind="macro" name="LV_ABS" file="misc/lv_math.h" line="162" url="https://github.com/lvgl/lvgl/tree/a7b95c5b0839ce901c09c205610bc2c77cc3345d/src/misc/lv_math.h#L162">
  LV_ABS [#lv_abs]

  ```c title=" " lineNumbers=1
  #define LV_ABS(x) \
      ((x) > 0 ? (x) : (-(x)))
  ```
</ApiMember>

<ApiMember kind="macro" name="LV_UDIV255" file="misc/lv_math.h" line="163" url="https://github.com/lvgl/lvgl/tree/a7b95c5b0839ce901c09c205610bc2c77cc3345d/src/misc/lv_math.h#L163">
  LV_UDIV255 [#lv_udiv255]

  ```c title=" " lineNumbers=1
  #define LV_UDIV255(x) \
      (((x) * 0x8081U) >> 0x17)
  ```
</ApiMember>

<ApiMember kind="macro" name="LV_IS_SIGNED" file="misc/lv_math.h" line="165" url="https://github.com/lvgl/lvgl/tree/a7b95c5b0839ce901c09c205610bc2c77cc3345d/src/misc/lv_math.h#L165">
  LV_IS_SIGNED [#lv_is_signed]

  ```c title=" " lineNumbers=1
  #define LV_IS_SIGNED(t) \
      (((t)(-1)) < ((t)0))
  ```
</ApiMember>

<ApiMember kind="macro" name="LV_UMAX_OF" file="misc/lv_math.h" line="166" url="https://github.com/lvgl/lvgl/tree/a7b95c5b0839ce901c09c205610bc2c77cc3345d/src/misc/lv_math.h#L166">
  LV_UMAX_OF [#lv_umax_of]

  ```c title=" " lineNumbers=1
  #define LV_UMAX_OF(t) \
      (((0x1ULL << ((sizeof(t) * 8ULL) - 1ULL)) - 1ULL) | (0xFULL << ((sizeof(t) * 8ULL) - 4ULL)))
  ```
</ApiMember>

<ApiMember kind="macro" name="LV_SMAX_OF" file="misc/lv_math.h" line="167" url="https://github.com/lvgl/lvgl/tree/a7b95c5b0839ce901c09c205610bc2c77cc3345d/src/misc/lv_math.h#L167">
  LV_SMAX_OF [#lv_smax_of]

  ```c title=" " lineNumbers=1
  #define LV_SMAX_OF(t) \
      (((0x1ULL << ((sizeof(t) * 8ULL) - 1ULL)) - 1ULL) | (0x7ULL << ((sizeof(t) * 8ULL) - 4ULL)))
  ```
</ApiMember>

<ApiMember kind="macro" name="LV_MAX_OF" file="misc/lv_math.h" line="168" url="https://github.com/lvgl/lvgl/tree/a7b95c5b0839ce901c09c205610bc2c77cc3345d/src/misc/lv_math.h#L168">
  LV_MAX_OF [#lv_max_of]

  ```c title=" " lineNumbers=1
  #define LV_MAX_OF(t) \
      ((unsigned long)(LV_IS_SIGNED(t) ? LV_SMAX_OF(t) : LV_UMAX_OF(t)))
  ```
</ApiMember>

Dependencies [#dependencies]

<FileIncludes includes="[&#x22;lv_conf_internal.h&#x22;, &#x22;lv_types.h&#x22;]" includedBy="[&#x22;lv_draw_sw_mask.h&#x22;, &#x22;lv_anim.h&#x22;, &#x22;lv_area.h&#x22;, &#x22;lv_color.h&#x22;, &#x22;lv_color_op.h&#x22;]" transitiveIncludes="[&#x22;lv_conf_kconfig.h&#x22;]" />
