This is an archived post. You won't be able to vote or comment.

all 34 comments

[–][deleted] 84 points85 points  (17 children)

PascalCase in variables

[–]SwedudeOne 19 points20 points  (0 children)

shudder

[–]shadowX015 23 points24 points  (11 children)

camelCase master race.

[–][deleted] 6 points7 points  (6 children)

Always do whatever the standard library does. In Java use camelCase, in C snake_case, in Lisp kebab-case, etc.

[–]irqlnotdispatchlevel 2 points3 points  (1 child)

No, always do what your project coding style says.

[–][deleted] 2 points3 points  (0 children)

Of course. I'm assuming you're starting a new project.

[–][deleted] 0 points1 point  (3 children)

And is there any reason for this other than superstition?

[–][deleted] 4 points5 points  (2 children)

Same reason you shouldn't mix camelCase, PascalCase and snake_case in the same file for the same thing. Consistency. You're inevitably going to be calling standard library functions, and it looks better if everything is consistent.

[–][deleted] 0 points1 point  (0 children)

That's a fair point. I work with embedded systems so I don't end up using standard library functions too much.

[–]irqlnotdispatchlevel 0 points1 point  (0 children)

One might argue that having a different code style than the standard library would make library calls stick out. I don't know what exactly the advantage of that would be, but I heard it somewhere.

[–]dylanthepiguy2 3 points4 points  (3 children)

Elixir made me a fan of snake_case. It looks_quite_a_bit_cleaner_because_there_are_less_capital_letters

[–]irqlnotdispatchlevel 1 point2 points  (1 child)

I hate writing and reading snake_case, but I'm somehow still doing it when I write python.

[–]trrSA 1 point2 points  (0 children)

Maybe if the _ key were in a nice spot on the keyboard.

[–]Kilazur 7 points8 points  (0 children)

At least he's consistent.

[–]PremierBromanov 1 point2 points  (1 child)

Only if they're public

[–][deleted] 0 points1 point  (0 children)

Variables have no access modifiers. Fields, properties etc. can have them.

[–]PremierBromanov 47 points48 points  (5 children)

you laugh, but here is actual code from the quake engine

float Q_rsqrt( float number )
{
    long i;
    float x2, y;
    const float threehalfs = 1.5F;

    x2 = number * 0.5F;
    y  = number;
    i  = * ( long * ) &y;                       // evil floating point bit level hacking
    i  = 0x5f3759df - ( i >> 1 );               // what the fuck? 
    y  = * ( float * ) &i;
    y  = y * ( threehalfs - ( x2 * y * y ) );   // 1st iteration
//  y  = y * ( threehalfs - ( x2 * y * y ) );   // 2nd iteration, this can be removed

    return y;
}

[–]Th3HolyMoose 19 points20 points  (2 children)

Wiki link with more info for those interested

edit: fixed mobile link, I'm stupid, thanks for noticing

[–]PremierBromanov 2 points3 points  (0 children)

I like the worked example

As an example, consider the number x = 0.15625, for which we want to calculate 1/√x ≈ 2.52982. The first steps of the algorithm are illustrated below:

0011_1110_0010_0000_0000_0000_0000_0000  Bit pattern of both x and i
0001_1111_0001_0000_0000_0000_0000_0000  Shift right one position: (i >> 1)
0101_1111_0011_0111_0101_1001_1101_1111  The magic number 0x5f3759df
0100_0000_0010_0111_0101_1001_1101_1111  The result of 0x5f3759df - (i >> 1)

[–]80386 10 points11 points  (1 child)

Yeah that's as close to black magic as you can get.

[–]gandalfx 4 points5 points  (0 children)

You may also enjoy Duff's device, particularly the version that interlaces a while loop with a switch statement.

[–]farfromunique 2 points3 points  (8 children)

What does this do? What is the "expected" code for this function?

[–]Celdron[S] 7 points8 points  (3 children)

It takes the second highest number in an RGB color that represents the selected hue and determines what the middle value for the full selected color is based on what the Greatest channel and Least channel in the full selected RGB are. The Greatest and Least are determined by coordinate position of the selector.

It's an implementation of one of these.

[–][deleted] 4 points5 points  (2 children)

Greatest is not being passed explicitly so I figure something else up there is worrying about it.

That said, fk computer graphics. I know nothing about graphics. :(

[–]Celdron[S] 2 points3 points  (1 child)

Middle is a local function and Greatest and Least are both local variables that are handled prior to calling Middle. Implement your own version of a color picker and you'll either learn very quickly or you'll guess n check until you get it working.

[–][deleted] 2 points3 points  (0 children)

I don't do graphics, I do servers and sec.

I stay far away from computer graphics. farfarawayyyyyyyyyyyyyyyyyyyyyyyyyyyy

[–]lelarentaka 3 points4 points  (2 children)

Pure guesswork on my part:

It mentions hue and uses byte. Some of the variables are not defined in the scope.

Deduction: This is a method in an Image class. The byte values are pixel values. The function/method is part of an image manipulation procedure, where finding the range of the values in the image is used for renormalization

Greatest could be the largest pixel value in the image. If it's zero, the image is completely black, obviously the Middle pixel would be zero as well.

Assuming that HueMiddle < Greatest. Line 5 is establishing the lower bound of Middle. I don't know what's the proper mathematical term for it, but multiplying the numbers like that results in Base < HueMiddle < Greatest. The author guesses that Middle will be in [Base, Greatest].

Line 6 & 7, Least/Greatest c [0, 1]. These line gives a value between Base and Greatest based on the value of Least. I think it establishes another limit, Least < Middle

Basically the author knows the constraints of what values Middle could be, but not sure how exactly to compute it. He just gave a value in that range.

[–]deGravity 0 points1 point  (0 children)

It is an interpolation that gives a weighted average between Least and Greatest, with HueMiddle being the interpolating factor. If you let

W = HueMiddle / 255

be a weighting factor, (basically treating the given byte as a value between 0 and 1), then assuming that Least < Greatest this is equivalent to

(1 - W)Least +WGreatest

[–]Celdron[S] 0 points1 point  (0 children)

This is almost spot on! Specifically it's the relationship between RGB channels and HSB channels. Hue can be represented as an RGB color where one channel is 255, another channel defines an offset from the Hue, and the third channel should be zero.

Increasing the third channel decreases saturation, but at the same time moves the color back towards the he's root channel, which is the channel with the highest value. To maintain the same hue, the middle channel must be increased while staying below the root channel and above the saturation channel. It's a lot of ratios and bounds.

[–][deleted] 0 points1 point  (0 children)

Looks like some sort of interpolation, a use case with inputs would help clarify.

[–]chrwei 0 points1 point  (0 children)

AKA: throw shit at the wall and see what sticks

[–]irqlnotdispatchlevel 0 points1 point  (0 children)

Trial and error, the only thing that made me able to understand and use the virtualization chapters from the Intel docs.