试图计算Pi
发布时间:2020-12-16 09:20:35 所属栏目:百科 来源:网络整理
导读:要计算Pi,您可以使用等式pi / 4 = 1-(1/3)(1/5) – (1/7)… 乘以4,你得到Pi 我创建了一个公式来计算方程相对于其位置1 /(2n-1)的每一步,并为其编写代码 #include stdio.h#include stdlib.hint main(int argc,char** argv) { double p = 0; double pi = 0; in
要计算Pi,您可以使用等式pi / 4 = 1-(1/3)(1/5) – (1/7)…
乘以4,你得到Pi 我创建了一个公式来计算方程相对于其位置1 /(2n-1)的每一步,并为其编写代码 #include <stdio.h> #include <stdlib.h> int main(int argc,char** argv) { double p = 0; double pi = 0; int j = 1; do { if (j % 2 == 1) { p = p + (1 / (2 * j - 1)); } else { p = p - (1 / (2 * j - 1)); } pi = p * 4; printf("%lfn",pi); j++; } while (j < 10); return (EXIT_SUCCESS); } 但它只是推出4.0000 解决方法
在声明中p = p(1 /(2 * j – 1));表达式(1 /(2 * j – 1)得到整数.为了得到预期的结果,使得任一操作数为浮动类型,即1.0或(float)(2 * j – 1)
替换声明 p = p + (1 / (2 * j - 1));/* 1/(2*j-1) results in integer,you won't get expected result */ 同 p = p + (1. / (2 * j - 1));/* 1. means making floating type */ (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- c# – ASP.NET MVC和MemoryCache – 我该如何使用它?
- objective-c – 单击UIWebView上的链接时打开新的UIViewCon
- Swift - 网络请求报App Transport Security has blocked a
- Swift – CGAffineTransformInvert:奇异矩阵. UITextView&
- react router 4
- 【支付】Cocos2d-x IOS内购(IAP支付)
- Vue2 Vue-cli中使用Typescript的配置详解
- C模板函数(基于Modern C Design)编译错误
- cocos2d-x多分辨率适配
- cocos2dx3.2升级Android5的坑