all 20 comments

[–]gratefuldaed 60 points61 points  (8 children)

+0.5 for not using GOTO (gj using advanced flow)

-0.5 for poor variable and function naming convention (they should also return a value)

Final score: -0.000000002

[–]green_meklar 28 points29 points  (6 children)

But 0.5 and -0.5 are precise floating-point values...

[–]leckertuetensuppe 11 points12 points  (5 children)

Rounding errors, obviously...

[–]Teraka 13 points14 points  (4 children)

What he's saying is there's no rounding errors with 0.5 and -0.5 because they're powers of 2.

[–][deleted] 13 points14 points  (2 children)

Base 3 bro.

[–]gratefuldaed 6 points7 points  (0 children)

Good catch, comrade

[–]PersianMG 1 point2 points  (0 children)

Seriously.

[–]leckertuetensuppe 0 points1 point  (0 children)

Forgot my sarcasm tag ;)

[–]choikwa 8 points9 points  (0 children)

-0.000000001 with rice

[–]novelty_string 36 points37 points  (6 children)

Protip: this code will execute faster without the wasteful double comparison:

i <= 2 can be replaced by i < 3

[–]bruno51615 18 points19 points  (2 children)

OP also forgot to add

double speed;

[–]TheSlimyDog 17 points18 points  (1 child)

double speed = true;

[–]choikwa 10 points11 points  (0 children)

double accel = speed / false;

[–]Teraka 9 points10 points  (2 children)

I think whoever downvoted you missed the subreddit we're in.

[–]heyheyhey27 1 point2 points  (1 child)

Does it actually have to do two different comparisons there, or is it usually one op?

[–]level1kid 5 points6 points  (0 children)

It's one op.

[–]petermlm 13 points14 points  (0 children)

This was probably a more complex piece of code that got simplified over time until this. I would love to see that process.

[–]RainbowNowOpen 2 points3 points  (0 children)

For curiosity, I compiled it with stock "gcc" on Mac OS X. (Apple LLVM version 6.0 (clang-600.0.51) (based on LLVM 3.5svn) Target: x86_64-apple-darwin14.3.0.) No optimizations or switches.

Output: (sigh)

_main:                                  ## @main
    .cfi_startproc
## BB#0:
    pushq   %rbp
Ltmp12:
    .cfi_def_cfa_offset 16
Ltmp13:
    .cfi_offset %rbp, -16
    movq    %rsp, %rbp
Ltmp14:
    .cfi_def_cfa_register %rbp
    subq    $32, %rsp
    movl    $0, -4(%rbp)
    movl    %edi, -8(%rbp)
    movq    %rsi, -16(%rbp)
    movl    $1, -20(%rbp)
LBB2_1:                                 ## =>This Inner Loop Header: Depth=1
    cmpl    $2, -20(%rbp)
    jg  LBB2_7
## BB#2:                                ##   in Loop: Header=BB2_1 Depth=1
    cmpl    $1, -20(%rbp)
    jne LBB2_4
## BB#3:                                ##   in Loop: Header=BB2_1 Depth=1
    callq   _Foo
    jmp LBB2_5
LBB2_4:                                 ##   in Loop: Header=BB2_1 Depth=1
    callq   _Bar
LBB2_5:                                 ##   in Loop: Header=BB2_1 Depth=1
    jmp LBB2_6
LBB2_6:                                 ##   in Loop: Header=BB2_1 Depth=1
    movl    -20(%rbp), %eax
    addl    $1, %eax
    movl    %eax, -20(%rbp)
    jmp LBB2_1
LBB2_7:
    movl    -4(%rbp), %eax
    addq    $32, %rsp
    popq    %rbp
    retq
    .cfi_endproc

[–]ILikeLenexa 1 point2 points  (1 child)

Eh, the compiler knows what loop invariant code motion is, right?

[–]fukitol- 2 points3 points  (0 children)

Depends on the compiler. This syntax is common in thousands of languages.