Logo Hardware.com.br
Bit0N3
Bit0N3 Cyber Highlander Registrado
14.5K Mensagens 3.4K Curtidas
#4 Por Bit0N3
05/02/2014 - 10:31
Não precisa calcular o Pi, ele é uma constante.

decora ai

3,14159 26535 89793 23846 26433 83279 50288 41971 69399 37510 58209 74944 59230 78164 06286 20899 86280 34825 34211 70679 82148 08651 32823 06647 09384 46095 50582 23172 53594 08128 48111 74502 84102 70193 85211 05559 64462 29489 54930 38196 44288 10975 66593 34461 28475 64823 37867 83165 27120 19091 45648 56692 34603 48610 45432 66482 13393 60726 02491 41273 ...

e ai você coloca o numero que quiser de casas na sua conta.

[]'s
Recomendação: Lord of the rings online MMORPG SHow de bola, roda no fedora pelo lutris.
Meyer!
Meyer! Ubbergeek Registrado
3.9K Mensagens 535 Curtidas
#5 Por Meyer!
05/02/2014 - 11:14
Henry-Keys disse:

Eu vi um método uma vez, que usa um tal de "arctan" pra calcular, tenho o source em c# (adaptado da net), talvez possa te dar umas ideias:

[php]using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Numerics;
using System.Threading.Tasks;
using System.Threading;
using System.Diagnostics;
using System.Collections;
using System.Collections.Concurrent;

namespace pid
{
class Program
{
static int Main(string[] args)
{
if (args.Length < 1 || args.Length > 2) { return 1; }
if (args.Length == 2 && args[1] == "rt")
{
Process.GetCurrentProcess().PriorityClass = ProcessPriorityClass.RealTime;
}
if (Convert.ToInt32(args[0]) < 1) { return 1; }
Stopwatch stopWatch = new Stopwatch();
stopWatch.Start();
HighPrecision dxt;
dxt = HighPrecision.GetPi(Convert.ToInt32(args[0]));
Console.Write(dxt.ToString());
stopWatch.Stop();
TimeSpan ts = stopWatch.Elapsed;
Console.Error.WriteLine();
Console.Error.WriteLine();
Console.Error.WriteLine("Tempo em execução: " + String.Format("{0:00}:{1:00}:{2:00}.{3:00}", ts.Hours, ts.Minutes, ts.Seconds, ts.Milliseconds / 10));



return 1;


}
}
}

public class HighPrecision
{
private static BigInteger denom;
private static int precision;
private static int slop = 4;
private BigInteger num;

public HighPrecision(BigInteger numerator, BigInteger denominator)
{
// public constructor rescales numerator as needed
num = (HighPrecision.denom * numerator) / denominator;
}

private HighPrecision(BigInteger numerator)
{
// private constructor for when we already know the scaling
num = numerator;
}

public static int Precision
{
get { return precision; }
set
{
HighPrecision.precision = value;
denom = BigInteger.Pow(10, HighPrecision.precision + slop); // leave some buffer
}
}

public bool IsZero
{
get { return num.IsZero; }
}

public BigInteger Numerator
{
get { return num; }
}

public BigInteger Denominator
{
get { return HighPrecision.denom; }
}

public static HighPrecision operator *(int left, HighPrecision right)
{
// no need to resale when multiplying by an int
return new HighPrecision(right.num * left);
}

public static HighPrecision operator *(HighPrecision left, HighPrecision right)
{
// a/D * b/D = ab/D^2 = (ab/D)/D
return new HighPrecision((left.num * right.num) / HighPrecision.denom);
}

public static HighPrecision operator /(HighPrecision left, int right)
{
// no need to rescale when dividing by an int
return new HighPrecision(left.num / right);
}

public static HighPrecision operator +(HighPrecision left, HighPrecision right)
{
// when we know the denominators are the same, can just add the numerators
return new HighPrecision(left.num + right.num);
}

public static HighPrecision operator -(HighPrecision left, HighPrecision right)
{
// when we know the denominators are the same, can just subtract the numerators
return new HighPrecision(left.num - right.num);
}

public override string ToString()
{
StringBuilder sb = new StringBuilder();

// pull out the integer part
BigInteger remain;
BigInteger quotient = BigInteger.DivRem(num, HighPrecision.denom, out remain);
int integerDigits = quotient.IsZero ? 1 : (int)BigInteger.Log10(quotient) + 1;
sb.Append(quotient.ToString());

int i = 0;
BigInteger smallDenom = HighPrecision.denom / 10;
BigInteger tempRemain;

// pull out all of the 0s after the decimal point
while (i++ < HighPrecision.Precision && (quotient = BigInteger.DivRem(remain, smallDenom, out tempRemain)).IsZero)
{
smallDenom /= 10;
remain = tempRemain;
sb.Append('0');
}

// append the rest of the remainder
sb.Append(remain.ToString());

// truncate and insert the decimal point
return sb.ToString().Remove(integerDigits + HighPrecision.Precision).Insert(integerDigits, ".");
}
public static HighPrecision GetPi(int digits)
{
//HighPrecision first = new HighPrecision(0);
//HighPrecision second = new HighPrecision(0);
HighPrecision.Precision = digits;
HighPrecision first = new HighPrecision(0);
HighPrecision second = new HighPrecision(0);
Thread thrFirst = new Thread(() =>
{
first = 4 * Atan(5);
});
Thread thrSecond = new Thread(() =>
{
second = Atan(239);
});
thrFirst.Start(); thrSecond.Start();
while (thrFirst.IsAlive || thrSecond.IsAlive)
{
if (!thrFirst.IsAlive) { Thread.Sleep(1); thrFirst.Abort(); }
if (!thrSecond.IsAlive) { Thread.Sleep(1); thrSecond.Abort(); }
}
/*
Task first = Task.Factory.StartNew(() =>
{
return 4 * Atan(5);
});
Task second = Task.Factory.StartNew(() =>
{
return Atan(239);
});
*/

return 4 * (first - second);
}

public static HighPrecision Atan(int denominator)
{
HighPrecision result = new HighPrecision(1, denominator);
int xSquared = denominator * denominator;

int divisor = 1;
HighPrecision term = result;
while (!term.IsZero)
{
divisor += 2;
term /= xSquared;
result -= term / divisor;

divisor += 2;
term /= xSquared;
result += term / divisor;
}
return result;
}




}


