Announcement

Collapse

Computer Lab Guidelines

Here in the computer lab, we talk about cool tech, the newest coolest gadgets, and tackle your toughest tech questions.

If you need to refresh yourself on the decorum, now would be a good time. Forum Rules: here
See more
See less

A QBasic Program that calculates . . .

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • A QBasic Program that calculates . . .

    Just for fun, see if you can guess what this program calculates:

    Code:
    A# = 1#
    B# = .5#
    C# = SQR(2#) / 2#
    D# = .25#
    LblA:
    E# = A#
    B# = 2# * B#
    A# = (A# + C#) / 2#
    C# = SQR(E# * C#)
    D# = D# - B# * (A# - E#) * (A# - E#)
    oldx# = x#
    x# = (A# + C#) * (A# + C#) / (4# * D#)
    PRINT x#
    IF oldx# <> x# THEN GOTO LblA
    SYSTEM
    . . . the gospel of Christ: for it is the power of God unto salvation to every one that believeth; . . . -- Romans 1:16 KJV

    . . . that Christ died for our sins according to the scriptures; And that he was buried, and that he rose again the third day according to the scriptures: . . . -- 1 Corinthians 15:3-4 KJV

    Whosoever believeth that Jesus is the Christ is born of God: . . . -- 1 John 5:1 KJV

  • #2
    a range of prime numbers?
    The first to state his case seems right until another comes and cross-examines him.

    Comment


    • #3
      Originally posted by 37818 View Post
      Just for fun, see if you can guess what this program calculates:

      Code:
      A# = 1#
      B# = .5#
      C# = SQR(2#) / 2#
      D# = .25#
      LblA:
      E# = A#
      B# = 2# * B#
      A# = (A# + C#) / 2#
      C# = SQR(E# * C#)
      D# = D# - B# * (A# - E#) * (A# - E#)
      oldx# = x#
      x# = (A# + C#) * (A# + C#) / (4# * D#)
      PRINT x#
      IF oldx# <> x# THEN GOTO LblA
      SYSTEM
      Its honestly been a while since I've last looked at QBASIC code. I'm not sure why you're willing to torture yourself with it, but let me get a QBASIC interpreter and see what this code actually does, before trying to guess what it does.

      Comment


      • #4
        Ah wait, this is recognisable now. Its the Gauss-Legendre's quadratic algorithm for calculating pi.

        You should still initisalise x even though grabbing something random off the memory stack probably won't make oldx match x.

        And its prettier in C.

        Code:
        int main()
        {
        double a = 1.0, b = 0.5, c = sqrt(2.0)/2, d = 1.0/4, e, x = 0.0, oldx;
        
        do {
          e = a;
          b = 2*b;
          a = (a+c)/2;
          c = sqrt(e*c);
          d = d - b * (a - e) * (a - e);
          oldx = x;
          x = (a + c) * (a + c) / (4 * d);
          fprintf(stdout,"%f\n",x);
        }
        while (x != oldx)
        
        return 0;
        }

        Comment

        Related Threads

        Collapse

        Topics Statistics Last Post
        Started by Ronson, 03-20-2024, 07:20 PM
        2 responses
        28 views
        0 likes
        Last Post rogue06
        by rogue06
         
        Started by Christian3, 03-15-2024, 10:15 AM
        13 responses
        64 views
        0 likes
        Last Post QuantaFille  
        Working...
        X