LAMMP 4.2.0
Lamina High-Precision Arithmetic Library
载入中...
搜索中...
未找到
nthroot_1.c 文件参考
#include <math.h>
#include "../../../include/lammp/numth.h"
+ nthroot_1.c 的引用(Include)关系图:

浏览源代码.

函数

ulong lmmp_nthroot_ulong_ (ulong n, ulong root)
 计算 floor(n^(1/root))
 
static ulong pow_n (ulong x, ulong n)
 Copyright (C) 2026 HJimmyK(Jericho Knox)
 

变量

static const float inv_table []
 
static const uint16_t max_base []
 

函数说明

◆ lmmp_nthroot_ulong_()

ulong lmmp_nthroot_ulong_ ( ulong  n,
ulong  root 
)

计算 floor(n^(1/root))

参数
n被开方数
root开方次数
返回
floor(n^(1/root))
注解
root=0时,返回0

在文件 nthroot_1.c44 行定义.

44 {
45 ulong x, currval, base, upper_limit;
46
47 if (n == 0 || root == 0)
48 return 0;
49 if (root == 1)
50 return n;
51 if (root == 2)
52 return lmmp_sqrt_ulong_(n);
53 if (root == 3)
54 return lmmp_cbrt_ulong_(n);
55
56 if (root >= LIMB_BITS || n < (1ULL << root))
57 return 1;
58
59 /* n <= upper_limit^root */
60 upper_limit = max_base[root - 4];
61
62 if (upper_limit == 2)
63 return upper_limit;
64
65 /* upper_limit = 2 for root >= 41 */
66 lmmp_debug_assert(root <= 40);
67
68 if (root == 4)
69 x = sqrt(sqrt(n));
70 else
71 x = expf(inv_table[root - 5] * logf(n));
72
73 base = x;
74
75 if (base >= upper_limit)
76 base = upper_limit - 1;
77
78 currval = pow_n(base, root);
79 if (currval == n)
80 return base;
81
82 while (currval <= n) {
83 base++;
84 currval = pow_n(base, root);
85 if (base == upper_limit)
86 break;
87 }
88
89 while (currval > n) {
90 base--;
91 currval = pow_n(base, root);
92 }
93
94 return base;
95}
#define lmmp_debug_assert(x)
Definition lmmp.h:485
#define LIMB_BITS
Definition lmmp.h:123
#define n
static const float inv_table[]
Definition nthroot_1.c:29
static ulong pow_n(ulong x, ulong n)
Copyright (C) 2026 HJimmyK(Jericho Knox)
Definition nthroot_1.c:21
static const uint16_t max_base[]
Definition nthroot_1.c:40
ulong lmmp_cbrt_ulong_(ulong n)
计算算数立方根 floor(cbrt(n))
Definition cbrt_1.c:132
ulong lmmp_sqrt_ulong_(ulong a)
计算算术平方根 floor(sqrt(a))
Definition sqrt_1.c:22
uint64_t ulong
Definition numth.h:32

引用了 inv_table, LIMB_BITS, lmmp_cbrt_ulong_(), lmmp_debug_assert, lmmp_sqrt_ulong_(), max_base, n , 以及 pow_n().

+ 函数调用图:

◆ pow_n()

static ulong pow_n ( ulong  x,
ulong  n 
)
inlinestatic

Copyright (C) 2026 HJimmyK(Jericho Knox)

This file is part of LAMMP.

LAMMP is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License (LGPL) as published by the Free Software Foundation; either version 3 of the License, or (at your option) any later version.

This program is distributed WITHOUT ANY WARRANTY.

See https://www.gnu.org/licenses/.

在文件 nthroot_1.c21 行定义.

21 {
22 ulong ret = 1;
23 for (ulong i = 0; i < n; ++i) {
24 ret *= x;
25 }
26 return ret;
27}

引用了 n.

被这些函数引用 lmmp_nthroot_ulong_().

+ 这是这个函数的调用关系图:

变量说明

◆ inv_table

const float inv_table[]
static
初始值:
= {
0.200000000000000, 0.166666666666667, 0.142857142857143, 0.125000000000000, 0.111111111111111, 0.100000000000000,
0.090909090909091, 0.083333333333333, 0.076923076923077, 0.071428571428571, 0.066666666666667, 0.062500000000000,
0.058823529411765, 0.055555555555556, 0.052631578947368, 0.050000000000000, 0.047619047619048, 0.045454545454545,
0.043478260869565, 0.041666666666667, 0.040000000000000, 0.038461538461538, 0.037037037037037, 0.035714285714286,
0.034482758620690, 0.033333333333333, 0.032258064516129, 0.031250000000000, 0.030303030303030, 0.029411764705882,
0.028571428571429, 0.027777777777778, 0.027027027027027, 0.026315789473684, 0.025641025641026, 0.025000000000000,
}

在文件 nthroot_1.c29 行定义.

29 {
30 0.200000000000000, 0.166666666666667, 0.142857142857143, 0.125000000000000, 0.111111111111111, 0.100000000000000,
31 0.090909090909091, 0.083333333333333, 0.076923076923077, 0.071428571428571, 0.066666666666667, 0.062500000000000,
32 0.058823529411765, 0.055555555555556, 0.052631578947368, 0.050000000000000, 0.047619047619048, 0.045454545454545,
33 0.043478260869565, 0.041666666666667, 0.040000000000000, 0.038461538461538, 0.037037037037037, 0.035714285714286,
34 0.034482758620690, 0.033333333333333, 0.032258064516129, 0.031250000000000, 0.030303030303030, 0.029411764705882,
35 0.028571428571429, 0.027777777777778, 0.027027027027027, 0.026315789473684, 0.025641025641026, 0.025000000000000,
36};

被这些函数引用 lmmp_nthroot_ulong_().

◆ max_base

const uint16_t max_base[]
static
初始值:
= {
65535, 7131, 1625, 565, 255, 138, 84, 56, 40, 30, 23, 19, 15, 13, 11, 10, 9, 8, 7, 6, 6, 5, 5, 5, 4, 4, 4, 4, 3, 3,
3, 3, 3, 3, 3, 3, 3, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2}

在文件 nthroot_1.c40 行定义.

40 {
41 65535, 7131, 1625, 565, 255, 138, 84, 56, 40, 30, 23, 19, 15, 13, 11, 10, 9, 8, 7, 6, 6, 5, 5, 5, 4, 4, 4, 4, 3, 3,
42 3, 3, 3, 3, 3, 3, 3, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2};

被这些函数引用 lmmp_nthroot_ulong_().