[Shootout-list] mandelbrot errors (perl, gforth, guile, ...)
Christophe TROESTLER
debian00 at tiscali.be
Sun Apr 23 22:56:49 UTC 2006
On Fri, 25 Feb 2005, Greg Buchholz <sleepingsquirrel at yahoo.com> wrote:
>
> I changed line 9 from "let niter = 50" to "let niter = 51" and I
> got a 1 pixel difference from the other program's output for the
> N=400 case (at pixel #300).
After investigation, it seems that the pixel #300 difference may be
due to a bug in the compiler (compiled to bytecode, the result should
be just fine). Attached is a new version that also solve the "niter"
value oddity.
> (could it be that Floats instead of Doubles are used, or something
> similar?)
No. OCaml uses doubles.
Regards,
ChriS
-------------- next part --------------
(* http://shootout.alioth.debian.org/benchmark.php?test=mandelbrot&lang=all *)
(* Plot the Mandelbrot set [-1.5-i,0.5+i] on an N-by-N bitmap
(http://www-info2.informatik.uni-wuerzburg.de/mitarbeiter/wolfram/lehre/bildformate.html#pbm). *)
let niter = 50
let limit = 2.
let limit2 = limit *. limit
let add_bit0 cr ci byte =
let rec loop i zr zi =
if zr *. zr +. zi *. zi > limit2 then (byte lsl 1) lor 0x00
else if i > niter then (byte lsl 1) lor 0x01
else loop (i + 1) (zr *. zr -. zi *. zi +. cr) (2. *. zr *. zi +. ci) in
loop 1 0. 0.
let () =
let w = int_of_string Sys.argv.(1) in
let h = w in
let invw = 2. /. float w
and invh = 2. /. float h
and cplmt8 = 8 - w mod 8 in
Printf.printf "P4\n%i %i\n" w h;
let byte = ref 0
and bit = ref 0 in
for y = 0 to h - 1 do
let ci = float y *. invh -. 1. in
for x = 0 to w - 1 do
byte := add_bit0 (float x *. invw -. 1.5) ci !byte;
incr bit;
if !bit = 8 then (
output_byte stdout !byte;
byte := 0;
bit := 0;
)
done;
if !bit <> 0 then (
output_byte stdout (!byte lsl cplmt8);
byte := 0;
bit := 0;
)
done
More information about the Shootout-list
mailing list