1

This is a followup question to a previous one I made.

I'm trying to compute the Harmonic series to very large terms, however when comparing to log(n)+γ I'm not getting the expected error.

I suspect the main problem is with the BigFloat julia type.

harmonic_bf = function(n::Int64)
    x=BigFloat(0)
    for i in n:-1:1
        x += BigFloat(1/i)
    end
    x
end

For example it is well known that the lower bound for the formula: H_n - log(n) - γ is 1/2/(n+1). However, this holds for n=10^7 then fails for n=10^8.

n=10^8
γ = big"0.57721566490153286060651209008240243104215933593992"
lower_bound(n) = 1/2/(n+1)

>>> harmonic_bf(n)-log(n)-γ > lower_bound(BigFloat(n))
false

It's driving me crazy, I can't seem to understand what is missing... BigFloat supossedly should get arithmetic precision problems out of the way, however it seems not to be the case.

Note: I tried with BigFloat with unset precision and with 256 bits of precision.

Adrian
  • 755
  • 9
  • 17
  • 1
    Side note: write `function harmonic_bf(n::Int) ... end` to define a function. What you are doing defines a variable holding a function object (and looks like R). – phipsgabler Nov 02 '18 at 20:52

1 Answers1

5

You have to make sure that you use BigFloat everywhere. First in your function (notice that BigFloat(1/n) is not the same as 1/BigFloat(i)):

function harmonic_bf(n::Int64)
    x=BigFloat(0)
    for i in n:-1:1
        x += 1/BigFloat(i)
    end
    x
end

and then in the test (notice BigFloat under log):

julia> harmonic_bf(n)-log(BigFloat(n))-γ > lower_bound(BigFloat(n))
true
Bogumił Kamiński
  • 66,844
  • 3
  • 80
  • 107
  • Wonderful! I will admit the cluelessness of not using BigFloat inside log, however I'm more interested in the difference between `BigFloat(1/i)` and `1/BigFloat(i)`. – Adrian Nov 02 '18 at 21:53
  • Check out `BigFloat(1/10^10)` vs `1/BigFloat(10^10)` to see the difference. – Bogumił Kamiński Nov 02 '18 at 21:59