You are viewing a single comment's thread from:

RE: C++ program of (a) power (b) without using builtin function

in #programming7 years ago

Sorry but your code and examples have errors.

First result variable is uninitialised and undefined in first example.

0**0 is undefined in maths.

O**-2 = 0

And your code doesn't check for overflows.

Sort:  

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:

  • you agree that 0^0 = 1 is fine (so why you mentioned it?) — as readers should be able to infer from the fact that the result agrees with the one given by pow();
  • there's an obscure O^-2 = 0 which is nonsense;
  • and at last there's the “inefficient” overflow request, as if it would add value to the code: no, it wouldn't, for at least two reasons: overflow is already accounted for someway with the special Inf value, and then anyway there's no way to signal the overflow, except by throwing an exception (in C++ — but guess what cmath's pow() does…); bonus third reason, the behavior is consistent with pow().

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

  • There's a post which has 9 fat votes, (for a little bit more than 9$ right now).
  • It pretends to show how to compute ab without builtin functions,
  • but it doesn't: it implements a wrong algorithm (considered as a whole), and moreover
  • it can be deduced that also it is a wrong implementation of that algorithm (41 = 16 and in general a1 = a² is a bug of what it is otherwise a correct algorithm for positive exponents, and 100-1 = 100-2 = 100-3 = 0.100 is a bug of the algorithm used for negative exponents).
  • Bonus 1, there are hopefully fake comments like “amazing post”, “its really a great logic in this program” and now also “good work bro...”… Even though fake, students stumbling on this post could naively believe they are informed comments and that it is a good “logic” algorithm for ab;
  • bonus 2, poor formatting, poor style, very rookie programming anyway.

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.

I thought I was pretty clear about my intention in my previous post but clearly you took offense.

I never sought to defend the post that you were correcting. If anything I said that was a good thing.

But clearly I thought that if you are going to provide corrections you need to be accurate.
I pointed at a mistake in your code. No need to be so sensitive. We all make mistakes.

As for the overflow I don't pretend to know the most efficient way to check for it but if your intention was to show an example implementation of pow, a quick check on the man page would show you that the library call would set errno to ERANGE in this scenario.

Anyway I certainly don't carry any hard feeling and this will be my last comment in this matter.

Wishing you all the best.

Loading...