{"id":296537,"date":"2024-05-20T16:04:14","date_gmt":"2024-05-20T08:04:14","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/296537.html"},"modified":"2024-05-20T16:04:28","modified_gmt":"2024-05-20T08:04:28","slug":"c-%e8%af%ad%e8%a8%80%e7%9a%84-sqrt-%e5%87%bd%e6%95%b0%e5%a6%82%e4%bd%95%e4%bd%bf%e7%94%a8","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/296537.html","title":{"rendered":"C \u8bed\u8a00\u7684 sqrt \u51fd\u6570\u5982\u4f55\u4f7f\u7528"},"content":{"rendered":"<p style=\"text-align:center\"><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/27143814\/51c28d21-64dd-4a28-9e27-386af4902bce.webp\" alt=\"C \u8bed\u8a00\u7684 sqrt \u51fd\u6570\u5982\u4f55\u4f7f\u7528\" \/><\/p>\n<p><p>C\u8bed\u8a00\u4e2d\u7684<code>sqrt<\/code>\u51fd\u6570\u7528\u4e8e\u8ba1\u7b97\u4e00\u4e2a\u6570\u7684\u5e73\u65b9\u6839\u3002<strong>\u8981\u4f7f\u7528\u8be5\u51fd\u6570\uff0c\u7528\u6237\u9700\u8981\u5305\u542b\u5934\u6587\u4ef6<code>&lt;math.h&gt;<\/code>\uff0c\u5e76\u8c03\u7528<code>sqrt<\/code>\u51fd\u6570\uff0c\u4f20\u5165\u4e00\u4e2a\u975e\u8d1f\u6570\u4f5c\u4e3a\u53c2\u6570\u3002<\/strong> \u51fd\u6570\u8fd4\u56de\u8be5\u53c2\u6570\u7684\u5e73\u65b9\u6839\u3002\u5982\u679c\u53c2\u6570\u662f\u8d1f\u6570\uff0c\u5219\u7ed3\u679c\u4e3aNaN\uff08\u975e\u6570\u5b57\uff09\uff0c\u8fd9\u662f\u9700\u8981\u7279\u522b\u6ce8\u610f\u7684\u4e00\u4e2a\u70b9\u3002<\/p>\n<\/p>\n<p><p>\u5728\u4f7f\u7528<code>sqrt<\/code>\u51fd\u6570\u65f6\uff0c<strong>\u6700\u5e38\u89c1\u7684\u7528\u9014\u5305\u62ec\u6570\u5b66\u8ba1\u7b97\u3001\u79d1\u5b66\u7814\u7a76\u548c\u5de5\u7a0b\u95ee\u9898\u89e3\u51b3\u4e2d\u5bf9\u6570\u503c\u7684\u5e73\u65b9\u6839\u6c42\u89e3<\/strong>\u3002\u4f8b\u5982\uff0c\u5728\u8ba1\u7b97\u4e00\u4e2a\u7269\u4f53\u79fb\u52a8\u7684\u76f4\u7ebf\u8ddd\u79bb\u65f6\uff0c\u7ecf\u5e38\u4f1a\u7528\u5230\u8fd9\u4e2a\u51fd\u6570\uff0c\u5c24\u5176\u662f\u5728\u5904\u7406\u52fe\u80a1\u5b9a\u7406\u7684\u8ba1\u7b97\u4e2d\uff0c\u5b83\u662f\u6c42\u89e3\u76f4\u89d2\u4e09\u89d2\u5f62\u659c\u8fb9\u957f\u5ea6\u7684\u5173\u952e\u51fd\u6570\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u5f15\u5165<code>sqrt<\/code>\u51fd\u6570<\/h3>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u8981\u4f7f\u7528<code>sqrt<\/code>\u51fd\u6570\uff0c<strong>\u5fc5\u987b\u5728\u7a0b\u5e8f\u6587\u4ef6\u7684\u9876\u90e8\u5305\u542b\u5934\u6587\u4ef6<code>&lt;math.h&gt;<\/code><\/strong>\u3002\u8fd9\u4e2a\u5934\u6587\u4ef6\u4e2d\u5b9a\u4e49\u4e86\u5404\u79cd\u6570\u5b66\u51fd\u6570\uff0c\u5305\u62ec\u7528\u4e8e\u8ba1\u7b97\u5e73\u65b9\u6839\u7684<code>sqrt<\/code>\u51fd\u6570\u3002<\/p>\n<\/p>\n<p><p>\u5305\u542b\u5934\u6587\u4ef6\u7684\u8bed\u53e5\u662f\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-c\">#include &lt;math.h&gt;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u53ea\u6709\u5305\u542b\u4e86\u8fd9\u4e2a\u5934\u6587\u4ef6\uff0c\u7a0b\u5e8f\u624d\u80fd\u8bc6\u522b\u5e76\u4f7f\u7528<code>sqrt<\/code>\u51fd\u6570\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u4f7f\u7528\u4f8b\u5b50<\/h3>\n<\/p>\n<p><p><strong>\u63a5\u4e0b\u6765\uff0c\u901a\u8fc7\u8c03\u7528<code>sqrt<\/code>\u51fd\u6570\u8ba1\u7b97\u4e00\u4e2a\u6570\u7684\u5e73\u65b9\u6839<\/strong>\u3002\u51fd\u6570\u7684\u539f\u578b\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-c\">double sqrt(double x);<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u610f\u5473\u7740<code>sqrt<\/code>\u51fd\u6570\u63a5\u53d7\u4e00\u4e2a<code>double<\/code>\u7c7b\u578b\u7684\u53c2\u6570\uff0c\u5e76\u8fd4\u56de\u8be5\u53c2\u6570\u7684\u5e73\u65b9\u6839\uff0c\u540c\u6837\u4ee5<code>double<\/code>\u7c7b\u578b\u51fa\u73b0\u3002\u5982\u679c\u9700\u8981\u8ba1\u7b97\u5176\u4ed6\u7c7b\u578b\u6570\u503c\u7684\u5e73\u65b9\u6839\uff0c\u5982<code>float<\/code>\u6216<code>int<\/code>\uff0cC\u8bed\u8a00\u4f1a\u81ea\u52a8\u8fdb\u884c\u7c7b\u578b\u8f6c\u6362\uff0c\u4f46\u5728\u8ba1\u7b97\u7ed3\u679c\u65f6\u53ef\u80fd\u4f1a\u56e0\u4e3a\u7cbe\u5ea6\u95ee\u9898\u800c\u6709\u6240\u4e0d\u540c\u3002<\/p>\n<\/p>\n<p><p>\u4f8b\u5982\uff0c\u8ba1\u7b979\u7684\u5e73\u65b9\u6839\u7684\u7b80\u5355\u7a0b\u5e8f\u5982\u4e0b\u6240\u793a\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-c\">#include &lt;stdio.h&gt;<\/p>\n<p>#include &lt;math.h&gt;<\/p>\n<p>int m<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>n() {<\/p>\n<p>    double num = 9.0;<\/p>\n<p>    double result = sqrt(num);<\/p>\n<p>    printf(&quot;The square root of %.2f is %.2f\\n&quot;, num, result);<\/p>\n<p>    return 0;<\/p>\n<p>}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u6bb5\u4ee3\u7801\u5c55\u793a\u4e86<code>sqrt<\/code>\u51fd\u6570\u7684\u57fa\u672c\u7528\u6cd5\uff0c\u8ba1\u7b97\u5e76\u8f93\u51fa\u4e86\u6570\u5b579\u7684\u5e73\u65b9\u6839\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u5904\u7406\u8d1f\u6570\u8f93\u5165<\/h3>\n<\/p>\n<p><p>\u5728\u4f7f\u7528<code>sqrt<\/code>\u51fd\u6570\u65f6\uff0c\u5bf9\u4e8e\u53c2\u6570\u7684\u5904\u7406\u9700\u8981<strong>\u6ce8\u610f\uff0c<code>sqrt<\/code>\u51fd\u6570\u4e0d\u63a5\u53d7\u8d1f\u6570\u4f5c\u4e3a\u53c2\u6570<\/strong>\u3002\u5982\u679c\u5c1d\u8bd5\u5c06\u8d1f\u6570\u4f20\u9012\u7ed9<code>sqrt<\/code>\uff0c\u7a0b\u5e8f\u53ef\u80fd\u4f1a\u8fd4\u56de\u9519\u8bef\u6216NaN\u3002<\/p>\n<\/p>\n<p><p>\u4e3a\u4e86\u5728\u53ef\u80fd\u51fa\u73b0\u8d1f\u6570\u7684\u60c5\u51b5\u4e0b\u5b89\u5168\u5730\u4f7f\u7528<code>sqrt<\/code>\u51fd\u6570\uff0c\u53ef\u4ee5\u5728\u8c03\u7528\u51fd\u6570\u4e4b\u524d\u5148\u68c0\u67e5\u53c2\u6570\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-c\">double squareRoot(double num) {<\/p>\n<p>    if (num &lt; 0) {<\/p>\n<p>        printf(&quot;Cannot calculate the square root of a negative number.\\n&quot;);<\/p>\n<p>        return -1; \/\/ \u6216\u8005\u8fd4\u56de\u5176\u4ed6\u8868\u660e\u9519\u8bef\u7684\u503c\u6216\u884c\u4e3a<\/p>\n<p>    }<\/p>\n<p>    return sqrt(num);<\/p>\n<p>}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u4e2a\u7b80\u5355\u7684\u51fd\u6570\u5728\u5c1d\u8bd5\u8ba1\u7b97\u4e4b\u524d\u68c0\u67e5\u6570\u503c\u662f\u5426\u4e3a\u8d1f\uff0c\u589e\u52a0\u4e86\u4ee3\u7801\u7684\u5065\u58ee\u6027\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u5728\u590d\u6742\u5e94\u7528\u4e2d\u7684\u8fd0\u7528<\/h3>\n<\/p>\n<p><p><strong><code>sqrt<\/code>\u51fd\u6570\u5728\u79d1\u5b66\u3001\u5de5\u7a0b\u548c\u6570\u5b66\u8ba1\u7b97\u4e2d\u6709\u5e7f\u6cdb\u5e94\u7528<\/strong>\u3002\u4f8b\u5982\uff0c\u5728\u7269\u7406\u5b66\u4e2d\uff0c\u6839\u636e\u725b\u987f\u7b2c\u4e8c\u5b9a\u5f8b\u8ba1\u7b97\u7269\u4f53\u7684\u901f\u5ea6\u65f6\uff0c\u7ecf\u5e38\u9700\u8981\u6c42\u89e3\u901f\u5ea6\u7684\u5e73\u65b9\u6839\uff1b\u5728\u7edf\u8ba1\u5b66\u4e2d\uff0c\u8ba1\u7b97\u6807\u51c6\u5dee\u65f6\u4e5f\u4f1a\u7528\u5230\u8be5\u51fd\u6570\u3002<\/p>\n<\/p>\n<p><p>\u8fdb\u4e00\u6b65\u5730\uff0c<code>sqrt<\/code>\u51fd\u6570\u4e5f\u5e38\u7528\u4e8e\u56fe\u5f62\u5b66\u4e2d\uff0c\u8fdb\u884c\u8ddd\u79bb\u8ba1\u7b97\u548c\u56fe\u5f62\u6e32\u67d3\u8fc7\u7a0b\u4e2d\u7684\u5149\u7ebf\u8ffd\u8e2a\u3002\u901a\u8fc7\u5229\u7528\u5e73\u65b9\u6839\u7684\u8ba1\u7b97\uff0c\u53ef\u4ee5\u6a21\u62df\u5149\u7ebf\u4e0e\u7269\u4f53\u7684\u4ea4\u4e92\uff0c\u4ece\u800c\u751f\u6210\u903c\u771f\u7684\u56fe\u50cf\u3002<\/p>\n<\/p>\n<p><h3>\u4e94\u3001\u4f18\u5316\u548c\u6ce8\u610f\u4e8b\u9879<\/h3>\n<\/p>\n<p><p>\u5728\u5927\u91cf\u8fd0\u7b97\u548c\u9700\u8981\u9ad8\u6027\u80fd\u7684\u7a0b\u5e8f\u4e2d\uff0c<code>sqrt<\/code>\u51fd\u6570\u7684\u6548\u7387\u53ef\u80fd\u6210\u4e3a\u4e00\u4e2a\u8003\u8651\u56e0\u7d20\u3002<strong>\u867d\u7136\u73b0\u4ee3\u7f16\u8bd1\u5668\u5df2\u7ecf\u5bf9<code>sqrt<\/code>\u51fd\u6570\u8fdb\u884c\u4e86\u4f18\u5316<\/strong>\uff0c\u4f46\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u4f8b\u5982\u5d4c\u5165\u5f0f\u7cfb\u7edf\u6216\u8d44\u6e90\u53d7\u9650\u7684\u73af\u5883\u4e2d\uff0c\u8ba1\u7b97\u6027\u80fd\u4ecd\u7136\u662f\u4e00\u4e2a\u91cd\u8981\u7684\u8003\u91cf\u3002<\/p>\n<\/p>\n<p><p>\u6b64\u5916\uff0c<strong>\u5728\u4f7f\u7528<code>sqrt<\/code>\u51fd\u6570\u65f6\uff0c\u8fd8\u5e94\u6ce8\u610f\u6570\u503c\u7684\u7cbe\u786e\u5ea6\u95ee\u9898<\/strong>\u3002\u7531\u4e8e\u6d6e\u70b9\u6570\u7684\u8868\u793a\u65b9\u5f0f\uff0c\u67d0\u4e9b\u6570\u503c\u7684\u5e73\u65b9\u6839\u53ef\u80fd\u65e0\u6cd5\u5b8c\u5168\u7cbe\u786e\u8868\u793a\uff0c\u5bfc\u81f4\u8ba1\u7b97\u7ed3\u679c\u5b58\u5728\u5fae\u5c0f\u7684\u8bef\u5dee\u3002\u5728\u5927\u591a\u6570\u5e94\u7528\u4e2d\uff0c\u8fd9\u79cd\u8bef\u5dee\u662f\u53ef\u4ee5\u63a5\u53d7\u7684\uff0c\u4f46\u5728\u5bf9\u7cbe\u5ea6\u8981\u6c42\u6781\u9ad8\u7684\u79d1\u5b66\u8ba1\u7b97\u4e2d\uff0c\u5219\u9700\u8981\u91c7\u53d6\u989d\u5916\u7684\u63aa\u65bd\u6765\u786e\u4fdd\u8ba1\u7b97\u7ed3\u679c\u7684\u51c6\u786e\u5ea6\u3002<\/p>\n<\/p>\n<p><p>\u603b\u7684\u6765\u8bf4\uff0c<code>sqrt<\/code>\u51fd\u6570\u662fC\u8bed\u8a00\u4e2d\u4e00\u4e2a\u975e\u5e38\u6709\u7528\u7684\u6570\u5b66\u5de5\u5177\uff0c\u901a\u8fc7\u6b63\u786e\u5e94\u7528\uff0c\u53ef\u4ee5\u5728\u5404\u79cd\u7a0b\u5e8f\u4e2d\u5b9e\u73b0\u5e73\u65b9\u6839\u7684\u51c6\u786e\u8ba1\u7b97\u3002\u4e0d\u8fc7\uff0c\u4f7f\u7528\u65f6\u4e5f\u9700\u8981\u6ce8\u610f\u53c2\u6570\u7684\u6709\u6548\u6027\u3001\u8ba1\u7b97\u7684\u7cbe\u5ea6\u548c\u6027\u80fd\u4f18\u5316\u7b49\u95ee\u9898\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p><strong>\u4ec0\u4e48\u662fC\u8bed\u8a00\u7684sqrt\u51fd\u6570\uff1f<\/strong><br \/>\nC\u8bed\u8a00\u4e2d\u7684sqrt\u51fd\u6570\u662f\u7528\u4e8e\u8ba1\u7b97\u4e00\u4e2a\u6570\u7684\u5e73\u65b9\u6839\u7684\u51fd\u6570\u3002\u5b83\u5c5e\u4e8eC\u6807\u51c6\u5e93\u4e2d\u7684\u4e00\u4e2a\u6570\u5b66\u51fd\u6570\uff0c\u53ef\u4ee5\u7528\u6765\u5904\u7406\u5404\u79cd\u6570\u503c\u8ba1\u7b97\u95ee\u9898\u3002<\/p>\n<p><strong>\u5982\u4f55\u5728C\u8bed\u8a00\u4e2d\u4f7f\u7528sqrt\u51fd\u6570\u8fdb\u884c\u5e73\u65b9\u6839\u8ba1\u7b97\uff1f<\/strong><br \/>\n\u8981\u4f7f\u7528C\u8bed\u8a00\u4e2d\u7684sqrt\u51fd\u6570\u8fdb\u884c\u5e73\u65b9\u6839\u8ba1\u7b97\uff0c\u60a8\u9700\u8981\u5728\u4ee3\u7801\u4e2d\u5f15\u5165&lt;math.h&gt;\u5934\u6587\u4ef6\uff0c\u7136\u540e\u76f4\u63a5\u8c03\u7528sqrt\u51fd\u6570\uff0c\u5e76\u5c06\u9700\u8981\u8ba1\u7b97\u5e73\u65b9\u6839\u7684\u6570\u4f5c\u4e3a\u53c2\u6570\u4f20\u9012\u7ed9sqrt\u51fd\u6570\u3002sqrt\u51fd\u6570\u5c06\u8fd4\u56de\u8ba1\u7b97\u51fa\u7684\u5e73\u65b9\u6839\u503c\u3002<\/p>\n<p><strong>\u80fd\u5426\u7ed9\u51fa\u4e00\u4e2a\u5177\u4f53\u7684C\u8bed\u8a00sqrt\u51fd\u6570\u7684\u793a\u4f8b\u4ee3\u7801\uff1f<\/strong><br \/>\n\u5f53\u7136\u53ef\u4ee5\uff01\u4e0b\u9762\u662f\u4e00\u4e2a\u7b80\u5355\u7684C\u8bed\u8a00sqrt\u51fd\u6570\u7684\u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n<pre><code class=\"language-c\">#include &lt;stdio.h&gt;\n#include &lt;math.h&gt;\n\nint main() {\n   double num = 16.0;\n   double result = sqrt(num);\n   \n   printf(&quot;\u6570\u5b57 %.2f \u7684\u5e73\u65b9\u6839\u662f %.2f\\n&quot;, num, result);\n   return 0;\n}\n<\/code><\/pre>\n<p>\u8fd9\u4e2a\u793a\u4f8b\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u5148\u5b9a\u4e49\u4e86\u4e00\u4e2a\u53d8\u91cf<code>num<\/code>\u5e76\u8d4b\u503c\u4e3a16.0\uff0c\u7136\u540e\u8c03\u7528sqrt\u51fd\u6570\u8ba1\u7b97\u51fanum\u7684\u5e73\u65b9\u6839\u5e76\u5c06\u7ed3\u679c\u5b58\u50a8\u5728\u53d8\u91cf<code>result<\/code>\u4e2d\uff0c\u6700\u540e\u4f7f\u7528printf\u51fd\u6570\u5c06\u7ed3\u679c\u8f93\u51fa\u5230\u63a7\u5236\u53f0\u4e0a\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"C\u8bed\u8a00\u4e2d\u7684sqrt\u51fd\u6570\u7528\u4e8e\u8ba1\u7b97\u4e00\u4e2a\u6570\u7684\u5e73\u65b9\u6839\u3002\u8981\u4f7f\u7528\u8be5\u51fd\u6570\uff0c\u7528\u6237\u9700\u8981\u5305\u542b\u5934\u6587\u4ef6&lt;math.h&gt;\uff0c\u5e76 [&hellip;]","protected":false},"author":3,"featured_media":296553,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[37],"tags":[],"acf":[],"_links":{"self":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/296537"}],"collection":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/comments?post=296537"}],"version-history":[{"count":0,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/296537\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/296553"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=296537"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=296537"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=296537"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}