summaryrefslogtreecommitdiff
path: root/lib/bt/esp_ble_mesh/models/common/model_common.c
blob: eb54ee1501d7de74683931721f17c0442efd7827 (plain)
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
/*
 * SPDX-FileCopyrightText: 2018 Vikrant More
 * SPDX-FileContributor: 2018-2023 Espressif Systems (Shanghai) CO LTD
 *
 * SPDX-License-Identifier: Apache-2.0
 */

#include <math.h>
#include "mesh/types.h"

#define MINDIFF (2.25e-308)

float bt_mesh_sqrt(float square)
{
    float root = 0.0, last = 0.0, diff = 0.0;

    root = square / 3.0;
    diff = 1;

    if (square <= 0) {
        return 0;
    }

    do {
        last = root;
        root = (root + square / root) / 2.0;
        diff = root - last;
    } while (diff > MINDIFF || diff < -MINDIFF);

    return root;
}

int32_t bt_mesh_ceil(float num)
{
    int32_t inum = (int32_t)num;

    if (num == (float)inum) {
        return inum;
    }

    return inum + 1;
}

float bt_mesh_log2(float num)
{
    return log2f(num);
}