Thomas comments on Automatic programming, an example - Less Wrong

12 Post author: Thomas 01 February 2012 08:55PM

You are viewing a comment permalink. View the original post to see all comments and the full post content.

Comments (32)

You are viewing a single comment's thread. Show more comments above.

Comment author: wmorgan 01 February 2012 10:10:13PM *  9 points [-]

The generated code is bizarre. I refactored it as well as I could, and it still doesn't make much sense:

aphelion = (aphelion + perihelion) >> 10;
aphelion = aphelion - (aphelion / 12);
guess = ( ( (aphelion | 12) * (int)sqrt(aphelion) ) ^ 12 ) / 12;

"To get the orbit time in days from the aphelion and perihelion in Kkm, first sum them and divide by 1024. Then from that, subtract one twelfth. Then, to the value, perform a bitwise OR with 0x0C, multiply by the square root, and bit-XOR 0x0C again. Finally, divide by 12, and that will give you the number of days."

Comment author: Thomas 02 February 2012 07:15:19AM *  2 points [-]

divide by 1024

Actually by 4096. And it is a rescaling as jimrandomh points out.

Comment author: FeepingCreature 02 February 2012 11:33:06AM 4 points [-]

Am I crazy? A right shift by 10 is equivalent to a division by 2^10. 2^10 is 1024..

Comment author: Thomas 02 February 2012 05:28:46PM *  2 points [-]

No. The posted code has a bit shift right for 12 places. The already optimized code by wmorgan has a bit shift for only 10 bits.

The metacommand $RESCOM if while val_operation inc_dec caused this. Having two constants (10 and 12) would be undesirable be cause of this "val_operation" and therefore only the constant 12 was used.

Comment author: wmorgan 02 February 2012 07:18:41PM 1 point [-]

This is the generated code segment:

aphelion=aphelion+aphelion;
aphelion=aphelion+aphelion;
guess=12;
aphelion=aphelion>>guess;

Those four lines together amount to a shift 10 bits to the right, i.e., division by 1024.

I think you understand what's going in the code. The point of my refactoring was to make something that was human-readable: something that I could describe in English. And the English for those four lines of code is "divide by 1024." That's what those four lines do.

Comment author: jimrandomh 02 February 2012 07:13:33PM 1 point [-]

The extra two places of bit shifting cancel with two previous self-additions.

Comment author: Thomas 02 February 2012 07:29:21PM 1 point [-]

I know and I agree with this.

Then you have two different constants (10 and 12). One for the shifting and another for the division. It's nothing wrong with that, but the simulator was prevented to have more constants then absolutely necessary. So everything was done with the "12" and I was discussing that.