Loading ...
Sorry, an error occurred while loading the content.

syntax thought about float_of_int

Expand Messages
  • Rahul Siddharthan
    OK after using ocaml for a few weeks, it feels like the only possible language for me already: I ve already written one non-trivial program in a few days that
    Message 1 of 16 , Jun 27, 2004
    • 0 Attachment
      OK after using ocaml for a few weeks, it feels like the only possible
      language for me already: I've already written one non-trivial program
      in a few days that would have taken me weeks in C and been dead-slow
      in Python for example. So everything's great... with one exception:
      numerical coding.

      The problem is the innumerable float_of_int's you have to sprinkle
      everywhere when mixing ints and floats. This makes the code ugly and
      hard to read, especially if someone else needs to read it later.

      Doing things like
      let nf = float_of_int n in
      exp (nf *. x)
      isn't that much better if you have lots of such ints all over.

      Has anyone got a solution? I was thinking the best syntactic solution
      would be to have a postfix dot convert an int expression to a float
      (eg, n. becomes (float_of_int n) ; (n+m). becomes (float_of_int (n+m))
      etc) just as it already does for operators. Are there any obvious
      disadvantages to it? If not, can this be done via camlp4 or
      something? (I haven't played with camlp4 yet.)

      Rahul
    • Richard Jones
      ... Did you know that you can write float_of_int as just float ? You could always define your own synonym for float_of_int/float, eg: let fl = float;; let
      Message 2 of 16 , Jun 27, 2004
      • 0 Attachment
        On Sun, Jun 27, 2004 at 04:29:38PM -0400, Rahul Siddharthan wrote:
        > The problem is the innumerable float_of_int's you have to sprinkle
        > everywhere when mixing ints and floats. This makes the code ugly and
        > hard to read, especially if someone else needs to read it later.
        >
        > Doing things like
        > let nf = float_of_int n in
        > exp (nf *. x)
        > isn't that much better if you have lots of such ints all over.
        >
        > Has anyone got a solution? I was thinking the best syntactic solution
        > would be to have a postfix dot convert an int expression to a float
        > (eg, n. becomes (float_of_int n) ; (n+m). becomes (float_of_int (n+m))
        > etc) just as it already does for operators. Are there any obvious
        > disadvantages to it? If not, can this be done via camlp4 or
        > something? (I haven't played with camlp4 yet.)

        Did you know that you can write 'float_of_int' as just 'float'?

        You could always define your own synonym for float_of_int/float, eg:

        let fl = float;;
        let n, m = 3, 2;;
        fl n +. fl m;;

        Rich.

        --
        Richard Jones. http://www.annexia.org/ http://www.j-london.com/
        Merjis Ltd. http://www.merjis.com/ - improving website return on investment
        MONOLITH is an advanced framework for writing web applications in C, easier
        than using Perl & Java, much faster and smaller, reusable widget-based arch,
        database-backed, discussion, chat, calendaring:
        http://www.annexia.org/freeware/monolith/
      • Rahul Siddharthan
        ... Didn t know, thanks, but ... I don t think it s much better.... ie, it s ok in simple expressions, but messes up anything complicated R
        Message 3 of 16 , Jun 27, 2004
        • 0 Attachment
          Richard Jones said on Jun 27, 2004 at 21:55:07:
          > Did you know that you can write 'float_of_int' as just 'float'?

          Didn't know, thanks, but

          > You could always define your own synonym for float_of_int/float, eg:
          >
          > let fl = float;;
          > let n, m = 3, 2;;
          > fl n +. fl m;;

          I don't think it's much better.... ie, it's ok in simple expressions,
          but messes up anything complicated

          R
        • Brian Hurt
          ... You might try defining an operator, like: let ( ~~ ) = float_of_int;; This allows you to convert the above to: exp ((~~n) *. x) That being said, if you re
          Message 4 of 16 , Jun 27, 2004
          • 0 Attachment
            On Sun, 27 Jun 2004, Rahul Siddharthan wrote:

            > let nf = float_of_int n in
            > exp (nf *. x)

            You might try defining an operator, like:

            let ( ~~ ) = float_of_int;;

            This allows you to convert the above to:
            exp ((~~n) *. x)

            That being said, if you're doing a lot of converting ints to floats, this
            is likely an indication you're doing something wrong. On most processors,
            converting an int to a float requires at the very least writting it out to
            memory (as an int) and reading it back in again (as a float). Often it's
            even slower than that. Most numerical programs don't convert back and
            forth between ints and floats at all, so it's not something the chip
            makers optimize for.

            You'll probably be better off leaving the values as floats all the time.
            What are you trying to do?

            --
            "Usenet is like a herd of performing elephants with diarrhea -- massive,
            difficult to redirect, awe-inspiring, entertaining, and a source of
            mind-boggling amounts of excrement when you least expect it."
            - Gene Spafford
            Brian
          • Rahul Siddharthan
            ... That s similar to Richard s let fn = float_of_int suggestion, I guess it s good enough for most things. ... Hm, that s true. What I was trying to do
            Message 5 of 16 , Jun 27, 2004
            • 0 Attachment
              Brian Hurt said on Jun 27, 2004 at 17:27:41:
              > > let nf = float_of_int n in
              > > exp (nf *. x)
              >
              > You might try defining an operator, like:
              >
              > let ( ~~ ) = float_of_int;;
              >
              > This allows you to convert the above to:
              > exp ((~~n) *. x)

              That's similar to Richard's let fn = float_of_int suggestion, I guess
              it's good enough for most things.

              > is likely an indication you're doing something wrong. On most processors,
              > converting an int to a float requires at the very least writting it out to
              > memory (as an int) and reading it back in again (as a float). Often it's
              > even slower than that. Most numerical programs don't convert back and
              > forth between ints and floats at all, so it's not something the chip
              > makers optimize for.
              >
              > You'll probably be better off leaving the values as floats all the time.
              > What are you trying to do?

              Hm, that's true. What I was trying to do yesterday was evaluate
              f(x,n) (say exp(nx) or something more complicated) for different
              values of n, integrated from x = 0 to 1, and store them in an array
              indexed by n. With a float n, I can't use for loops but I can still
              do while loops or recursion (except that doing comparisons on floats
              is error-prone); also I can't use it as an index for an array.

              Rahul
            • promiller
              ... I dont have a general solution for your problem but give something like this a thought: let (+:) a b = float_of_int a *. b It allows you to write exp (n +:
              Message 6 of 16 , Jun 27, 2004
              • 0 Attachment
                > Rahul Siddharthan wrote:
                > [...]
                > let nf = float_of_int n in
                > exp (nf *. x)
                > [...]

                I dont have a general solution for your problem but give something like this
                a thought:

                let (+:) a b = float_of_int a *. b

                It allows you to write
                exp (n +: x)
                Of course it wont allow x +: n (which is rather ugly in this context).

                Another "solution" (more some kind of hack) would be to write a function
                with 4 optional parameters (2 int, 2 float).
                I'm thinking about something like
                let add ?(a=0) ?(b=0) ?(c=0.) ?(d=0.) = float_of_int a +. float_of_int b +.
                c +. d;;

                But afaik optional parameters and infix operators can not be combined.
                (You'd have to use
                exp(add n x)

                Sebastian
              • Rahul Siddharthan
                ... It s a thought, but not very general-purpose. Also doesn t work with more than two terms (n *: m *: f). Still, could be useful... ... Well, even that
                Message 7 of 16 , Jun 27, 2004
                • 0 Attachment
                  promiller said on Jun 28, 2004 at 00:53:52:
                  > > Rahul Siddharthan wrote:
                  > > [...]
                  > > let nf = float_of_int n in
                  > > exp (nf *. x)
                  > > [...]
                  >
                  > I dont have a general solution for your problem but give something like this
                  > a thought:
                  >
                  > let (+:) a b = float_of_int a *. b
                  >
                  > It allows you to write
                  > exp (n +: x)
                  > Of course it wont allow x +: n (which is rather ugly in this context).

                  It's a thought, but not very general-purpose. Also doesn't work with
                  more than two terms (n *: m *: f). Still, could be useful...

                  > Another "solution" (more some kind of hack) would be to write a function
                  > with 4 optional parameters (2 int, 2 float).
                  > I'm thinking about something like
                  > let add ?(a=0) ?(b=0) ?(c=0.) ?(d=0.) = float_of_int a +. float_of_int b +.
                  > c +. d;;
                  >
                  > But afaik optional parameters and infix operators can not be combined.
                  > (You'd have to use
                  > exp(add n x)

                  Well, even that doesn't work, you need to call "add" with labelled
                  parameters...

                  Rahul
                • Matthew O'Connor
                  ... One way to get around that is to intermix the different operators. Thus if n and m are ints and f and g are floats. Then these will work: n + m +: f n +
                  Message 8 of 16 , Jun 27, 2004
                  • 0 Attachment
                    >>I dont have a general solution for your problem but give something like this
                    >>a thought:
                    >>
                    >> let (+:) a b = float_of_int a *. b
                    >>
                    >>It allows you to write
                    >> exp (n +: x)
                    >>Of course it wont allow x +: n (which is rather ugly in this context).
                    >
                    >
                    > It's a thought, but not very general-purpose. Also doesn't work with
                    > more than two terms (n *: m *: f). Still, could be useful...

                    One way to get around that is to intermix the different operators. Thus
                    if n and m are ints and f and g are floats. Then these will work:

                    n + m +: f
                    n + m +: f +. g
                    n *: f +. (n + m) *: g
                    etc...

                    Hope that helps.


                    cheers,
                    matt.
                  • Stefano Zacchiroli
                    ... Wow, I didn t know that either. Has it always been there or is it a recent introduction in the Pervasives module? Thanks for the ocaml tip of the day :)
                    Message 9 of 16 , Jun 28, 2004
                    • 0 Attachment
                      On Sun, Jun 27, 2004 at 09:55:07PM +0100, Richard Jones wrote:
                      > Did you know that you can write 'float_of_int' as just 'float'?

                      Wow, I didn't know that either. Has it always been there or is it a
                      recent introduction in the Pervasives module?

                      Thanks for the "ocaml tip of the day" :)

                      --
                      Stefano Zacchiroli -*- Computer Science PhD student @ Uny Bologna, Italy
                      zack@{cs.unibo.it,debian.org,bononia.it} -%- http://www.bononia.it/zack/
                      If there's any real truth it's that the entire multidimensional infinity
                      of the Universe is almost certainly being run by a bunch of maniacs. -!-
                    • Richard Jones
                      ... No idea. I only discovered it because an SML programmer pointed it out to me. Perhaps it comes from SML? Rich. -- Richard Jones. http://www.annexia.org/
                      Message 10 of 16 , Jun 28, 2004
                      • 0 Attachment
                        On Mon, Jun 28, 2004 at 09:02:40AM +0200, Stefano Zacchiroli wrote:
                        > On Sun, Jun 27, 2004 at 09:55:07PM +0100, Richard Jones wrote:
                        > > Did you know that you can write 'float_of_int' as just 'float'?
                        >
                        > Wow, I didn't know that either. Has it always been there or is it a
                        > recent introduction in the Pervasives module?

                        No idea. I only discovered it because an SML programmer pointed it
                        out to me. Perhaps it comes from SML?

                        Rich.

                        --
                        Richard Jones. http://www.annexia.org/ http://www.j-london.com/
                        Merjis Ltd. http://www.merjis.com/ - improving website return on investment
                        Perl4Caml lets you use any Perl library in your type-safe Objective
                        CAML programs. http://www.merjis.com/developers/perl4caml/
                      • Richard Jones
                        ... Note that this is probably going to be very inefficient. Optional parameters are passed as None or Some x , so requiring much boxing and unboxing and
                        Message 11 of 16 , Jun 28, 2004
                        • 0 Attachment
                          On Mon, Jun 28, 2004 at 12:53:52AM +0200, promiller wrote:
                          > I'm thinking about something like
                          > let add ?(a=0) ?(b=0) ?(c=0.) ?(d=0.) = float_of_int a +. float_of_int b +.
                          > c +. d;;

                          Note that this is probably going to be very inefficient. Optional
                          parameters are passed as 'None' or 'Some x', so requiring much boxing
                          and unboxing and an implicit pattern match. (Unless ocamlopt can
                          inline the function and avoid this overhead??)

                          Rich.

                          --
                          Richard Jones. http://www.annexia.org/ http://www.j-london.com/
                          Merjis Ltd. http://www.merjis.com/ - improving website return on investment
                          'There is a joke about American engineers and French engineers. The
                          American team brings a prototype to the French team. The French team's
                          response is: "Well, it works fine in practice; but how will it hold up
                          in theory?"'
                        • Henri DF
                          ... are you sure they are passed as None or Some x, when a default is provided? ie why would, let f ?(x=0) y () = x + y need to use any int option
                          Message 12 of 16 , Jun 28, 2004
                          • 0 Attachment
                            > On Mon, Jun 28, 2004 at 12:53:52AM +0200, promiller wrote:
                            > > I'm thinking about something like
                            > > let add ?(a=0) ?(b=0) ?(c=0.) ?(d=0.) = float_of_int a +. float_of_int b +.
                            > > c +. d;;
                            >
                            > Note that this is probably going to be very inefficient. Optional
                            > parameters are passed as 'None' or 'Some x', so requiring much boxing
                            > and unboxing and an implicit pattern match. (Unless ocamlopt can
                            > inline the function and avoid this overhead??)
                            >
                            are you sure they are passed as None or Some x, when a default is
                            provided?

                            ie why would,

                            let f ?(x=0) y () = x + y

                            need to use any 'int option' representation anywhere?

                            henri
                          • Richard Jones
                            ... The OCaml type system cannot represent values (ie. 0). The type of this expression is: # let f ?(x=0) y () = x + y;; val f : ?x:int - int - unit - int
                            Message 13 of 16 , Jun 28, 2004
                            • 0 Attachment
                              On Mon, Jun 28, 2004 at 04:26:24PM +0200, Henri DF wrote:
                              > are you sure they are passed as None or Some x, when a default is
                              > provided?
                              >
                              > ie why would,
                              >
                              > let f ?(x=0) y () = x + y
                              >
                              > need to use any 'int option' representation anywhere?

                              The OCaml type system cannot represent values (ie. 0). The type
                              of this expression is:

                              # let f ?(x=0) y () = x + y;;
                              val f : ?x:int -> int -> unit -> int = <fun>

                              (note the 0 literal disappears in the type definition).

                              Thus if all the information that the compiler has available is that
                              type definition, it must do boxing when calling f, and f must do
                              unboxing and a pattern match. (Only the function f "knows" about the
                              default value).

                              All bets are off when you've got sufficient information for ocamlopt
                              to inline the function. One would *hope* that it can get rid of the
                              unnecessary boxing, but I defer to someone who has more knowledge of
                              the internals.

                              Rich.

                              --
                              Richard Jones. http://www.annexia.org/ http://www.j-london.com/
                              Merjis Ltd. http://www.merjis.com/ - improving website return on investment
                              MOD_CAML lets you run type-safe Objective CAML programs inside the Apache
                              webserver. http://www.merjis.com/developers/mod_caml/
                            • Brian Hurt
                              ... Or keep a seperate index: let arr = Array.make 100 0. in let rec loop idx x = if idx
                              Message 14 of 16 , Jun 28, 2004
                              • 0 Attachment
                                On Sun, 27 Jun 2004, Rahul Siddharthan wrote:

                                > Hm, that's true. What I was trying to do yesterday was evaluate
                                > f(x,n) (say exp(nx) or something more complicated) for different
                                > values of n, integrated from x = 0 to 1, and store them in an array
                                > indexed by n. With a float n, I can't use for loops but I can still
                                > do while loops or recursion (except that doing comparisons on floats
                                > is error-prone); also I can't use it as an index for an array.

                                Or keep a seperate index:
                                let arr = Array.make 100 0. in
                                let rec loop idx x =
                                if idx < 100 then
                                begin
                                arr.(idx) <- f x;
                                loop (idx+1) (x +. 0.01)
                                end
                                else
                                ()
                                in
                                loop 0 0.

                                In my experience, for loops are useless for all but the simplest loops.
                                And I'd bet the above code is actually faster than the for loop version,
                                as the extra fp addition is almost certainly cheaper than converting an
                                integer to a floating point value.

                                --
                                "Usenet is like a herd of performing elephants with diarrhea -- massive,
                                difficult to redirect, awe-inspiring, entertaining, and a source of
                                mind-boggling amounts of excrement when you least expect it."
                                - Gene Spafford
                                Brian
                              • Henri DF
                                Hello, I have a bunch of logging calls which i would like to be able to remove from my code for performance purposes. These calls are typically in the form of
                                Message 15 of 16 , Jun 30, 2004
                                • 0 Attachment
                                  Hello,

                                  I have a bunch of logging calls which i would like to be able to
                                  remove from my code for performance purposes. These calls are typically in
                                  the form of a method call (using an inherited log class).

                                  Using camlp4 with pa_macro would be one option, but i really don't want to
                                  litter my code with ifdefs.

                                  Is it possible to do this type of thing with camlp4, and if so would
                                  anyone have pointers to examples of how to do this?

                                  thanks
                                  henri
                                • james woodyatt
                                  ... The approach I take to this is to put my log message generation inside [assert] expressions and use a method of sending log messages that always returns
                                  Message 16 of 16 , Jun 30, 2004
                                  • 0 Attachment
                                    On 30 Jun 2004, at 17:06, Henri DF wrote:
                                    >
                                    > I have a bunch of logging calls which i would like to be able to
                                    > remove from my code for performance purposes. These calls are
                                    > typically in
                                    > the form of a method call (using an inherited log class).
                                    >
                                    > Using camlp4 with pa_macro would be one option, but i really don't
                                    > want to
                                    > litter my code with ifdefs.
                                    >
                                    > Is it possible to do this type of thing with camlp4, and if so would
                                    > anyone have pointers to examples of how to do this?

                                    The approach I take to this is to put my log message generation inside
                                    [assert] expressions and use a method of sending log messages that
                                    always returns [true] as a result. When the -noassert flag is used,
                                    all this code is removed.


                                    --
                                    j h woodyatt <jhw@...>
                                    don't take any wooden root certs...
                                  Your message has been successfully submitted and would be delivered to recipients shortly.