Results 1 to 10 of 10

Thread: mathematical recreations

  1. #1

    mathematical recreations

    I was reading an online book the other day, and came across a formula which gives the abs function (absolute value of a number) in terms of the max function (the larger of two numbers):

    Code:
    abs(x) = max(x, -x)
    So one step ahead I wondered if there was a formula which goes the other way: the max function in terms of the abs function. After much hair-pulling I managed to derive it:

    Code:
    max(x, y) = 0.5 * (x + y + abs(x-y))
    Not as fast as the way it's normally done in code (or in our brains, for that matter):

    Code:
    if x > y then Result := x else Result := y;
    But I thought it was pretty neat all the same. Moving on...

    This thread about tessellating spheres got me thinking, too. I wondered if the geosphere algorithm might produce a better sphere if one started from something somewhat more sphere-like than an octahedron. Enter the icosahedron, which has twenty faces, each face an equilateral triangle. But how do you draw an icosahedron?

    Here's a simple technique, illustrated below (taken from the aforementioned book):



    Initially, draw an appropriately oriented line on each face of a cube. Then join the ends of each line together in an appropriate manner. The only tricky bit is knowing how long the initial lines are supposed to be. To guarantee equilateral triangles, I found after further hair-pulling that on a cube with sides of length D, the length of these initial lines must be:

    Code:
    L = 0.5 * D * (sqrt(5) - 1)
    Or more simply:

    Code:
    L = D / G
    where G is the well-known golden ratio, which has applications in art, music, and Egyptian pyramids!

    Mathematical recreation is not a bad hobby if you don't mind baldness.
    [size=10px]"In science one tries to tell people, in such a way as to be understood by everyone, something that no one ever knew before. But in poetry, it's the exact opposite." -- Paul Dirac[/size]

  2. #2

    mathematical recreations

    Mathematical recreation is not a bad hobby if you don't mind baldness.
    Many of us probably wont need math for that

    Still, it's cool stuff nonetheless. Have a quick looksee in the math thread of GD.net and you'll find lots of interesting things. I think I have seen your first sample asked there some time ago, as well.

  3. #3

    mathematical recreations

    A branch-free max! Elegant. I'm sure it can be useful in the right context.
    ZGameEditor - Develop 64kb games for Windows.
    Thrust for Vectrex - ROM-file and 6809 source code.

  4. #4

    mathematical recreations

    I'm not sure if it's branch-free, because I don't think the abs function is branch-free. I suppose you could try to circumvent that by writing the abs function as:

    Code:
    abs(x) = sqrt(x*x)
    So our formula for the max function would become:

    Code:
    max(x, y) = 0.5*(x + y + sqrt((x-y)*(x-y)))
    But is the sqrt function branch-free? Even ignoring the compiler implementation, determining the square root of a number requires making a choice: do you choose the negative or positive root?
    [size=10px]"In science one tries to tell people, in such a way as to be understood by everyone, something that no one ever knew before. But in poetry, it's the exact opposite." -- Paul Dirac[/size]

  5. #5

    mathematical recreations

    Quote Originally Posted by cragwolf
    I'm not sure if it's branch-free, because I don't think the abs function is branch-free.
    Actually Abs(), Min(), and Max() functions can be branch-free using conditional moves, e.g.:

    Code:
    function Max(a, b: Integer): Integer;
    asm
     cmp   edx, eax
     cmovg eax, edx
    end;

  6. #6

    mathematical recreations

    I guess I don't know what branch-free code is.
    [size=10px]"In science one tries to tell people, in such a way as to be understood by everyone, something that no one ever knew before. But in poetry, it's the exact opposite." -- Paul Dirac[/size]

  7. #7

    mathematical recreations

    As far as I understand brachfree code, it is a piece of code which doesn't have to do a "decision" within...


    So the CPU doesn't have to do something like:

    IF xy THEN yz
    ELSE
    za;

    The max procedure for example I would expect to do a branch:

    IF a > b then result := A
    ELSE result := b;


    You see the branch?
    Lifepower shows, that this function can give the right answer without branching...
    <a href="http://www.greatgamesexperiment.com/game/Valgard/?utm_source=gge&amp;utm_medium=badge_game"><img border="0" alt="GGE" title="GGE" src="http://static.greatgamesexperiment.com/badge/game/valgard/gge400x56.png"></a>

  8. #8

    mathematical recreations

    I was thinking about the intel cpu "fabs" instruction for floating point abs, or simply clearing the sign bit for integers.
    Branching code is everywhere the code makes a jump based on a test operation.

    edit: yes Huehnerschaender explains it well.
    ZGameEditor - Develop 64kb games for Windows.
    Thrust for Vectrex - ROM-file and 6809 source code.

  9. #9

    mathematical recreations

    Well, if the processor knows the data type for the integer (be it 16, 32, 64, 128, etc) then really the processor should only be performing a bitmask:

    CLR Var, SignBitPosition

    No jump, break, or other instructions to manage, a single call to mask the absolute value quickly. Now for Max its another story, one might think that it would look something to this:

    CMP A, B
    JGT USET
    PUSH A
    JMP UEND
    USET:
    PUSH B
    UEND:

    But this would be very inefficient as well, I've used the same type of code in assembly for max's but typically you take into account why a max is being performed and what other hardware is in place. In fact an Add and Divide method as first stated may be faster then the jumps depending on how often witch version is true (remember a jump takes an extra cycle when you don't use immediates and if you have a compare as above) where the math would always take 2 the above code could take 3.

    Of course the above is not x86 assembly, though looking it may compile on an x86 assembler. I may also be really off base as all of my assembly these days is in 8 bit micros

  10. #10

    mathematical recreations

    Thanks, I think I get it now.
    [size=10px]&quot;In science one tries to tell people, in such a way as to be understood by everyone, something that no one ever knew before. But in poetry, it&#39;s the exact opposite.&quot; -- Paul Dirac[/size]

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •