• <tfoot id='LFjg7'></tfoot>
    • <bdo id='LFjg7'></bdo><ul id='LFjg7'></ul>

    <small id='LFjg7'></small><noframes id='LFjg7'>

  • <i id='LFjg7'><tr id='LFjg7'><dt id='LFjg7'><q id='LFjg7'><span id='LFjg7'><b id='LFjg7'><form id='LFjg7'><ins id='LFjg7'></ins><ul id='LFjg7'></ul><sub id='LFjg7'></sub></form><legend id='LFjg7'></legend><bdo id='LFjg7'><pre id='LFjg7'><center id='LFjg7'></center></pre></bdo></b><th id='LFjg7'></th></span></q></dt></tr></i><div id='LFjg7'><tfoot id='LFjg7'></tfoot><dl id='LFjg7'><fieldset id='LFjg7'></fieldset></dl></div>

      <legend id='LFjg7'><style id='LFjg7'><dir id='LFjg7'><q id='LFjg7'></q></dir></style></legend>

      1. 如何自己编写幂函数?

        时间:2023-08-03

        <small id='ApacW'></small><noframes id='ApacW'>

                <bdo id='ApacW'></bdo><ul id='ApacW'></ul>
                    <tbody id='ApacW'></tbody>
                  <i id='ApacW'><tr id='ApacW'><dt id='ApacW'><q id='ApacW'><span id='ApacW'><b id='ApacW'><form id='ApacW'><ins id='ApacW'></ins><ul id='ApacW'></ul><sub id='ApacW'></sub></form><legend id='ApacW'></legend><bdo id='ApacW'><pre id='ApacW'><center id='ApacW'></center></pre></bdo></b><th id='ApacW'></th></span></q></dt></tr></i><div id='ApacW'><tfoot id='ApacW'></tfoot><dl id='ApacW'><fieldset id='ApacW'></fieldset></dl></div>

                1. <tfoot id='ApacW'></tfoot>
                2. <legend id='ApacW'><style id='ApacW'><dir id='ApacW'><q id='ApacW'></q></dir></style></legend>
                  本文介绍了如何自己编写幂函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  我一直想知道如何自己制作一个计算幂的函数(例如 23).在大多数语言中,这些都包含在标准库中,主要是作为 pow(double x, double y),但我如何自己编写?

                  I was always wondering how I can make a function which calculates the power (e.g. 23) myself. In most languages these are included in the standard library, mostly as pow(double x, double y), but how can I write it myself?

                  我在考虑 for 循环,但它认为我的大脑陷入了循环(当我想用非整数指数计算幂时,例如 54.5 或底片 2-21) 然后我疯了 ;)

                  I was thinking about for loops, but it think my brain got in a loop (when I wanted to do a power with a non-integer exponent, like 54.5 or negatives 2-21) and I went crazy ;)

                  那么,如何编写一个计算实数幂的函数?谢谢

                  So, how can I write a function which calculates the power of a real number? Thanks

                  哦,也许需要注意的是:我不能使用使用幂的函数(例如 exp),这会使它最终变得毫无用处.

                  Oh, maybe important to note: I cannot use functions which use powers (e.g. exp), which would make this ultimately useless.

                  推荐答案

                  负幂不是问题,它们只是正幂的倒数 (1/x).

                  Negative powers are not a problem, they're just the inverse (1/x) of the positive power.

                  浮点运算稍微复杂一点;如您所知,分数幂等效于根(例如 x^(1/2) == sqrt(x)),并且您还知道以相同的基数乘幂等效于将它们的相加指数.

                  Floating point powers are just a little bit more complicated; as you know a fractional power is equivalent to a root (e.g. x^(1/2) == sqrt(x)) and you also know that multiplying powers with the same base is equivalent to add their exponents.

                  有了以上所有内容,您可以:

                  With all the above, you can:

                  • 将指数分解为整数部分和有理部分.
                  • 使用循环计算整数幂(您可以优化它分解因子并重用部分计算).
                  • 使用您喜欢的任何算法计算根(任何迭代近似,如二分法或牛顿法都可以).
                  • 将结果相乘.
                  • 如果指数为负,则应用倒数.

                  示例:

                  2^(-3.5) = (2^3 * 2^(1/2)))^-1 = 1 / (2*2*2 * sqrt(2))
                  

                  这篇关于如何自己编写幂函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:不能让价值通过carry传播 下一篇:在 C++ 中表示 128 位数字

                  相关文章

                  最新文章

                  <small id='73ofw'></small><noframes id='73ofw'>

                  <i id='73ofw'><tr id='73ofw'><dt id='73ofw'><q id='73ofw'><span id='73ofw'><b id='73ofw'><form id='73ofw'><ins id='73ofw'></ins><ul id='73ofw'></ul><sub id='73ofw'></sub></form><legend id='73ofw'></legend><bdo id='73ofw'><pre id='73ofw'><center id='73ofw'></center></pre></bdo></b><th id='73ofw'></th></span></q></dt></tr></i><div id='73ofw'><tfoot id='73ofw'></tfoot><dl id='73ofw'><fieldset id='73ofw'></fieldset></dl></div>

                  1. <legend id='73ofw'><style id='73ofw'><dir id='73ofw'><q id='73ofw'></q></dir></style></legend>
                    1. <tfoot id='73ofw'></tfoot>
                      • <bdo id='73ofw'></bdo><ul id='73ofw'></ul>