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.
(Score: 3, Informative) by DannyB on Tuesday September 03 2024, @07:17PM (8 children)
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.
(Score: 1) by khallow on Wednesday September 04 2024, @01:09AM (3 children)
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)
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)
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
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)
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
I should include an example code and calculation:
Example python code:
Example calculating square root of two:
Example of calculating pi but without the 3 in front to make the math easier:
(Score: 3, Insightful) by DannyB on Thursday September 05 2024, @08:42PM (1 child)
<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
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.