|
chevu wrote: long long a
If I were a canadian, thats how I would describe this thread.modified on Thursday, March 4, 2010 8:11 AM
|
|
|
|
|
|
Erm, a half-assed joke, you douche.
|
|
|
|
|
Here's how:
static decimal pow(decimal x, uint n)
{
decimal result = 1;
while (n > 0)
{
if ((n & 1) == 1)
{
result *= x;
}
n >>= 1;
if (n == 0)
break;
x *= x;
}
return result;
}
Good luck!
ok so it's not the best possible code, I just hacked it together, but it works (tested)modified on Friday, March 5, 2010 11:40 AM
|
|
|
|
|
harold aptroot wrote: (tested)
that is unacceptable. This is the Coding Horrors forum after all. You're expected to publish something that is completely wrong, yet claim it is correct.
|
|
|
|
|
Oops! Sorry
|
|
|
|
|
No problem. Anyway, it fails for negative n.
|
|
|
|
|
It's not supposed to work for negative n anyway
|
|
|
|
|
I didn't see any specs; you could have tested and thrown an InvalidArgumentException; or made the second parameter a uint.
[EDIT]Negative exponents result in divisions, which for integers tend to yield either 0 or 1 depending on the value of a.[/EDIT]
|
|
|
|
|
Is that really necessary..? Fortunately adding just 1 letter of code takes the problem away.
|
|
|
|
|
castling? sure, I do it every day.
|
|
|
|
|
Not sure a cast is the best approach here - decimal types are OK for currency and other situations where accuracy is critical.
Floating point types can introduce rounding errors - it all depends how the value is used.
Best solution I've seen in the comments is 1E18M, but really 1E19M may be better
|
|
|
|
|
Yes! I'm sorry I could only give you one five. When the problem domain calls for decimal calculations rather than floating point, casting from a floating point type would be a greater horror. Please do not read this signature.
|
|
|
|
|
Gets the offset of the last backslash of a path. Enjoy!!
int PathLen( LPCWSTR sWholePath)
{
CString sTmp(sWholePath),sTmp2;
int iPos=sTmp.Find('\\'),iStart=iPos+1;
while(1)
{
sTmp2=sTmp.Mid(iStart);
iPos=sTmp2.Find('\\');
if(iPos >= 0)
iStart+=(iPos+1);
else
break;
};
return iStart;
};
|
|
|
|
|
Bah! Go recursive!
|
|
|
|
|
I think you guy has read a book about algorithms and optimizing them – overzealous with a weird way of thinking.
“I go through the string from the first char through all the backslashes until there aren’t any”
instead of
“I go through the string from the last char until there is a backslash”
Or maybe he was just trying to make things harder for anyone else that would look at the code... I have no smart signature yet...
|
|
|
|
|
or he hasn't heard of the LastIndex string method O_o
|
|
|
|
|
LastIndex is only more efficient if you already know how long the string string is.
In C, that would have to scan the entire string and then back up, which is less efficient.
|
|
|
|
|
Ohhhh, I've known many developers that write code harder because they think it looks cool.
Unfortunately I've known WAY too many especially this fat pig that preached about how website development is very difficult; he then went on to develop a very difficult website.
Mike's code was soooooooo bad that they have to run to instances of the website on the same machine to properly test it....according to him. He was also complaining about how hard it was to test his code after his iterative code went more than 30 levels deep.
|
|
|
|
|
I was asked to check why they're query wasn't working, so I was going to add a column to their table. I started to copy/paste a line... and the parens are just one of the million issues with this "classic" asp code... lovely
Response.Write "<table width=1000 cellspacing=0 cellpadding=0 class=style1><tr>" _
& "<td width=40>" & (rsSearch("WKCTR") & "</td>" _
& "<td width=65>" & (rsSearch("START") & "</td>" _
& "<td width=65>" & (rsSearch("FINISH") & "</td>" _
& "<td width=60>" & (rsSearch("JOBNO") & "</td>" _
& "<td width=55>" & (rsSearch("ORDNO") & "</td>" _
& "<td width=35>" & (rsSearch("OPSEQ") & "</td>" _
& "<td width=55>" & (rsSearch("FITEM") & "</td>" _
& "<td width=215>" & (rsSearch("FDESC") & "</td>" _
& "<td width=45 class=style4>" & (rsSearch("REMAINING") & "</td>" _
& "<td width=45 class=style3>" & (rsSearch("TQCTD") & "</td>" _
& "<td width=45 class=style3>" & (rsSearch("SCRAP") & "</td>" _
& "<td width=45 class=style3>" & (rsSearch("ORTTL") & "</td>" _
& "<td width=45 class=style3>" & (Round(rsSearch("LABTIME")) & "</td>" _
& "<td width=45 class=style3>" & (Round(rsSearch("MACHTIME")) & "</td>" _
& "<td width=15 class=style3>" & (rsSearch("CONDS") & "</td>" _
& "<td width=45 class=style3>" & (rsSearch("CONQT") & "</td>" _
& "<td width=80>" & (rsSearch("OSTAT") & "</td>" _
& "</tr><br></table>")))))))))))))))))
|
|
|
|
|
I should note that it's printing each data-row in it's own table.
|
|
|
|
|
ColinBashBash wrote: I should note that it's printing each data-row in it's own table.
Not always a bad thing. If some items are overly long, they'll cause some rows of the table not to match others, but the entire table won't get pushed out by an overly-long item. Further, depending upon how many items there are, some browsers may display a number of short tables better than one really long one. I wouldn't consider the HTML table split horrible, even though the formatting of the source code is nasty.
|
|
|
|
|
Guys,
If you look careful, you will discover that the problem is not actually the HTML table, but the wrong usage of parenteses which make the code very dificult to understand.
In each line, the guy put a open parenteses to separate the function call, and not close it in time. So, at the end of code block, you have a lot of parenteses.
In time, the parenteses aren't need it this case and don't change the return of expression.
|
|
|
|
|
Personally I'm glad the parentheses are there because now I can clearly see the precedence of the & operators. Without the parentheses I would be totally lost and wouldn't be able to follow the code. I think this is an excellent example of self-documenting code.
|
|
|
|
|
The other users got this right of course...bad and incorrect use of parens but jeez dont you just hate this style of ASP coding! I havent done ASP is years now but isnt this so much easier to look at that than trash:
<table width="1000" cellspacing="0" cellpadding="0" class="style1">
<tr>
<td width="40"><% = rsSearch("WKCTR") %></td>
<td width="40"><% = rsSearch("START") %></td>
<td width="40"><% = rsSearch("FINISH") %></td>
<td width="40"><% = rsSearch("JOBNO") %></td>
</tr>
</table>
|
|
|
|