1

I am implementing the Mandelbrot function both in Python and Julia; however, after the 6th iteration, the codes yield different results. What can be the main reason?

Here is the code in Python:

def mandelbrot(a):
    z = 0
    for i in range(50):
        z = z**2 + a
    return z

and, here is the same code in Julia:

function mandelbrot(a)
    z = 0
    for i=1:50
        z = z^2 + a
    end
    return z
end
  • 2
    Could you add an example of the ways in which the result is different? Absent other information, slightly different floating point rounding would be my first guess. – cbk Jul 08 '21 at 02:13
  • 2
    You could try the julia code with z initialized to big"0.0" and see if the results change. – Bill Jul 08 '21 at 02:49
  • Starting with 0.1, I get the exact same sequence. – phipsgabler Jul 08 '21 at 08:02
  • what `different results`? You should show these results. Don't expect that we will run code to see it. – furas Jul 08 '21 at 12:30
  • 1
    It is likely integer overflow. Do they differ if a is a float? – jverzani Jul 08 '21 at 12:36

1 Answers1

6

This does not seem to be reproducible for points within the Mandelbrot set:

Python 3.7.4 (default, Aug 13 2019, 15:17:50)
[Clang 4.0.1 (tags/RELEASE_401/final)] :: Anaconda, Inc. on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> def mandelbrot(a):
...     z = 0
...     for i in range(50):
...         z = z**2 + a
...     return z
...
>>> mandelbrot(-0.75)
-0.40274177046812404
>>> mandelbrot(0.1)
0.11270166537925831
>>> mandelbrot(0.2)
0.2763932022500072
               _
   _       _ _(_)_     |  Documentation: https://docs.julialang.org
  (_)     | (_) (_)    |
   _ _   _| |_  __ _   |  Type "?" for help, "]?" for Pkg help.
  | | | | | | |/ _` |  |
  | | |_| | | | (_| |  |  Version 1.6.1 (2021-04-23)
 _/ |\__'_|_|_|\__'_|  |  Official https://julialang.org/ release
|__/                   |

julia> function mandelbrot(a)
           z = 0
           for i=1:50
               z = z^2 + a
           end
           return z
       end
mandelbrot (generic function with 1 method)

julia> mandelbrot(-0.75)
-0.40274177046812404

julia> mandelbrot(0.1)
0.11270166537925831

julia> mandelbrot(0.2)
0.2763932022500072

The only difference I have been able to observe thus far is that for points outside the set, Python may return an OverflowError where Julia returns Inf.

>>> mandelbrot(0.3)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<stdin>", line 4, in mandelbrot
OverflowError: (34, 'Result too large')
julia> mandelbrot(0.3)
Inf

presumably due to the two languages making somewhat different decisions as regards handling floating point special values such as Inf.

In particular, Python by default does not appear to follow the standard rules for IEEE 754 floating point arithmetic, wherein the following should return + and - Inf, respectively (c.f. IEEE 754, division by zero)

>>> 1.0/+0.0
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ZeroDivisionError: float division by zero
>>> 1.0/-0.0
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ZeroDivisionError: float division by zero

whereas Julia does:

julia> 1.0/+0.0
Inf
julia> 1.0/-0.0
-Inf
cbk
  • 4,225
  • 6
  • 27