I've renamed r to result on the fly, forgot some, fixed in the final source but forgot to fix it back in the text, sorry. Check the given hastebin, which I've compiled and run actually, so it's fine.
Then, don't forget that this is not meant to be ready-for-production code, but an straightforward implementation of a simple algorithm, to contrast with the post I talk about here.
0^0 is undefined in maths
It's quite disputed (see e.g. here or well, just wikipedia).
But anyway, the result is coherent with what pow()
does: haven't you read the test part, how it calls my_pow()
and checks the result with pow()
?
O^-2 = 0
What? Is it supposed to be a zero (0) or a (O)? What do you mean?
And your code doesn't check for overflows.
Think about how should it handle overflows, and ask yourself how the original pow()
does it and how the programmer calling the overflowing pow()
should notice. The double just stick to the special value Inf, and this again is coherent with the result of the standard pow(). Therefore I don't get your point.
I suppose you have a solid experience implementing correctly math functions in C/C++, so I look forward to read your suggestions about how to do the overflow checks and what to do inside the function once you spot the overflow.
Sorry but as you were correcting someone else's post (which is good) I thought that it was important to be accurate in yours.
You are right about the fact that 0**0 is controversial.
You could check for overflows condition in your loop:
if (a > (max_double / a))
But I agree that it's quite inneficient.
There might be some clever mathematical shortcut to check the above.
No, I'm no expert at math algorithm implementation. 😁
I accepted gladly the point about
r
/result
since it is a matter of fact. But I had doubts about your other remarks. Now you admit they aren't genuine but just childish aimless behavior.And yours is also just pointless and empty pedantry:
pow()
;Inf
value, and then anyway there's no way to signal the overflow, except by throwing an exception (in C++ — but guess whatcmath
'spow()
does…); bonus third reason, the behavior is consistent withpow()
.Now I must stress that I am not “correcting someone else's post” for a matter of accuracy, nor I've been in the post pointlessly pedant about something that it's good (but not just as good as I'd like it to be), or that it is just not bad.
No, the reasons for my post are different. Let me summarize few points here, though it's a waste of my time because your comment wasn't genuine but driven by an irrational kind of “spite”.
I sincerely believe that showing those problems can improve the quality of the contents on steemit, and that that kind of posts should be swept away, not implicitly defended trying to give me “the same medicine” — which is not, in case it isn't clear yet. They are “toxic” posts.
And I want to stress another point. Let us suppose it is a genuine post and not just a “fraud” to get money. I am not judging a poem, or an opinion: code can be objectively judged at least by one criterion, which is very simple: does the code do what you say it does?
If it doesn't, and it fails blatantly, not subtly in few special cases hard to get, how come you publish it trying to sell it as something that supposedly is teaching how to compute ab without builtin functions?
And moreover it takes votes… It's a scam, and it shouldn't be defended, if not for the money at least because there's a risk a novice takes the code seriously and steemit is marked as a bad place for code and programming.