C, Perl, and Python similar loops different results

2024/11/19 10:21:39

I wrote scripts to calculate pi in python, perl and c. They all use the same algorithm (trapezoidal reimann sum of a circle with n subintervals) and the python and perl programs always get the same result when n is the same. However, the c program doesn't get the same answer like it should, it actually overestimates pi, which is impossible. What is wrong with the c program?

Python:

#!/usr/bin/python
n = 1000000
def f(x):return (1-(float(x)**2))**float(0.5)
val = 0
for i in range(n):
i = i+1
val = val+f(float(i)/float(n))
val = val*2
pi = (float(2)/n)*(float(1)+val)
print pi

Perl:

#!/usr/bin/perl
$n = 1000000;
$h = 0;
for($a = 1; $a < $n; ++$a){$t = $a/$n;$val = (1-(($t)**2))**0.5;$h+=$val;
}
$h = $h*2;
$pi = (2/$n)*(1+$h);
printf "%.11f", $pi;

C:

#include <stdio.h>
#include <math.h>
int main()
{int count, n = 1000000;double pi;double val = 0.0, p = 0.50, x = 1.0, four = 4.0;for(count = 1; count < n; ++count){val += (pow(x-(((double)count/(long double)n)*((double)count/(long 
double)n)), p));}pi = (val + x) * (four/(long double)n);printf("%.11f", pi);
}

Results: C = 3.14159465241 Perl and Python = 3.14159265241

Answer

Well, comparing your methods, it became obvious your final operation for calculating pi was incorrect.

Replace pi = (val + x) * (four/(long double)n); with these two lines:

val = val * (long double)2.0;
pi = (val + x) * ((long double)2.0/(long double)n);

Compiling and running gives:

3.14159265241

Which I believe is the output you're looking for.

https://en.xdnf.cn/q/119965.html

Related Q&A

How to print a single backslash in python in a string? [duplicate]

This question already has answers here:Why do backslashes appear twice?(2 answers)Closed 1 year ago.In python (3x version)\ # this is error no doubt \\ # this prints two backslashes ,i.e., \\ r\ # thi…

Return nested JSON item that has multiple instances

So i am able to return almost all data, except i am not able to capture something like this:"expand": "schema""issues": [{"expand": "<>","id…

What request should I make to get the followers list from a specific profile page

I am trying to get the followers list from this profile, I tried making a GET request using python requests to the API using this request URL but it didnt seem to work, I got a METHOD_NOT_ALLOWED error…

PlayerDB API Post Requests bring 404

I made a little script to get the UUIDs of people who joined my minecraft server, then run them through the PlayerDB API through a post request to: https://playerdb.co/api/player/minecraft/* where the …

How to make changes using Configparser in .ini file persistent

How to modify the .ini file? My ini file looks like this. And i want the format section ini to be changed like this[Space to be replaced with a tab followed by $] Format="[%TimeStamp%] $(%Thre…

How to structure template libraries in a Django project? [closed]

Closed. This question is opinion-based. It is not currently accepting answers.Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.Clo…

Not able to click button with Selenium (Python)

Im trying to click a button using Selenium, but everytime I get the message that it cant find the element. This happens even when I put a time.sleep() in front of it. time.sleep(5)#Click on downloaddow…

how to aproximate shapes height and width for image detection using opencv and python

i was following a tutorial about shapes detection using opencv ,numpy and python ,and it was this function i know the reason from it but i do not know how to modify it so i can use it as i want the to…

Using str.replace in a for loop

I am working on an assignment that is asking me to change the below code so that line 4 uses str.isalnum and lines 5-7 become uses only one line using str.replace.s = p55w-r@d result = for c in s:if(c…

extract all vertical slices from numpy array

I want to extract a complete slice from a 3D numpy array using ndeumerate or something similar. arr = np.random.rand(4, 3, 3)I want to extract all possible arr[:, x, y] where x, y range from 0 to 2