|
run your code -> how many digits do you count?
|
|
|
|
|
I did - and checked they gave the same results before I posted it. You should never use standby on an elephant. It always crashes when you lift the ears. - Mark Wallace
C/C++ (I dont see a huge difference between them, and the 'benefits' of C++ are questionable, who needs inheritance when you have copy and paste) - fat_boy
|
|
|
|
|
lol - same results...
Check it:
using System;
namespace DecimalTest
{
class Program
{
static void Main(string[] args)
{
decimal divider;
divider = 10*10*10;
divider = Decimal.Multiply(divider,10*10*10*10*10);
divider = Decimal.Multiply(divider,10*10*10*10*10);
divider = Decimal.Multiply(divider,10*10*10*10*10);
Console.WriteLine(divider);
decimal tenE18 = 10;
for (int i = 0; i < 18; i++)
tenE18 *= 10;
Console.WriteLine(tenE18);
Console.WriteLine(10E18M);
Console.WriteLine(String.Format("{0:N}", Math.Pow(10d, 18d)));
Console.ReadKey();
}
}
}
who invented those nasty zero!?!
|
|
|
|
|
Yeah, I checked my results matched!
I didn't check his!
Oops.You should never use standby on an elephant. It always crashes when you lift the ears. - Mark Wallace
C/C++ (I dont see a huge difference between them, and the 'benefits' of C++ are questionable, who needs inheritance when you have copy and paste) - fat_boy
|
|
|
|
|
yea - it was even hard for me to check his crazy code - lol. There is some resistence to execute such a thing -
|
|
|
|
|
|
chevu wrote: This one is correct code
wrong again:
1. the result for pow=18 is wrong.
2. pow=5 and pow=6 give the same result???
I think you have abundantly proven now that your code has high debugging complexity.
|
|
|
|
|
sorry dude... i had really forget to check till 18... coz of odd even cases that code will fail...
I knw you people are getting irritated by now, but you can check this code
double pow(long long a, long long b)
{
if(b == 0)
return 1.0;
else if(b == 1)
return a;
else if(b%2 == 0)
return pow(a*a,b/2);
else
return a* pow(a*a,b/2);
}
i have tested this code upto long long limits...
|
|
|
|
|
Recursive functions brings color in life I have no smart signature yet...
|
|
|
|
|
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...
|
|
|
|