Author Topic: Calculating Pi  (Read 4851 times)

0 Members and 1 Guest are viewing this topic.

Offline Dragoncat

  • Newbie
  • Posts: 10
    • View Profile
Calculating Pi
« on: May 07, 2019, 08:58:28 pm »
a# = 1
b# = 1 / (2 ^ .5)
t# = 1 / 4
p# = 1
FOR l = 0 TO 9
    s# = (a# + b#) / 2
    u# = (a# * b#) ^ .5
    v# = t# - p# * (a# - s#) ^ 2
    p# = 2 * p#
    a# = s#
    b# = u#
    t# = v#
    f# = ((a# + b#) ^ 2) / (4 * t#)
    PRINT f#
NEXT l
the above program generates the value of Pi to the maximum precision of double precision numbers in just 3 iterations... is there any way you can have a value that you can add multiply and raise to a power or root or divide etc that would have more precision, like say 2X the precision or perhaps even an ungodly 10X or 1000X the precision that can be easily implimented? what can I say I am obsessed with precision... thanks in advance.

Offline jack

  • Seasoned Forum Regular
  • Posts: 408
    • View Profile
Re: Calculating Pi
« Reply #1 on: May 07, 2019, 09:37:04 pm »
there's the spigot algorithm for Pi https://www.qb64.org/forum/index.php?topic=1154.msg103525#msg103525
though certainly not the fastest algorithm

Offline Dragoncat

  • Newbie
  • Posts: 10
    • View Profile
Re: Calculating Pi
« Reply #2 on: May 07, 2019, 09:57:08 pm »
what I need is quadruple 256 bit floating point precision math support in qb64... that would make things simpler for me... in other words to support a 256 floating point number, I know that's a lot of bytes for a number, but I think it would be great for mathematical accuracy and all...

Offline jack

  • Seasoned Forum Regular
  • Posts: 408
    • View Profile
Re: Calculating Pi
« Reply #3 on: May 08, 2019, 05:37:07 am »
you can use the mpfr  library https://www.mpfr.org, see example here https://www.qb64.org/forum/index.php?topic=1175.0

Offline Dragoncat

  • Newbie
  • Posts: 10
    • View Profile
Re: Calculating Pi
« Reply #4 on: May 08, 2019, 11:00:59 am »
I tried looking at that it hurt my eyes.. it is overly complicated, isn't there a simpler way of using really big high precision numbers??
I need a much simpler way.. something like BigNumAdd$(a$,b$) where a$ and b$ are strings that represent NUMBERS and that can INCLUDE a decimal point and  / or a SIGN (-) isn't there something like that in Qbasic? there should be all the math functions built in to QB64 like this it would make life so much easier for us all, native big decimal support in native Qbasic, what a NOVEL IDEA THAT WOULD BE HA?? ALSO please fix/restore the ability to have subroutine programs without us getting "invalid name" errors PLEASE!!

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Calculating Pi
« Reply #5 on: May 08, 2019, 11:33:54 am »
I tried looking at that it hurt my eyes.. it is overly complicated, isn't there a simpler way of using really big high precision numbers??
I need a much simpler way.. something like BigNumAdd$(a$,b$) where a$ and b$ are strings that represent NUMBERS and that can INCLUDE a decimal point and  / or a SIGN (-) isn't there something like that in Qbasic? there should be all the math functions built in to QB64 like this it would make life so much easier for us all, native big decimal support in native Qbasic, what a NOVEL IDEA THAT WOULD BE HA?? ALSO please fix/restore the ability to have subroutine programs without us getting "invalid name" errors PLEASE!!

Pete was working on string math. Steve had something worked out some time ago...

Quote
ALSO please fix/restore the ability to have subroutine programs without us getting "invalid name" errors PLEASE!!

??? Do you have code example because it should be working fine.

BTW that is a sweet and fast calculation of PI!
Compared this:
Code: QB64: [Select]
  1. _TITLE "pi calc Dragoncat"
  2. a = 1
  3. b = 1 / (2 ^ .5)
  4. t = 1 / 4
  5. p = 1
  6. f = 3
  7. WHILE lastf <> f
  8.     lastf = f
  9.     s = (a + b) / 2
  10.     u = (a * b) ^ .5
  11.     v = t - p * (a - s) ^ 2
  12.     p = 2 * p
  13.     a = s
  14.     b = u
  15.     t = v
  16.     f = ((a + b) ^ 2) / (4 * t)
  17.     loopcnt = loopcnt + 1
  18.     PRINT loopcnt, f
  19.  
  20.  

with this:
Code: QB64: [Select]
  1. _TITLE "pi calc by Nilakantha" '   ref: https://en.wikipedia.org/wiki/Pi
  2. pE = 3: n = 2: s = -1
  3. WHILE lastPE <> pE
  4.     lastPE = pE
  5.     s = s * -1
  6.     t = s * 4 / (n * (n + 1) * (n + 2))
  7.     pE = pE + t
  8.     PRINT n, n + 1, n + 2, pE
  9.     n = n + 2
  10.  

« Last Edit: May 08, 2019, 11:37:47 am by bplus »

Offline Dragoncat

  • Newbie
  • Posts: 10
    • View Profile
Re: Calculating Pi
« Reply #6 on: May 08, 2019, 11:37:10 am »
oh the invalid name problem was fixed, it was that I was trying to pass an argument where the program was not expecting an argument to be passed, I hope it did not "break" the program.. it is a very delicate random number generator.

Offline Dragoncat

  • Newbie
  • Posts: 10
    • View Profile
Re: Calculating Pi
« Reply #7 on: May 08, 2019, 12:03:04 pm »
YEs thanks that program for calculating pi uses the Gauss-Legendre algorith  also known as Gauss - Euler or Brent-Salamin or Salamin-brent (named after the inventors who came up with this method)  with this algorythm pi was calculated to 206,158,430,000 decimal places and the results were checked with Borwein's algorithm, (sept 18 - 20 1999 was when the calculation took place.)  awesome ha??

Offline jack

  • Seasoned Forum Regular
  • Posts: 408
    • View Profile
Re: Calculating Pi
« Reply #8 on: May 08, 2019, 01:01:00 pm »
I tried looking at that it hurt my eyes.. it is overly complicated, isn't there a simpler way of using really big high precision numbers??
there's DecimalBasic that has precision upto 1000 digits, also, there's a basic compiler that supports operator overloading making the use of mpfr as easy as any built-in data type, you will have to guess which compiler I am referring to, as to DecimalBasic google will lead you there.

Offline Dragoncat

  • Newbie
  • Posts: 10
    • View Profile
Re: Calculating Pi
« Reply #9 on: May 08, 2019, 07:33:30 pm »
ok thanks a lot....