Live data from Hacker News

Loop invariants can give you coding superpowers

yourbasic.org

51–60 of 88 posts

Re: Loop invariants can give you coding superpowers

#51
post #50
post #47

Earlier quoted context omitted.

Which floating point operator is not commutative? Addition is.

(small number)+(small number)+ ... +(small number)+(much bigger number) may not give the same result as (much bigger number)+(small number)+(small number)+ ... +(small number)

You are confusing associativity with commutativity. The IEEE floating point standard guarantees commutativity for addition.

Re: Loop invariants can give you coding superpowers

#52
post #47

Earlier quoted context omitted.

Which floating point operator is not commutative? Addition is.

Adding smaller magnitude first gives a different result than adding larger magnitudes first due to mixing precisions.

If I read you right, you are saying that if you compute a+b, you'll get a different result than if you compute b+a, if a is tiny and b is huge.

I believe this is incorrect. Feel free to show me a counterexample.

Re: Loop invariants can give you coding superpowers

#53
post #51
post #50

Earlier quoted context omitted.

(small number)+(small number)+ ... +(small number)+(much bigger number) may not give the same result as (much bigger number)+(small number)+(small number)+ ... +(small number)

You are confusing associativity with commutativity. The IEEE floating point standard guarantees commutativity for addition.

I think you are confusing commutativity in general with commutativity of binary operations.

Re: Loop invariants can give you coding superpowers

#54
post #28

To sum (haha) up, I am no n00b. However, I've looked over the page a couple times and still can't figure out the use of this, let alone what "superpowers" it may grant. It's just comments? The best parts I can see are the mostly-prose ones farther down that just say what the loop's supposed to do, but even those are just repeating the "input" and "output" info, really, which are sort of also comments I think. Some of…

I believe the superpower is the ability to prove correctness of the program easily.

For most of us, this isn't a big deal. But let's say you're writing a subroutine that will control part of a pacemaker, rocket, fighter jet, satellite- anything where bugs are deadly or expensive- then this is a Very Big Deal.

Re: Loop invariants can give you coding superpowers

#55
post #31

Earlier quoted context omitted.

Why is 'totalUpToPrevious' better than 'sum' or 'total'? I could see it being good for explaining what a reduce does, but once you know how they work, 'sum' tells you exactly which argument is the accumulator.

Because it underlines that it's the sum of all elements seen so far. It does not just say "yo, this one here is the accumulator", it also implicitly defines the meaning of "accumulator" by saying "and the code that follows will guarantee that it is always the sum of the elements that have been seen so far". It's almost like a spec for the body of the combining function. I recognize that in a codebase where every 10 l…

I wonder if totalSoFar would be a good tradeoff of precision for conciseness. It's how you're consistently explaining it in plain English, which is a powerful indicator it might be adequate.

I do normally call this sort of thing acc or accumulator though. I feel like an understanding of what that specific term means comes with the field expertise, sort of like we can say "LinkedList" and not "ObjectWithPointerToNextObject" and know exactly what that pattern should look like. A lot of CS skill comes down to expanding simple standard names.

That's not to say that your approach doesn't have value. It might just be that the toy example doesn't show it off very well. But if you were juggling that 3-way partition, for example, your naming scheme starts making a lot of sense for naming the boundary variables.

Re: Loop invariants can give you coding superpowers

#56
I think what many commenters are missing is why this is important.

Invariants like this allow one to prove the correctness of their code. That may not seem like a big deal most of the time, but imagine you're working on a satellite that's going to Jupiter, where a bug in the code could mean 10 years from now a billion dollar project gets scuttled. Imagine you're building subroutines that will go into 10,000 pacemakers next year. Imagine you're writing the navigation logic for a cruise missile.

There are many times where proving your code correct is taken very, very seriously. Invariants are a tool to do so more easily.

Re: Loop invariants can give you coding superpowers

#57
post #52

Earlier quoted context omitted.

Adding smaller magnitude first gives a different result than adding larger magnitudes first due to mixing precisions.

If I read you right, you are saying that if you compute a+b, you'll get a different result than if you compute b+a, if a is tiny and b is huge. I believe this is incorrect. Feel free to show me a counterexample.

a + b commutes all right, but some-start-value +a +b ... +x does not necessarily.

Re: Loop invariants can give you coding superpowers

#58
post #50
post #47

Earlier quoted context omitted.

Which floating point operator is not commutative? Addition is.

(small number)+(small number)+ ... +(small number)+(much bigger number) may not give the same result as (much bigger number)+(small number)+(small number)+ ... +(small number)

That is due to associativity. You're implicitly relying on a consistent left-to-right evaluation order, but the fundamental problem can be phrased as (using s for small and b for big):

   (((b + s) + s) + s) + s
Can be different to

    b + (s + (s + (s + s)))
This re-parenthesising is related to associativity, not commutivity.

Re: Loop invariants can give you coding superpowers

#59
post #52

Earlier quoted context omitted.

Adding smaller magnitude first gives a different result than adding larger magnitudes first due to mixing precisions.

If I read you right, you are saying that if you compute a+b, you'll get a different result than if you compute b+a, if a is tiny and b is huge. I believe this is incorrect. Feel free to show me a counterexample.

Not quite a+b, but a+b+c reordered may give different results[1]. Example from Python:

  >>> 0.1+0.2+0.3
  0.6000000000000001
  >>> 0.2+0.3+0.1
  0.6
It's an associativity problem and not a commutative one, however, that is relevant to the original point about folding floating point operations over lists.

[1] https://www.quora.com/Is-floating-point-addition-commutative...

Re: Loop invariants can give you coding superpowers

#60
post #22

Earlier quoted context omitted.

Not sure what you mean by your comment. You can use loop invariants when solving problems that call for an iterative solution.

“Can you write code to reverse a linked list in place?” “Sure, I’ll just implemment a loop according to this invariant ... “ Would be nice to have the invariant ready then.

Why not just keep the implementation of the solution to any question you might be asked in your pocket?
Post reply on HN