Discussion about math, puzzles, games and fun. Useful symbols: ÷ × ½ √ ∞ ≠ ≤ ≥ ≈ ⇒ ± ∈ Δ θ ∴ ∑ ∫ π -¹ ² ³ °

You are not logged in.

- Topics: Active | Unanswered

```
sqrt' :: Double -> Double -> Double
sqrt' x guess
| guessIsGoodEnough = guess
| otherwise = sqrt' x improvedGuess
where
-- Test the precision of the guess
precision = 1e-15
guessIsGoodEnough = abs (guess * guess - x) < precision * x
-- Improve our guessed square
improvedGuess = (guess + x / guess) / 2
main = putStrLn.show $ 1.0/((sqrt' 1234567899 1) + (sqrt' 1234567898 1))
```

`1.4230249486517707e-5`

'And fun? If maths is fun, then getting a tooth extraction is fun. A viral infection is fun. Rabies shots are fun.'

'God exists because Mathematics is consistent, and the devil exists because we cannot prove it'

I'm not crazy, my mother had me tested.

Offline

**bobbym****bumpkin**- From: Bumpkinland
- Registered: 2009-04-12
- Posts: 109,606

I am sorry, the question was a bit vague.

**In mathematics, you don't understand things. You just get used to them.****If it ain't broke, fix it until it is.**** Always satisfy the Prime Directive of getting the right answer above all else.**

Offline

What question?

'And fun? If maths is fun, then getting a tooth extraction is fun. A viral infection is fun. Rabies shots are fun.'

'God exists because Mathematics is consistent, and the devil exists because we cannot prove it'

I'm not crazy, my mother had me tested.

Offline

**bobbym****bumpkin**- From: Bumpkinland
- Registered: 2009-04-12
- Posts: 109,606

**In mathematics, you don't understand things. You just get used to them.****If it ain't broke, fix it until it is.**** Always satisfy the Prime Directive of getting the right answer above all else.**

Offline

What's wrong?

'And fun? If maths is fun, then getting a tooth extraction is fun. A viral infection is fun. Rabies shots are fun.'

'God exists because Mathematics is consistent, and the devil exists because we cannot prove it'

I'm not crazy, my mother had me tested.

Offline

**bobbym****bumpkin**- From: Bumpkinland
- Registered: 2009-04-12
- Posts: 109,606

Modern languages are constructed to defeat to some extent "subtractive cancellation." This does not mean that problem has gone away, it just means that people do not notice it.

The idea was to use a basic calculator on the problem so that you could see an important algebraic point about numerical stability.

**In mathematics, you don't understand things. You just get used to them.****If it ain't broke, fix it until it is.**** Always satisfy the Prime Directive of getting the right answer above all else.**

Offline

Modern languages are constructed to defeat to some extent "subtractive cancellation.

Oy! I used the conjugate surds method.

'God exists because Mathematics is consistent, and the devil exists because we cannot prove it'

I'm not crazy, my mother had me tested.

Offline

**bobbym****bumpkin**- From: Bumpkinland
- Registered: 2009-04-12
- Posts: 109,606

The point is to eliminate the subtraction because in floating point arithmetic the subtraction of two nearly equal numbers results in a loss of digits.

**In mathematics, you don't understand things. You just get used to them.****If it ain't broke, fix it until it is.**** Always satisfy the Prime Directive of getting the right answer above all else.**

Offline

BRB after dinner. Will take ~20 min

'God exists because Mathematics is consistent, and the devil exists because we cannot prove it'

I'm not crazy, my mother had me tested.

Offline

**bobbym****bumpkin**- From: Bumpkinland
- Registered: 2009-04-12
- Posts: 109,606

The answer is to 50 places:

The first method to eliminate the subtraction is to make use of the identity:

This form does not suffer from subtractive cancellation and is therefore numerically stable.

Another way is to use the workhorse of numerical analysis... the Taylor series.

If we take the first term of the Mclaurin series of

in terms of , we get

This too, does not suffer from subtractive cancellation and should be numerically stable.

**In mathematics, you don't understand things. You just get used to them.****If it ain't broke, fix it until it is.**** Always satisfy the Prime Directive of getting the right answer above all else.**

Offline

Did you read my code?

'God exists because Mathematics is consistent, and the devil exists because we cannot prove it'

I'm not crazy, my mother had me tested.

Offline

**bobbym****bumpkin**- From: Bumpkinland
- Registered: 2009-04-12
- Posts: 109,606

Please look at the answer in post #110. Looks like you used the first method.

**In mathematics, you don't understand things. You just get used to them.****If it ain't broke, fix it until it is.**** Always satisfy the Prime Directive of getting the right answer above all else.**

Offline

Part II works only if epsilon is small enough wrt x

'God exists because Mathematics is consistent, and the devil exists because we cannot prove it'

I'm not crazy, my mother had me tested.

Offline

**bobbym****bumpkin**- From: Bumpkinland
- Registered: 2009-04-12
- Posts: 109,606

