Author Topic: Pi calculation  (Read 283 times)

Galileo

  • Jr. Member
  • **
  • Posts: 84
    • View Profile
Pi calculation
« on: 05. September 2018, 19:26:51 »
I'm back.

Calculation of "some" pi digits.

Code: [Select]
// Cálculo de pi --- Pi calculation
// Translation from Phix code to Yabasic by Galileo, 09/2018.

n = 35001 : a = 10000 : dim f(n)

for i = 1 to n : f(i) = 2000 : next i
 
for c = n to 14 step -14
d = 0
for b = c to 1 step -1
d = d * b
g = b * 2 - 1
d = d + f(b) * a
f(b) = mod(d, g)
d = int(d / g)
k = e + int(d / a)
next
k2$ = "000000" + str$(k)
print right$(k2$, 4);
e = mod(d, a)
next

print "\nTime insumed: ", peek("millisrunning") / 1000, " seconds."

In my machine, delayed about 55 secs. Phix code about 21 secs.

Tomaaz

  • Full Member
  • ***
  • Posts: 213
    • View Profile
Re: Pi calculation
« Reply #1 on: 05. September 2018, 19:33:39 »
It looks familiar. I think that the Pix code is a translation of my code.  ;)

Galileo

  • Jr. Member
  • **
  • Posts: 84
    • View Profile
Re: Pi calculation
« Reply #2 on: 05. September 2018, 19:51:04 »
It looks familiar. I think that the Pix code is a translation of my code.  ;)

Could you show the code? What dialect did you use? What's the performance?

By the way, the same code in Lua 5.3 takes about 65 seconds (on my machine). I didn't expect it.

Tomaaz

  • Full Member
  • ***
  • Posts: 213
    • View Profile
Re: Pi calculation
« Reply #3 on: 05. September 2018, 19:55:16 »
https://openeuphoria.org/forum/m/132983.wc

Euphoria and Lua.

Pirx == Tomaaz. I had problems with registering on that forum and tried several times. Finally they activated that Pirx account. Luajit less than 1 sec.
« Last Edit: 05. September 2018, 19:59:46 by Tomaaz »

Galileo

  • Jr. Member
  • **
  • Posts: 84
    • View Profile
Re: Pi calculation
« Reply #4 on: 06. September 2018, 17:05:05 »
But LuaJIT doesn't belong in the same league. Or am I wrong?

Tomaaz

  • Full Member
  • ***
  • Posts: 213
    • View Profile
Re: Pi calculation
« Reply #5 on: 06. September 2018, 18:58:12 »
But LuaJIT doesn't belong in the same league.

And why would that be?

ZXDunny

  • Full Member
  • ***
  • Posts: 237
    • View Profile
Re: Pi calculation
« Reply #6 on: 07. September 2018, 00:00:31 »
About the only reason I can think of is that LuaJIT executes compiled code (in the form of a dynarec) as opposed to a bytecode or pure interpreting engine.

Tomaaz

  • Full Member
  • ***
  • Posts: 213
    • View Profile
Re: Pi calculation
« Reply #7 on: 07. September 2018, 00:26:09 »
About the only reason I can think of is that LuaJIT executes compiled code (in the form of a dynarec) as opposed to a bytecode or pure interpreting engine.

Yes, but by accepting this point of view you agree that we can't compare almost anything. BaCon, Pure Basic, NaaLaa, Yabasic, Luajit - they all do things in a different way. You can't even compare 32-bit version of FreeBASIC to it's 64-bit equivalent, cause one translates to C and use C compiler, while the other don't (forgive me, if I'm wrong). And I think it's more interesting to compare different things than to compare similar things.

ZXDunny

  • Full Member
  • ***
  • Posts: 237
    • View Profile
Re: Pi calculation
« Reply #8 on: 07. September 2018, 14:09:30 »
To be honest, I've been thinking about using a Dynarec for SpecBAS. There's only so far a stack-based bytecode implementation will take you for speed concerns. But then, SpecBAS doesn't really need it as nobody is using it for CPU-intensive tasks (and that's not a job for a BASIC in any case, IMO), and as all the heavy lifting is done via my library of support routines, the language itself is just a method for calling that at the end of the day.

To be fair, I suppose if LuaJIT is BASIC-alike enough in its language implementation (and that's a very, very wide field these days - we have strayed so, so far from Dartmouth) then it can indeed be compared to others.

Galileo

  • Jr. Member
  • **
  • Posts: 84
    • View Profile
Re: Pi calculation
« Reply #9 on: 07. September 2018, 19:49:00 »
Phix-to-Yabasic conversion again. Little faster.

Code: [Select]
t1 = peek("millisrunning")

chki = 1
a = 10000
c = 35000 // 8400
fr = int(a / 5)
dim f(c)

for i = 1 to c : f(i) = fr : next

while(c > 0)
    g = 2 * c
    d = 0
    b = c
    while(b > 0)
        d = d + f(b) * a
        g = g - 1
        f(b) = mod(d, g)
        d = int(d / g)
        g = g - 1
        b = b - 1
        if not(b = 0) d = d * b
    wend
    c = c - 14
    n4$ = str$(e + int(d / a),"%04g")
    print n4$;
    e = mod(d, a)
wend

print "\nTime elapsed: ", (peek("millisrunning") - t1) / 1000, " seconds"