0

I am trying to find a way to slice an arbitrary index of an irrational number, eg. getting a slice of 5 digits of the Euler's number starting at the decimal index 100. This is not a practical task, just a thing I was wondering about.

I was trying to find an answer here

and sliced my number after converting my float into string:

z=str(format(math.e, '.105f'))[-6:-1]

It does work with floats with smaller precision (till around .50f), but with bigger it returns zeros. I was wondering how could I possibly represent and slice long floats.

khleeloo
  • 1
  • 1
  • 1
    A number is a number, not a sequence of decimal digits and a period. Slicing or indexing a number does not make sense. Also, floats have limited precision. `math.e` represents the best possible `float` approximation of e, not the actual value. – user2357112 Jan 02 '21 at 15:29
  • `math`.e is 2.718281828459045, with 15 decimals digits, so there's no way you can find any digit of e further than that this way - even the last digit is only correct by chance, as the next digit is a 2, so it got rounded down. – Thierry Lathuille Jan 02 '21 at 15:34

1 Answers1

1

Python has a package bigfloat for this very high precision arithmetics.

Lior Cohen
  • 5,570
  • 2
  • 14
  • 30