3

I am having trouble working with high precision in julia. I am not able to set a precision of 240 decimal places. I am currently using the BigFloat package, it is the most suitable for me at the moment.

I have this code:

epsilon = big(10)^(-240)

a = BigFloat("1.0") + epsilon

println(a)

This is my epsilon: 1.000000000000000000000000000000000000000000000000000000000000000000000000000522e-240there is a small error, "522", but we will disregard it for now. the problem is that I get a = 1.0. I would like it to be a = 1+10^-240

obs. I need to perform several calculations with a high precision, of 250 decimal places, but I am having problems defining this high precision in julia.

Tomaz
  • 181
  • 4

1 Answers1

4

Use the setprecision function with base=10:

julia> setprecision(BigFloat, 250; base=10)
250

julia> epsilon = big(10)^(-240)
9.99999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999991631e-241

julia> a = BigFloat("1.0") + epsilon
1.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000100000000005
Bogumił Kamiński
  • 66,844
  • 3
  • 80
  • 107
  • I think doing something like 1//BigInt(10)^BigInt(exp) will be exact (unless floating points are preferred over an exact representation of rational numbers for some reason). – AKdemy Apr 28 '23 at 08:44
  • Yes, this will be exact, but I assumed OP wanted to use `BigFloat` because it will be faster than `Rational{BigInt}`. – Bogumił Kamiński Apr 28 '23 at 13:59