|
-
Nov 29th, 2003, 12:35 AM
#1
Thread Starter
Fanatic Member
"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.
-
Nov 29th, 2003, 06:45 AM
#2
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.
-
Nov 29th, 2003, 07:34 AM
#3
Addicted Member
-
Nov 29th, 2003, 07:53 AM
#4
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
-
Nov 29th, 2003, 08:22 AM
#5
Lively Member
You'll also find this useful:
Y = (N - Abs(N - 2*X)) / 2
-
Nov 29th, 2003, 08:23 AM
#6
Addicted Member
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
-
Nov 29th, 2003, 04:31 PM
#7
Hyperactive Member
in terms of vb
y = IIF(x>4,(9-4),x)
-
Nov 30th, 2003, 02:06 AM
#8
Thread Starter
Fanatic Member
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.
-
Dec 1st, 2003, 11:39 PM
#9
Hyperactive Member
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:
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|