Stories
Slash Boxes
Comments

SoylentNews is people

The Fine print: The following are owned by whoever posted them. We are not responsible for them in any way.

Journal by day of the dalek

This journal is available for random discussion topics of interest to the community, with just a few rules:

1) No personal attacks, grumbling about site policy, or complaining about other users and the staff.
2) No racism, bigotry, or culture wars of any kind.
3) No politics of any kind. There are plenty of other places to discuss this topic. Policy discussion is okay, though, as long as it focuses on the issues instead of people or parties. Be sure to follow rule #2 here.
4) No random spamming. If you're an member of the site who intends to regularly contribute and participate, and you wish to discuss your personal projects, that's fine. If you're a drive-by spammer who's here just to promote your business or hosts file software, get lost.
5) Please be kind to each other.

Otherwise, you're welcome to discuss anything you'd like in this journal.

Display Options Threshold/Breakthrough Reply to Comment Mark All as Read Mark All as Unread
The Fine Print: The following comments are owned by whoever posted them. We are not responsible for them in any way.
  • (Score: 3, Informative) by DannyB on Tuesday September 03 2024, @07:17PM (8 children)

    by DannyB (5839) Subscriber Badge on Tuesday September 03 2024, @07:17PM (#1371072) Journal

    Successive approximation explained.

    Using successive approximation, you can, for example, compute the square root of 2.

    First you must start with a guess. I'll use my calculator to get the first guess.

    Square root of two by successive approximation:

    1.414213562.... (initial guess from my calculator)
    1.41421356
    1.4142136
    1.414214 <--- note rounding here from prior approximation
    1.41421
    1.4142
    1.414
    1.41
    1.4
    1

    Start with a guess, then continue approximating. Conclusion: the square root of two is one.

    (I had unintentionally posted this earlier to a different journal entry.)

    --
    The Jupiter 2 is headed into a region of highly energized saran wrap.
    Starting Score:    1  point
    Moderation   +1  
       Informative=1, Total=1
    Extra 'Informative' Modifier   0  
    Karma-Bonus Modifier   +1  

    Total Score:   3  
  • (Score: 1) by khallow on Wednesday September 04 2024, @01:09AM (3 children)

    by khallow (3766) Subscriber Badge on Wednesday September 04 2024, @01:09AM (#1371126) Journal
    How do you handle rounding of digit 5? For example, successive approximation can give different answers depending on how you round. Consider 2.46.

    Rounding up:

    2.46
    2.5
    3

    versus rounding to nearest even digit:

    2.46
    2.5
    2
    • (Score: 2, Touché) by Anonymous Coward on Wednesday September 04 2024, @02:34AM (2 children)

      by Anonymous Coward on Wednesday September 04 2024, @02:34AM (#1371136)

      He is a Java programmer, so the answer is obvious. How rounding is handled depends on the version of Java in use, the input type, and the desired output type. The fact that the actual rounding mode is different for the different combinations is just one of those language behaviours you have to remember. Yet another language feature that makes it the best language ever.

      • (Score: 2) by DannyB on Wednesday September 04 2024, @04:08PM (1 child)

        by DannyB (5839) Subscriber Badge on Wednesday September 04 2024, @04:08PM (#1371215) Journal

        When using the BigDecimal type (in Java), you must specify the rounding mode for operations. BigDecimal internally uses a BigInteger along with an int that keeps track of how many decimal digits are to the right of the decimal point.

        Never use floating point types for money.

        --
        The Jupiter 2 is headed into a region of highly energized saran wrap.
        • (Score: 0) by Anonymous Coward on Wednesday September 04 2024, @11:27PM

          by Anonymous Coward on Wednesday September 04 2024, @11:27PM (#1371287)

          No, you don't. Most BigDecimal operations are not infinite precision, (half of those are only infinite for certain expansions) and, therefore, don't require a MathContext. If you never use one of those, then you may not even realize it. And there are multiple MathContexts that could be considered the "default" with different rounding modes. And don't get me started on DecimalFormat having a different default rounding mode or that the defaults change for various versions.

  • (Score: 0) by Anonymous Coward on Thursday September 05 2024, @06:11AM (3 children)

    by Anonymous Coward on Thursday September 05 2024, @06:11AM (#1371329)

    For those that don't know, that's not how successive approximation works. You are supposed to start with the general value and work your way towards the specific value. You can do that with a number of algorithms. There are ones that work specifically for different operations in better than worse case. However, what I really wanted to do was to inform everyone of my favorite one using the Farey Tree.

    So to calculate answer to your operation you start with your lower and upper bound for your operation. You find the new of bounds with four steps. First, match fractions as a/c and b/d. Second, find the mediant ((a+b) / (c+d)). Third, test if your new fraction is higher or lower than the answer you seek. Finally, replace the respective bounds. Then keep going until the desired precision is reached or you get tired of waiting.

    So for the square root of 2. The bounds of square root are 1 and the operand, 1/1 and 2/1. Calculate the mediant ((1+2)/(1+1)) == 3/2. 3/2 squared is 9/4 and is higher than 2 so the original fraction is higher than the square root of 2. So you replace the high bound. The bounds are now 1/1 and 3/2. The new mediant is 4/3rds. 4/3 squared is 16 / 9, which is lower than 2, so the lower bound is replaced and they are now 4/3 and 3/2. Keep going until you are close enough.

    I like this one because I find it easier to understand and do than many of the alternatives. Yes, it often approaches worst case performance, but I find the ability to understand it, do it in my head quickly, and to start anywhere worth the trade off.

    • (Score: 1, Interesting) by Anonymous Coward on Thursday September 05 2024, @06:28AM

      by Anonymous Coward on Thursday September 05 2024, @06:28AM (#1371331)

      I should include an example code and calculation:

      Example python code:

      from fractions import Fraction

      def farey(test_func, lower=Fraction(0,1), upper=Fraction(1,1), max_iters=-1):
              iters = 0
              while iters != max_iters:
                      iters += 1
                      a, b = lower.numerator, upper.numerator
                      c, d = lower.denominator, upper.denominator
                      mediant = Fraction(a+b, c+d)
                      yield mediant
                      test_result = test_func(mediant)
                      if test_result == 0:
                              iters = max_iters
                      elif test_result < 0:
                              lower = mediant
                      elif test_result > 0:
                              upper = mediant
                      else:
                              ValueError("test_func must return a comparable number.")
              return mediant

      Example calculating square root of two:

      >>> test = lambda x: x * x - 2
      >>> generator = farey(test, Fraction(1,1), Fraction(2,1), 10)
      >>> for x in generator:
      ... print(x, float(x))
      ...
      3/2 1.5
      4/3 1.3333333333333333
      7/5 1.4
      10/7 1.4285714285714286
      17/12 1.4166666666666667
      24/17 1.411764705882353
      41/29 1.4137931034482758
      58/41 1.4146341463414633
      99/70 1.4142857142857144
      140/99 1.4141414141414141

      Example of calculating pi but without the 3 in front to make the math easier:

      >>> import math
      >>> test = lambda x: x + 3 - math.pi
      >>> generator = farey(test, max_iters=10)
      >>> for x in generator:
      ... print(x, float(x + 3))
      ...
      1/2 3.5
      1/3 3.3333333333333335
      1/4 3.25
      1/5 3.2
      1/6 3.1666666666666665
      1/7 3.142857142857143
      1/8 3.125
      2/15 3.1333333333333333
      3/22 3.1363636363636362
      4/29 3.1379310344827585
    • (Score: 3, Insightful) by DannyB on Thursday September 05 2024, @08:42PM (1 child)

      by DannyB (5839) Subscriber Badge on Thursday September 05 2024, @08:42PM (#1371445) Journal

      For those that don't know, that's not how successive approximation works. You are supposed to start with the general value and work your way towards the specific value.

      <no-sarcasm>
      Many decades ago, in the late 1970s, in high school, I figured this out with a calculator that did not have a square root operation. I came up with a guess of 1.5, squared it . . . too high. Tried 1.4 . . . too low. Tried 1.45, 1.44, etc.

      </no-sarcasm>

      In my old age I find that my new method of successive approximation is more amusing.

      --
      The Jupiter 2 is headed into a region of highly energized saran wrap.
      • (Score: 1, Funny) by Anonymous Coward on Thursday September 05 2024, @10:34PM

        by Anonymous Coward on Thursday September 05 2024, @10:34PM (#1371455)

        As do I. But in my old age, I find it harder to resist the urge to share information I find interesting. Stuck between two haystacks, this old ass chose his standby.