Ceiling function

Here you can discuss and share functionality improvements and helper programs to make Context Free better.

Moderators: MtnViewJohn, chris, mtnviewmark

Post Reply
User avatar
pakin
Posts: 43
Joined: Sat Apr 21, 2007 8:59 pm
Location: United States
Contact:

Ceiling function

Post by pakin »

Hey, how about adding a ceiling function (or ceil, if you want to follow C's convention)? CFDG has floor, but no ceiling function.

I kind of need a ceiling at the moment so I guess I'll have to implement it in terms of floor for now.

Thanks,
— Scott

User avatar
MtnViewJohn
Site Admin
Posts: 882
Joined: Fri May 06, 2005 2:26 pm
Location: Mountain View, California
Contact:

Re: Ceiling function

Post by MtnViewJohn »

OK. I'm working on a minor release with an improved random number generator and the ability for paths to include themselves recursively (plus mutual recursion). Are there any other functions that may be useful?

User avatar
pakin
Posts: 43
Joined: Sat Apr 21, 2007 8:59 pm
Location: United States
Contact:

Re: Ceiling function

Post by pakin »

MtnViewJohn wrote:Are there any other functions that may be useful?
Hmm...I can't think of anything critical at the moment, although I'm sure a ton of ideas will come to mind as soon as you release the new version of the program. :wink:

One thing you might consider, though—and I seem to recall someone else requesting this a long time ago—is a function for converting RGB to HSB. While that would occasionally be convenient, it's not absolutely essential; I was able to code up the following conversion using CFDG's existing mechanisms:

Code: Select all

// Convert a vector of RGB colors (0..255, 0..255, 0..255) to HSB
// colors (-360..360, 0..1, 0..1).
vector3 rgb_to_hsb(vector3 rgb) =
  let(rd = rgb[0];
      gr = rgb[1];
      bl = rgb[2];
      let(minRGB = min(rd, gr, bl);
          maxRGB = max(rd, gr, bl);
          let(deltaRGB = maxRGB - minRGB;
              br = maxRGB;
              let(st = if(maxRGB,
                          255*deltaRGB/maxRGB,
                          0);
                  let(hu = if(st == 0,
                              0,
                              60 * if(rd == maxRGB,
                                      (gr - bl)/deltaRGB,
                                      if(gr == maxRGB,
                                         2 + (bl - rd)/deltaRGB,
                                         4 + (rd - gr)/deltaRGB)));
                      hu, st, br)))))
— Scott

User avatar
pakin
Posts: 43
Joined: Sat Apr 21, 2007 8:59 pm
Location: United States
Contact:

Re: Ceiling function

Post by pakin »

MtnViewJohn wrote:Are there any other functions that may be useful?
Here's another useful function but one that probably has to wait for a more major update to the language: a debug function that displays messages to stderr. Haskell's trace function (http://hackage.haskell.org/package/base ... ml#v:trace) may be a good model for this as it' can be used not just in CFDG rules but also within CFDG functions. The challenge, I believe, is that CFDG currently lacks a string datatype, which is why I won't complain if this idea gets put on the "wish list" heap for future consideration.

User avatar
MtnViewJohn
Site Admin
Posts: 882
Joined: Fri May 06, 2005 2:26 pm
Location: Mountain View, California
Contact:

Re: Ceiling function

Post by MtnViewJohn »

I just learned Haskell, so you have my attention. Mark says that trace is a terrible Haskell function that never executes when you expect it to because of the lazy execution model. Context Free has an eager execution model, but it may share some of the tracing issues that Haskell does because the eager execution model is out-of-order with biggest-before-smallest. The lack of strings as a data type is not a serious problem. String constants exist in the parser, so parsing string constants in a trace function or command would not be too hard. Maybe breakpoints too.

User avatar
pakin
Posts: 43
Joined: Sat Apr 21, 2007 8:59 pm
Location: United States
Contact:

Re: Ceiling function

Post by pakin »

MtnViewJohn wrote:I just learned Haskell, so you have my attention. Mark says that trace is a terrible Haskell function that never executes when you expect it to because of the lazy execution model.
Mark is probably referring to the way that trace executes only when the resulting value is required (because of the non-strictness) and only once per given set of arguments (because of the lazy implementation). That can certainly lead to surprising behavior, but it does generally make sense in retrospect after you ponder the missing or out-of-order trace output.

I was mostly proposing trace's type signature (String -> a -> a), which is compatible with user-defined functions in CFDG. That is, debug(string, value) would mean, "Output string then return value." Here's what that might look like:

Code: Select all

fib(n) =
    debug(concat("n is currently ", toString(n)),
          if(n <= 1, 
              n, 
              fib(n-1) + fib(n-2)
          )
    )
As that example indicates, concat and toString would greatly complement debug.
MtnViewJohn wrote:Context Free has an eager execution model, but it may share some of the tracing issues that Haskell does because the eager execution model is out-of-order with biggest-before-smallest.
Perhaps; it's hard to tell. My guess is that it will be a lot less weird than Haskell and just slightly weirder than a conventional programming language.
MtnViewJohn wrote:String constants exist in the parser, so parsing string constants in a trace function or command would not be too hard. Maybe breakpoints too.
That's good to hear.

I'm having trouble picturing how breakpoints might work, though. Are you thinking of flushing the output at each breakpoint so the user could examine what the picture looks like at various stages of the execution?

— Scott

rodrigosetti
Posts: 2
Joined: Wed Dec 24, 2014 1:52 pm

Re: Ceiling function

Post by rodrigosetti »

Some different random sources, like Perlin and Normal Distribution?

User avatar
MtnViewJohn
Site Admin
Posts: 882
Joined: Fri May 06, 2005 2:26 pm
Location: Mountain View, California
Contact:

Re: Ceiling function

Post by MtnViewJohn »

rodrigosetti wrote:Some different random sources, like Perlin and Normal Distribution?
I can easily produce all of the random distributions found in the C++11 random spec. A Perlin noise function would be a different matter. How would you use it? What kind of function would you need?

Post Reply