It would always be small enough. If it were not small enough you would not have to do it!

**In mathematics, you don't understand things. You just get used to them.****If it ain't broke, fix it until it is.**** Always satisfy the Prime Directive of getting the right answer above all else.**

Offline

bobbym wrote:

Modern languages are constructed to defeat to some extent "subtractive cancellation." This does not mean that problem has gone away, it just means that people do not notice it.

The idea was to use a basic calculator on the problem so that you could see an important algebraic point about numerical stability.

I know. When I try to do the subtraction right away, most significant digits cancel out. Haskell is not immune to this. For example:

```
*Main> ((sqrt' 1234567899 1) - (sqrt' 1234567898 1))
1.4230252418201417e-5
```

'God exists because Mathematics is consistent, and the devil exists because we cannot prove it'

I'm not crazy, my mother had me tested.

Offline

**thickhead****Member**- Registered: 2016-04-16
- Posts: 1,086

I used the same method multiplying and dividing by conjugate but excel has limitation. In the second one I expanded (x+1)^(1/2) by taylor series and could get better result.

**{1}Vasudhaiva Kutumakam.{The whole Universe is a family.}(2)Yatra naaryasthu poojyanthe Ramanthe tatra Devataha{Gods rejoice at those places where ladies are respected.}**

Offline

**bobbym****bumpkin**- From: Bumpkinland
- Registered: 2009-04-12
- Posts: 109,606

@Agnishom Yes, lots of digits given but most of them are wrong!

@thickhead if you knew enough to use the Taylor series as well as the conjugate method then you either have had some experience in numerical work or you did some research.

Very good from both of you.

**In mathematics, you don't understand things. You just get used to them.****If it ain't broke, fix it until it is.**** Always satisfy the Prime Directive of getting the right answer above all else.**

Offline

Yep.

What does stability mean?

'God exists because Mathematics is consistent, and the devil exists because we cannot prove it'

I'm not crazy, my mother had me tested.

Offline

**bobbym****bumpkin**- From: Bumpkinland
- Registered: 2009-04-12
- Posts: 109,606

Stability used here means resistance to subtractive cancellation. There are other forms of error too, we are only looking at one.

The first point being made is that that to a computer, (a - b)^2 is not equal to a^2 - 2 a b + b^2.

**In mathematics, you don't understand things. You just get used to them.****If it ain't broke, fix it until it is.**** Always satisfy the Prime Directive of getting the right answer above all else.**

Offline

Why would (a-b)^2 = a^2 - b^2? That does not make sense to humans either

'God exists because Mathematics is consistent, and the devil exists because we cannot prove it'

I'm not crazy, my mother had me tested.

Offline

**bobbym****bumpkin**- From: Bumpkinland
- Registered: 2009-04-12
- Posts: 109,606

Oh boy, my typing is getting so bad I can not even copy and paste correctly!

I meant, to a computer (a-b)^2 is not equal to a^2 - 2 a b + b^2

**In mathematics, you don't understand things. You just get used to them.****If it ain't broke, fix it until it is.**** Always satisfy the Prime Directive of getting the right answer above all else.**

Offline

Hm, what is it equal to, then?

'God exists because Mathematics is consistent, and the devil exists because we cannot prove it'

I'm not crazy, my mother had me tested.

Offline

**bobbym****bumpkin**- From: Bumpkinland
- Registered: 2009-04-12
- Posts: 109,606

Here is how M sees it:

Change a to x and substitute b = 1.

Plot[(x - 1)^2 - (x^2 - 2 x + 1), {x, 0, 5}]

**In mathematics, you don't understand things. You just get used to them.****If it ain't broke, fix it until it is.**** Always satisfy the Prime Directive of getting the right answer above all else.**

Offline

**thickhead****Member**- Registered: 2016-04-16
- Posts: 1,086

Agnishom wrote:

Yep.

What does stability mean?

Stability means as iterations continue the value comes nearer and nearer to a point or you can say the difference between successive values goes on reducing. I used a term moderation factor in earlier postings but it is wrong, it is actually called damping factor.the numerical analysis is something similar to physical vibrations. if we use dampers physical system stabilizes. It is same here also.When you calculate new value from old value you need not blindly take new value for next iterations. you can blend old and new values by c*old value +(1-c)* new value where c is damping factor.0<c<1. If c is more, damping is more. In those cases where iterations slowly converge from one side without overshooting to the other side you can use negative damping. c<0 for fast convergence.

regarding experience I was professor in engineering college in engineering subjects but I had more fancy towards mathematics. I had taught "Finite Element methods" for mechanical engineering students. Possibly this should have been in the introduction.

**{1}Vasudhaiva Kutumakam.{The whole Universe is a family.}(2)Yatra naaryasthu poojyanthe Ramanthe tatra Devataha{Gods rejoice at those places where ladies are respected.}**

Offline

eww. What was that?

'God exists because Mathematics is consistent, and the devil exists because we cannot prove it'

I'm not crazy, my mother had me tested.

Offline