Google Groups Home
Help | Sign in
Message from discussion Port my ray tracer
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
Matthew Fluet  
View profile
 More options 2 Jun 2005, 13:35
Newsgroups: comp.lang.functional
From: Matthew Fluet <mfl...@acm.org>
Date: Thu, 02 Jun 2005 08:35:12 -0400
Local: Thurs 2 Jun 2005 13:35
Subject: Re: Port my ray tracer

> It is interesting to note that the SML is 50% longer than the OCaml.

It may be 50% longer than the original 66-line OCaml program, but it
seems to be exactly the same size at the optimized OCaml program you posted.

> Here is the SML (for Mlton):

Just a couple of SML notes:

> fun real n = Real.fromInt n

A function "real" (of identical semantics) is available in the top-level
environment of the Basis Library, so there is no need to define your own
version.

> val delta = 0.00000001 (* FIXME: Where is mach eps in the std libs? *)

The Standard ML Basis Library does have Real.minNormalPos which
corresponds to OCaml's min_float.
You can compute OCaml's epsilon_float with
val epsilon = Real.nextAfter(1.0,2.0) - 1.0

> val infinity = 1.0 / 0.0 (* FIXME: Where is infinity? *)

val infinity = Real.posInf

> val pi = 4.0 * Real.Math.atan 1.0 (* FIXME: Where is pi? *)

val pi = Real.Math.pi

> (* 3D vector
>    SML typically uses tuples rather than records (as in the OCaml implementation). *)
> type vec = real * real * real

I don't know if it is typical, but records will have no impact on the
peformance of the code compiled under MLton (and presumably under other
SML compilers).

> (* SML allows operator precedences to be declared when defining infix operators. *)
> infix 2 *| fun s *| (x, y, z) = (s*x+0.0, s*y, s*z)
> infix 1 +| fun (x1, y1, z1) +| (x2, y2, z2) =
>                (x1+x2+0.0, y1+y2+0.0, z1+z2+0.0)
> infix 1 -| fun (x1, y1, z1) -| (x2, y2, z2) =
>                (x1-x2+0.0, y1-y2+0.0, z1-z2+0.0)
> fun dot (x1, y1, z1) (x2, y2, z2) = x1*x2 + y1*y2 + z1*z2+0.0

Rather than using "+0.0" to force the overloaded operators to resolve to
  Real.real, you could use a simple type annotation on the functions:

fun s *| (x, y, z) : vec = (s*x, s*y, s*z)

MLton doesn't actually perform constant folding on floating point
operations, so you are paying for some extra operations in the final
executable.


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message, you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.

Create a group - Google Groups - Google Home - Terms of Service - Privacy Policy
©2008 Google