471,317 Members | 2,590 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,317 software developers and data experts.

~Simple number problem

if int is between 0-29 return 0. If 30-59, return 30.
Can you do this with a one line math expression and no IF statements or bit
shifting? TIA

--
William Stacey [MVP]

Jan 28 '06 #1
12 1312
int i = (value / 30) * 30;

Jan 28 '06 #2
Math.Floor(30 / x)*30;

Which is much more expensive CPU-wise than

y = (x > 30 ? 30 : 0);

Is there a reason for these constraints (no if-then etc...) or is this just
intellectial exercise?

"William Stacey [MVP]" <wi************@gmail.com> wrote in message
news:uS**************@tk2msftngp13.phx.gbl...
if int is between 0-29 return 0. If 30-59, return 30.
Can you do this with a one line math expression and no IF statements or
bit
shifting? TIA

--
William Stacey [MVP]

Jan 28 '06 #3
> Math.Floor(30 / x)*30;
Which is much more expensive CPU-wise than
y = (x > 30 ? 30 : 0);


Actually I should shut up about this... The compiler team might have a
super-fast implementation of Floor() in the library (ie, cast to int) and
I'm just stating unsubstantiated ideas here...
Jan 28 '06 #4

"Gabriel Magaña" <no*****@no-spam.com> wrote in message
news:OG**************@TK2MSFTNGP14.phx.gbl...
Math.Floor(30 / x)*30;

Which is much more expensive CPU-wise than

y = (x > 30 ? 30 : 0);


Although he didn't explicitly rule out (?:) , it is sort of cheating....don't you think?

Bill
Jan 28 '06 #5
Thanks Bruce!

--
William Stacey [MVP]

"Bruce Wood" <br*******@canada.com> wrote in message
news:11**********************@g43g2000cwa.googlegr oups.com...
| int i = (value / 30) * 30;
|
Jan 28 '06 #6
Thanks. It was for some sql thing I was doing so I needed an expression.

--
William Stacey [MVP]

"Gabriel Magaña" <no*****@no-spam.com> wrote in message
news:OG**************@TK2MSFTNGP14.phx.gbl...
| Math.Floor(30 / x)*30;
|
| Which is much more expensive CPU-wise than
|
| y = (x > 30 ? 30 : 0);
|
| Is there a reason for these constraints (no if-then etc...) or is this
just
| intellectial exercise?
|
| "William Stacey [MVP]" <wi************@gmail.com> wrote in message
| news:uS**************@tk2msftngp13.phx.gbl...
| > if int is between 0-29 return 0. If 30-59, return 30.
| > Can you do this with a one line math expression and no IF statements or
| > bit
| > shifting? TIA
| >
| > --
| > William Stacey [MVP]
| >
| >
| >
|
|
Jan 28 '06 #7
> Although he didn't explicitly rule out (?:) , it is sort of
cheating....don't you think?


Yeh, it's really an if-then-else. I included it just for comparison's
sake...
Jan 28 '06 #8
Something like this?

int GetValue(int x)
{
return (x/30) * 30;
}

So:
x=0
(0/30)* 30 = 0

x=29
(29/30)*30 = 0

x=30
(30/30)*30 = 30

x=59
(59/30)*30 = 30 (need to round the 59/30 down or drop the decimal)
--
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Charles Cox
VC/VB/C# Developer
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

"William Stacey [MVP]" <wi************@gmail.com> wrote in message
news:uS**************@tk2msftngp13.phx.gbl...
if int is between 0-29 return 0. If 30-59, return 30.
Can you do this with a one line math expression and no IF statements or
bit
shifting? TIA

--
William Stacey [MVP]

Jan 28 '06 #9
You must be a young'un, William! That's an old, old programmer's trick
from back in the days before we had fancy stuff like Decimal.Round.

You kids don't know how good you have it these days. Back when I
started programming, we used to store six-digit dates to save a byte on
each one... (never dreaming of how much money that would make for all
of us twenty years on...)

Jan 30 '06 #10
However, your question leave open what number to return in the int is
outside the range [0-59]. And the suggestion offered here give differenet
results for those values.

--
Truth,
James Curran
[erstwhile VC++ MVP]

Home: www.noveltheory.com Work: www.njtheater.com
Blog: www.honestillusion.com Day Job: www.partsearch.com
"William Stacey [MVP]" <wi************@gmail.com> wrote in message
news:uS**************@tk2msftngp13.phx.gbl...
if int is between 0-29 return 0. If 30-59, return 30.
Can you do this with a one line math expression and no IF statements or bit shifting? TIA

--
William Stacey [MVP]

Jan 30 '06 #11
"Gabriel Magaña" <no*****@no-spam.com> wrote in message
news:OG**************@TK2MSFTNGP14.phx.gbl...
Math.Floor(30 / x)*30;

Which is much more expensive CPU-wise than


And pointless more expensive, as that would be calculated as:

(int) (Math.Floor( (double) ((int) ((int) 30 / (int) x)) ) * (double)
30)

In other words, after calculating precisionly the value we want (an integer
0 or 1), we then promote it to a double, merely to call Math.Floor on it,
even though the integer division has already accomplished what we need. We
then do a floating-point multiple, before casting the number back down to an
int.

--
Truth,
James Curran
[erstwhile VC++ MVP]

Home: www.noveltheory.com Work: www.njtheater.com
Blog: www.honestillusion.com Day Job: www.partsearch.com
Jan 30 '06 #12
:-) I wish! Old enouph to have been around when ATT System V was king.
After I saw it, I smacked my head - doh. Cheers Bruce.

--
William Stacey [MVP]

"Bruce Wood" <br*******@canada.com> wrote in message
news:11**********************@g14g2000cwa.googlegr oups.com...
| You must be a young'un, William! That's an old, old programmer's trick
| from back in the days before we had fancy stuff like Decimal.Round.
|
| You kids don't know how good you have it these days. Back when I
| started programming, we used to store six-digit dates to save a byte on
| each one... (never dreaming of how much money that would make for all
| of us twenty years on...)
|
Jan 30 '06 #13

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

17 posts views Thread by savesdeday | last post: by
7 posts views Thread by J. Hall | last post: by
5 posts views Thread by Rob Somers | last post: by
5 posts views Thread by awasthi.ashish | last post: by
18 posts views Thread by Bob Cummings | last post: by
2 posts views Thread by Vitali Gontsharuk | last post: by
1 post views Thread by Synapse | last post: by
reply views Thread by rosydwin | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.