-1
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?

Grismar
  • 27,561
  • 4
  • 31
  • 54
Bobby2153
  • 9
  • 1
  • 6
    Welcome to Stack Overflow. [Please don't post screenshots of text](https://meta.stackoverflow.com/a/285557/354577). They can't be searched or copied, or even consumed by users of adaptive technologies like screen readers. Instead, paste the code as text directly into your question. If you select it and click the `{}` button or Ctrl+K the code block will be indented by four spaces, which will cause it to be rendered as code. – ChrisGPT was on strike Feb 19 '22 at 23:05
  • 1
    Floating point numbers have limited precision. – Barmar Feb 19 '22 at 23:06
  • 1
    As long as you are typing it all out and want a string in the end, why not just put quotes around it instead of `str()`? – Mark Feb 19 '22 at 23:09
  • 1
    always put code, data and full error message as text (not screenshot, not link) in question (not in comment). It will be more readable and easier to use in answer, and more people will see it - so more people can help you. – furas Feb 19 '22 at 23:55
  • Related background: https://stackoverflow.com/q/588004/11082165 – Brian61354270 Feb 20 '22 at 00:07
  • Does this answer your question? [Is floating point math broken?](https://stackoverflow.com/questions/588004/is-floating-point-math-broken) – wovano Feb 21 '22 at 13:23

2 Answers2

1

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
Grismar
  • 27,561
  • 4
  • 31
  • 54
-2

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

Numerlor
  • 799
  • 1
  • 3
  • 16
  • 1
    To be precise, I wouldn't say "while the object is a float the data is still there" - Look at the result in the example you provided, the outputs match up to `459045` (the part Python prints when just printing the `float`), but after that, the entered value has `235..` while the printed value shows `090..`. The data isn't there at all, but if you force Python to print it, it will show what the rest of the value picked closest to e actually looks like. `e` does not have the data, the value of `e`, as provided, just the closest `float` that was available in the range of the data type. – Grismar Feb 20 '22 at 01:23