1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

math: sqrti, powi, factoriali (#12072)

This commit is contained in:
05st
2021-10-08 14:07:44 -05:00
committed by GitHub
parent cd5b304cbf
commit 43931be451
5 changed files with 88 additions and 0 deletions

View File

@@ -35,3 +35,22 @@ pub fn sqrt(a f64) f64 {
pub fn sqrtf(a f32) f32 {
return f32(sqrt(a))
}
// sqrti calculates the integer square-root of the provided value. (i64)
pub fn sqrti(a i64) i64 {
mut x := a
mut q, mut r := i64(1), i64(0)
for ; q <= x; {
q <<= 2
}
for ; q > 1; {
q >>= 2
t := x - r - q
r >>= 1
if t >= 0 {
x = t
r += q
}
}
return r
}