Results 1 to 9 of 9

Thread: "Middle" equation

  1. #1

    Thread Starter
    Fanatic Member WorkHorse's Avatar
    Join Date
    Jul 2002
    Location
    Where you live.
    Posts
    591

    "Middle" equation

    What equation would produce the following results:

    0 - > 0
    1 - > 1
    2 - > 2
    3 - > 3
    4 - > 4
    5 - > 4
    6 - > 3
    7 - > 2
    8 - > 1
    9 - > 0

    So for any n set of numbers, where n is always even, the result is that 0 and n-1 = 0; and (n/2) and ((n/2)-1) = (n-2)/2 and the numbers fill in in between? At least I think that is right. The idea is that the list on the left hand side is consecutive numbers and the list on the right hand side goes from 0 on up from both ends.

    If this possible with an equation, or can it only be done by code?

    Thanks for any help.

  2. #2
    Fanatic Member twanvl's Avatar
    Join Date
    Dec 2001
    Posts
    771
    I think this equation would do the trick:
    Code:
    f(x) := x + floor(2x/n)*(n-1-2x)
    Often results that seem to need an if can be rewriten using floor or abs.

  3. #3
    Addicted Member
    Join Date
    Apr 2003
    Posts
    170
    what us a "floor" function ??!

  4. #4
    Fanatic Member twanvl's Avatar
    Join Date
    Dec 2001
    Posts
    771
    floor means rounding a number down to the largest integer smaller then it. For example:
    floor(3) = 3
    floor(3.9) = 3
    floor(-3.1) = -4

    abs (I didn't use it in the equation, but I did mention it) means absolute value, if the argument is negative, returns it without the minus sign:
    abs(10) = 10
    abs(-10) = 10

  5. #5
    Lively Member Something Else's Avatar
    Join Date
    Nov 2003
    Location
    Where Humboldt Intersects Carlson
    Posts
    99
    You'll also find this useful:

    Y = (N - Abs(N - 2*X)) / 2



  6. #6
    Addicted Member
    Join Date
    Apr 2003
    Posts
    170
    well, just because most people here (i suppose) are
    vb programmers, i suggest you use,
    "int()" function its exactly like floor function, but thats
    how it is in vb.
    also another usefull function (not supported by vb,
    but some other basic stuff)
    is "frac", which is how much you are away from the "floor"
    ie.
    int(3.3)=3
    int(-3.3)=-4
    frac(3.3)=0.3
    frac(-3.3)=0.7

  7. #7
    Hyperactive Member sw_is_great's Avatar
    Join Date
    Nov 2003
    Posts
    330
    in terms of vb

    y = IIF(x>4,(9-4),x)
    Regards

  8. #8

    Thread Starter
    Fanatic Member WorkHorse's Avatar
    Join Date
    Jul 2002
    Location
    Where you live.
    Posts
    591
    Originally posted by Something Else
    You'll also find this useful:

    Y = (N - Abs(N - 2*X)) / 2


    Thanks! That's the one I was looking for.

    p/s: y = (n - Abs(n - (2 * x))) / 2 runs twice as fast as y = IIf(x > (((n + 1) / 2) - 1), (n - x), x)). Edit: Actually, the speed difference (at least on my PC) would be less than 1 second for 1 million iterations, so it is inconsequential. I just wanted clean coding and documentation, which in this case I feel is better with an equation than a VB function such as IIF. Just my preference I guess. Cheers, and thanks all.)

    Last edited by WorkHorse; Nov 30th, 2003 at 02:18 AM.

  9. #9
    Hyperactive Member
    Join Date
    Jul 2002
    Location
    WGTN, New Zealand
    Posts
    338
    I try to avoid the "conditional if function", in some cases it makes your code look nicer if you have small parameters (i.e. IIf(x < y, x, y)) but if you have large parameters it looks ugly:

    VB Code:
    1. strSaveFileName = IIf (MsgBox ("Save changes to data?", vbYesNo) = vbYes, strOpenFileName, "")

Posting Permissions

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



Click Here to Expand Forum to Full Width