zephyr/lib/libc/minimal/source/math/sqrt.c
Keith Packard 05946ed9b2 lib/libc/minimal: Move sqrt/sqrtf from samples
The lmp90100_evb sample included an implementation of double sqrt, and the
on_off_level_lighting_vnd_app sample included an implementation of float
sqrtf. Move that code into minimal libc instead of requiring applications
to hand-roll their own version.

Signed-off-by: Keith Packard <keithp@keithp.com>
2022-05-14 08:49:36 +09:00

24 lines
296 B
C

/*
* Copyright (c) 2019 Vestas Wind Systems A/S
*
* SPDX-License-Identifier: Apache-2.0
*/
#include <math.h>
double sqrt(double value)
{
double sqrt = value / 3;
int i;
if (value <= 0) {
return 0;
}
for (i = 0; i < 6; i++) {
sqrt = (sqrt + value / sqrt) / 2;
}
return sqrt;
}