~Simple number problem

  • Thread starter William Stacey [MVP]
  • Start date
W

William Stacey [MVP]

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
 
G

Gabriel Magaña

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?
 
G

Gabriel Magaña

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...
 
W

William Stacey [MVP]

Thanks. It was for some sql thing I was doing so I needed an expression.

--
William Stacey [MVP]

| 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?
|
| | > 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]
| >
| >
| >
|
|
 
B

Bill Butler

Gabriel Magaña said:
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
 
G

Gabriel Magaña

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...
 
C

C.C. \(aka Me\)

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)
 
B

Bruce Wood

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...)
 
J

James Curran

Gabriel Magaña said:
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
 
W

William Stacey [MVP]

:) 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]

| 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...)
|
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top