[/php]
Henry-Keys
Henry-Keys Geek Registrado
1.8K Mensagens 235 Curtidas
#9 Por Henry-Keys
05/02/2014 - 14:35
Eu acho que o melhor método é o criado pelos detentores do recorde de PI com mais dígitos, vou tentar esse mesmo:
http://en.wikipedia.org/wiki/Chudnovsky_algorithm

ice

Não precisa calcular o Pi, ele é uma constante.

decora ai

3,14159 26535 89793 23846 26433 83279 50288 41971 69399 37510 58209 74944 59230 78164 06286 20899 86280 34825 34211 70679 82148 08651 32823 06647 09384 46095 50582 23172 53594 08128 48111 74502 84102 70193 85211 05559 64462 29489 54930 38196 44288 10975 66593 34461 28475 64823 37867 83165 27120 19091 45648 56692 34603 48610 45432 66482 13393 60726 02491 41273 ...

e ai você coloca o numero que quiser de casas na sua conta.

Vc não entendeu, é mesmo usar técnicas para obter milhares de dígitos do PI para fins de benchmarking smile.png

Abraços e vlw pessoal.
JOMARUMU
JOMARUMU General de Pijama Registrado
2.7K Mensagens 342 Curtidas
#11 Por JOMARUMU
05/02/2014 - 18:09
Henry-Keys disse:

Se é para benchmarking tem que ser por algum método iterativo, mas uma alternativa boa é o MatLab, ele feito para cálculos de coisas gigantes, então tem muita ferramenta que ajuda. Na faculdade eu e uns amigos mandávamos ele randomizar matrizes enormes, 1000x1000 por exemplo, depois pedir a inversa da matriz para ver quanto tempo demorava e até que temperatura o PC chegaria.
Bit0N3
Bit0N3 Cyber Highlander Registrado
14.5K Mensagens 3.4K Curtidas
#13 Por Bit0N3
05/02/2014 - 21:55
mtulio_2000 disse:


não tinha sido ironia não. achei que ele queria o valor pra achar circunferências e área de circunferências.

ai era só por la a variável PI com o valor, e dimensionar a precisão.

mas se é para benchmarks, ai o esquema é dar um tempo, digamos 2 segundos e fazer um loop calculando que breca quando o tempo acaba, e ai pelo numero de casas que ele conseguiu no tempo dar um score.

[]'s
Recomendação: Lord of the rings online MMORPG SHow de bola, roda no fedora pelo lutris.
Henry-Keys
Henry-Keys Geek Registrado
1.8K Mensagens 235 Curtidas
#14 Por Henry-Keys
06/02/2014 - 14:50
mtulio

Pra quê criar um novo se existe o Super Pi e o Hyper Pi (para multicore)?

É só para ter algo pra fazer, programar especificamente, será que existe algum problema nisso.

JOMARUMU

Se é para benchmarking tem que ser por algum método iterativo, mas uma alternativa boa é o MatLab, ele feito para cálculos de coisas gigantes, então tem muita ferramenta que ajuda. Na faculdade eu e uns amigos mandávamos ele randomizar matrizes enormes, 1000x1000 por exemplo, depois pedir a inversa da matriz para ver quanto tempo demorava e até que temperatura o PC chegaria.

Quero achar o PI na mão mesmo, só por diversão e penso que consegui com a fórmula de Chudnovsky
[php]
#include
#include
#include
#include
#include

using namespace std;

double fac(double num) {
double result = 1.0;
for (double i=2.0; i result *= i;
return result;
}

int main() {
double pi=0.0;
for (double k = 0.0; k < 10.0; k++) {
pi += (pow(-1.0,k) * fac(6.0 * k) * (13591409.0 + (545140134.0 * k)))
/ (fac(3.0 * k) * pow(fac(k), 3.0) * pow(640320.0, 3.0 * k + 3.0/2.0));
}
pi *= 12.0;
cout << setprecision(15) << 1.0 / pi << endl;
getchar();
return 0;
}
[/php]

Abraços.
© 1999-2025 Hardware.com.br. Todos os direitos reservados.
Imagem do Modal