Home > database >  Why does my program only print the first few characters of e rather than the whole number?
Why does my program only print the first few characters of e rather than the whole number?

Time:02-20

e = str(2.7182818284590452353602874713526624977572470936999595749669676277240766303535475945713821785251664274)
print(e)

Output:

2.718281828459045

Screenshots: here and here.

Why does the code only print out the first few characters of e instead of the whole string?

CodePudding user response:

A string str has characters, but a number (be it an int or a float) just has a value.

If you do this:

e_first_100 = '2.7182818284590452353602874713526624977572470936999595749669676277240766303535475945713821785251664274'
print(e_first_100)

You'll see all digits printed, because they are just characters in a string, it could have also been the first 100 characters from 'War and Peace' and you would not expect any of that to get lost either.

Since 'e' is not an integer value, you can't use int here, so you'll have to use float, but Python uses a finite number of bits to represent such a number, while there's an infinite number of real numbers. In fact there's an infinite number of values between any two real numbers. So a clever way has to be used to represent at least the ones you use most often, with a limited amount of precision.

You often don't notice the lack of precision, but try something like .1 .1 .1 == .3 in Python and you'll see that it can pop up in common situations.

Your computer already has a built-in way to represent these floating point numbers, using either 32 or 64 bits, although many languages (Python included) do offer additional ways of representing floats that aren't part of the way your computer works and allow a bit more precision. By default, Python uses these standard representations of real numbers.

So, if you then do this:

e1 = float(e_first_100)
print(e1)

e2 = 2.7182818284590452353602874713526624977572470936999595749669676277240766303535475945713821785251664274
print(e2)

Both result in a value that, when you print it, looks like:

2.718281828459045

Because that's the precision up to which the number is (more or less) accurately represented.

If you need to use e in a more precise manner, you can use Python's own representation:

from decimal import Decimal

e3 = Decimal(e_first_100)
print(e3)

That looks promising, but even Decimal only has limited precision, although it's better than standard floats:

print(e2 * 3)
print(e3 * Decimal(3))

The difference:

8.154845485377136
8.154845485377135706080862414

CodePudding user response:

To expand on Grismar's answer, you don't see the data because the default string representation of floats cuts off at that point as going further than that wouldn't be very useful, but while the object is a float the data is still there.

To get a string with the data, you could provide a fixed precision to some larger amount of digits, for example

In [2]: e = format(
   ...:     2.7182818284590452353602874713526624977572470936999595749669676277240766303535475945713821785251664274,
   ...:     ".50f",
   ...: )

In [3]: e
Out[3]: '2.71828182845904509079559829842764884233474731445312'

which gives us the first 50 digits, but this is of course not particularly useful with floats as the loss of precision picks up the further you go

  • Related