## Re: [Jython-dev] complex calculation

 Re: [Jython-dev] complex calculation From: - 2001-03-27 08:37:30 ```[Pilho Kim] >I have meet Jython's wrong calculation about complex numbers. > >Check the results of the following example. > >Regards, >Kim > > > >""" > test.py >""" > >z = 2.0 + 1.0j > >for i in range(1, 101): > print "%s^%d = %s" % (z, i, z**i) > > > >[Result on Python 2.0] >(2+1j)^55 = (1.55453313831e+019+5.99152971656e+018j) > >[Result on Jython 2.0 or Jython 2.1 alpha 1] >(2+1j)^55 = (9223372036854775807+5991529716557069312j) I think I have tracked it down to PyComplex.toString(): public static String toString(double value) { if (value == Math.floor(value)) { return Long.toString((long)value); } else { return Double.toString(value); } } Changing that method to: public static String toString(double value) { if (value == Math.floor(value) && value < Long.MAX_VALUE && value > Long.MIN_VALUE) { return Long.toString((long)value); } else { return Double.toString(value); } } seems to work for the test program, but my understanding of the finer points of FP and complex numbers is zero. I hope somebody else with a better understanding of this can verify that it is the intended behaviour. regards, finn ```

 [Jython-dev] complex calculation From: Pilho Kim - 2001-03-26 01:33:55 ```Hi, Jython is a gool tool for me. But I have meet Jython's wrong calculation about complex numbers. Check the results of the following example. Regards, Kim ```
 [Jython-dev] complex calculation From: Pilho Kim - 2001-03-26 01:40:46 ```Hi, Sorry, I have missed a source and results in my previous posting. I have meet Jython's wrong calculation about complex numbers. Check the results of the following example. Regards, Kim """ test.py """ z = 2.0 + 1.0j for i in range(1, 101): print "%s^%d = %s" % (z, i, z**i) [Result on Python 2.0] .......................... .......................... .......................... (2+1j)^53 = (2.82408452063e+018-1.76826945532e+018j) (2+1j)^54 = (7.41643849657e+018-7.12454390007e+017j) (2+1j)^55 = (1.55453313831e+019+5.99152971656e+018j) (2+1j)^56 = (2.50991330497e+019+2.75283908163e+019j) (2+1j)^57 = (2.26698752832e+019+8.01559146823e+019j) (2+1j)^58 = (-3.48161641158e+019+1.82981704648e+020j) (2+1j)^59 = (-2.52614032879e+020+3.3114724518e+020j) (2+1j)^60 = (-8.36375310938e+020+4.0968045748e+020j) (2+1j)^61 = (-2.08243107936e+021-1.70143959786e+019j) (2+1j)^62 = (-4.14784776274e+021-2.11645987131e+021j) (2+1j)^63 = (-6.17923565416e+021-8.38076750536e+021j) (2+1j)^64 = (-3.97770380295e+021-2.29407706649e+022j) .......................... .......................... .......................... [Result on Jython 2.0 or Jython 2.1 alpha 1] .......................... .......................... .......................... (2+1j)^53 = (2824084520626955264-1768269455316917248j) (2+1j)^54 = (7416438496570826752-712454390006879744j) (2+1j)^55 = (9223372036854775807+5991529716557069312j) (2+1j)^56 = (9223372036854775807+9223372036854775807j) (2+1j)^57 = (9223372036854775807+9223372036854775807j) (2+1j)^58 = (-9223372036854775808+9223372036854775807j) (2+1j)^59 = (-9223372036854775808+9223372036854775807j) (2+1j)^60 = (-9223372036854775808+9223372036854775807j) (2+1j)^61 = (-9223372036854775808-9223372036854775807j) (2+1j)^62 = (-9223372036854775808-9223372036854775807j) (2+1j)^63 = (-9223372036854775808-9223372036854775807j) (2+1j)^64 = (-9223372036854775808-9223372036854775807j) .......................... .......................... .......................... ```
 Re: [Jython-dev] complex calculation From: - 2001-03-27 08:37:30 ```[Pilho Kim] >I have meet Jython's wrong calculation about complex numbers. > >Check the results of the following example. > >Regards, >Kim > > > >""" > test.py >""" > >z = 2.0 + 1.0j > >for i in range(1, 101): > print "%s^%d = %s" % (z, i, z**i) > > > >[Result on Python 2.0] >(2+1j)^55 = (1.55453313831e+019+5.99152971656e+018j) > >[Result on Jython 2.0 or Jython 2.1 alpha 1] >(2+1j)^55 = (9223372036854775807+5991529716557069312j) I think I have tracked it down to PyComplex.toString(): public static String toString(double value) { if (value == Math.floor(value)) { return Long.toString((long)value); } else { return Double.toString(value); } } Changing that method to: public static String toString(double value) { if (value == Math.floor(value) && value < Long.MAX_VALUE && value > Long.MIN_VALUE) { return Long.toString((long)value); } else { return Double.toString(value); } } seems to work for the test program, but my understanding of the finer points of FP and complex numbers is zero. I hope somebody else with a better understanding of this can verify that it is the intended behaviour. regards, finn ```
 RE: [Jython-dev] complex calculation From: Tim Peters - 2001-03-28 03:48:44 ```[Pilho Kim] > [Result on Python 2.0] > (2+1j)^55 = (1.55453313831e+019+5.99152971656e+018j) > > [Result on Jython 2.0 or Jython 2.1 alpha 1] > (2+1j)^55 = (9223372036854775807+5991529716557069312j) [Finn Bock] > I think I have tracked it down to PyComplex.toString(): > > public static String toString(double value) { > if (value == Math.floor(value)) { > return Long.toString((long)value); > } else { > return Double.toString(value); > } > } > > Changing that method to: > > public static String toString(double value) { > if (value == Math.floor(value) && > value < Long.MAX_VALUE && value > Long.MIN_VALUE) { > return Long.toString((long)value); > } else { > return Double.toString(value); > } > } > > seems to work for the test program, but my understanding of the finer > points of FP and complex numbers is zero. Take heart! Mine isn't, and I'm sure you've nailed it, except *perhaps* for wanting <= and >= instead of < and >. The original bare "value == Math.floor(value)" test is a common floating-point coding mistake, and across languages: the author always has in mind, for example, distinguishing 3.1 from "3.0 on the nose", but doesn't realize that, e.g., floor(1e300) == 1e300 too (every f.p. number with a "big enough" exponent is an exact integer!). no-wonder-they-call-it-"complex"-ly y'rs - tim ```