0

Hello all I am using python 2.7 32 on a windows 10 64 bit OS and I get some weird results.
When runnig the following code

from numpy import sin, pi

for i in range(6, 10):
    x = 10**-i
    print(sin(x*pi)/x/pi)
    print(sin(x*pi)/x/pi, i)

I get different results, though it is the same math

0.999999999998
(0.99999999999835498, 6)
1.0
(0.99999999999998357, 7)
1.0
(0.99999999999999967, 8)
1.0
(1.0, 9)

When I import print function from future I get the same results. Just curious why thouse this occurs?

Note: Though the print is different I get the same results if I compare them.

Dudi b
  • 240
  • 3
  • 12
  • I don't think that's a valid duplicate answer. The output should have been `0.999999999998355 0.999999999998355 6, 0.9999999999999836 0.9999999999999836 7, 0.9999999999999997 0.9999999999999997 8, 1.0 1.0 9`. Python 3 gives expected output – Equinox Oct 07 '19 at 06:54
  • @venky__ correct, even if i test in python 2.7 on windows i get your results... so which OS you are using ? – Fabian Oct 07 '19 at 07:03
  • It looks like `print()` prints floats differently depending on whether they're by themselves or part of a tuple. When it's printing just the float it rounds it to fewer decimal places. – Barmar Oct 07 '19 at 07:08
  • If you want precise control, use a format string so you can specify the number of digits. – Barmar Oct 07 '19 at 07:09
  • Thenks for all the comments, I am using a windows 10 64 bit os and a python 2.7 32 bit, The thing is I don't have any problems I just wanted to know why do I get different results though I do the same math problem – Dudi b Oct 07 '19 at 10:09

1 Answers1

1

You are printing floats with your first command, and tuples with your second command. These, when printed, are represented with different levels of accuracy.

You can simply verify it by specifying the accuracy yourself:

from numpy import sin, pi

for i in range(6, 10):
    x = 10**-i
    print("{0:.30f}".format(sin(x*pi)/x/pi))  # print with more accuracy
    print(sin(x*pi)/x/pi, i)

Output:

0.999999999998354982544412905554    # more accuracy and (if rounded) same as the tuple value
(0.999999999998355, 6)
0.999999999999983568699235547683
(0.9999999999999836, 7)
0.999999999999999666933092612453
(0.9999999999999997, 8)
1.000000000000000000000000000000
(1.0, 9)

@Bamar answered the same in a comment - this is just more "visible".

Patrick Artner
  • 50,409
  • 9
  • 43
  • 69
  • Thanks for the answer but I asked why does the print function print different values – Dudi b Oct 07 '19 at 10:13
  • @Dudib because there are default representations used when printing. binary data f.e. wii replace `0x13` values by printing `\n` - if you want a different representation then what is default - you need to specify it. Classes use `__str__` for a informal representation and `__repr__` for a unambigious representation. read more here: https://stackoverflow.com/questions/1436703/difference-between-str-and-repr - if you target more into "why is water wet?" kinf of curiosity questions - well - because. – Patrick Artner Oct 07 '19 at 11:16