0

One of my friends asked me why simple equivalent formulas get different results. Excuse me that the code is a bit long but the interesting part is just line 99 and 100. Here is the formulas and the code. I checked the code. The problem is where f = 1. The problem is line 99 and 100. It takes about 10 s to run the code. Thanks for any guide or comment.

gariepy
  • 3,576
  • 6
  • 21
  • 34
Abolfazl
  • 453
  • 4
  • 8
  • 19

1 Answers1

1

Simple. It is just because of round off errors. What you have commented out has some round off errors which makes fplus~=1, so:

E = log( (1 - 1") / 1) >= log(eps) >= -36.0437. 

while the real value should be:

E = log( (1 - 1) / 1) = log(0) = -Inf
eulerleibniz
  • 271
  • 3
  • 11
  • So here the simplified form is better. Dose it matter `log` show imaginary part? Its also for rounding error. – Abolfazl Jun 12 '16 at 06:49
  • @Abolfazl in your code, both `fminus` and `fplus` are positive. so there is no imaginary part. And when calculating log, round-off errors only have effect on some special values, that is numbers approximately equal to zero or one(where you should use `log1p`). – eulerleibniz Jun 12 '16 at 13:29