Skip to main content
added 213 characters in body
Source Link
Madagascar
  • 10.1k
  • 1
  • 16
  • 52

Use the standard library where possible:

stdlib.h defines these functions:

int        abs( int n );
long       labs( long n );
long long llabs( long long n ); // (since C99)

and math.h defines these functions:

float       fabsf( float arg ); // (since C99)
double      fabs( double arg );     
long double fabsl( long double arg ); // (since C99)

// and sqrtf() et cetera.

Consider using them instead of rolling out your own. See also: Pitfalls of function-like macros

Simplify:

float Vector2D_DotProduct(Vector2D a, Vector2D b)
{
#if 0
    float dp = a.x*b.x+a.y*b.y;
    return dp;
#else
    return a.x * b.x + a.y * b.y;
#endif
}

Vector2D Vector2DSubtract(Vector2D a, Vector2D b)
{
#if 0
    Vector2D v;
    v.x = a.x - b.x;
    v.y = a.y - b.y;
    return v;
#else
    return (Vector2D) {.x = a.x - b.x, .y = a.y - b.y};
#endif
}

See: Compound Literals.

Use the standard library where possible:

stdlib.h defines these functions:

int        abs( int n );
long       labs( long n );
long long llabs( long long n ); // (since C99)

Consider using them instead of rolling out your own. See also: Pitfalls of function-like macros

Simplify:

float Vector2D_DotProduct(Vector2D a, Vector2D b)
{
#if 0
    float dp = a.x*b.x+a.y*b.y;
    return dp;
#else
    return a.x * b.x + a.y * b.y;
#endif
}

Vector2D Vector2DSubtract(Vector2D a, Vector2D b)
{
#if 0
    Vector2D v;
    v.x = a.x - b.x;
    v.y = a.y - b.y;
    return v;
#else
    return (Vector2D) {.x = a.x - b.x, .y = a.y - b.y};
#endif
}

See: Compound Literals.

Use the standard library where possible:

stdlib.h defines these functions:

int        abs( int n );
long       labs( long n );
long long llabs( long long n ); // (since C99)

and math.h defines these functions:

float       fabsf( float arg ); // (since C99)
double      fabs( double arg );     
long double fabsl( long double arg ); // (since C99)

// and sqrtf() et cetera.

Consider using them instead of rolling out your own. See also: Pitfalls of function-like macros

Simplify:

float Vector2D_DotProduct(Vector2D a, Vector2D b)
{
#if 0
    float dp = a.x*b.x+a.y*b.y;
    return dp;
#else
    return a.x * b.x + a.y * b.y;
#endif
}

Vector2D Vector2DSubtract(Vector2D a, Vector2D b)
{
#if 0
    Vector2D v;
    v.x = a.x - b.x;
    v.y = a.y - b.y;
    return v;
#else
    return (Vector2D) {.x = a.x - b.x, .y = a.y - b.y};
#endif
}

See: Compound Literals.

added 404 characters in body
Source Link
Madagascar
  • 10.1k
  • 1
  • 16
  • 52

Use the standard library where possible:

stdlib.h defines these functions:

int        abs( int n );
long       labs( long n );
long long llabs( long long n ); // (since C99)

Consider using them instead of rolling out your own. See also: Pitfalls of function-like macros

Simplify:

float Vector2D_DotProduct(Vector2D a, Vector2D b)
{
#if 0
    float dp = a.x*b.x+a.y*b.y;
    return dp;
#else
    return a.x * b.x + a.y * b.y;
#endif
}

Vector2D Vector2DSubtract(Vector2D a, Vector2D b)
{
#if 0
    Vector2D v;
    v.x = a.x - b.x;
    v.y = a.y - b.y;
    return v;
#else
    return (Vector2D) {.x = a.x - b.x, .y = a.y - b.y};
#endif
}

See: Compound Literals.

Use the standard library where possible:

stdlib.h defines these functions:

int        abs( int n );
long       labs( long n );
long long llabs( long long n ); // (since C99)

Consider using them instead of rolling out your own. See also: Pitfalls of function-like macros

Use the standard library where possible:

stdlib.h defines these functions:

int        abs( int n );
long       labs( long n );
long long llabs( long long n ); // (since C99)

Consider using them instead of rolling out your own. See also: Pitfalls of function-like macros

Simplify:

float Vector2D_DotProduct(Vector2D a, Vector2D b)
{
#if 0
    float dp = a.x*b.x+a.y*b.y;
    return dp;
#else
    return a.x * b.x + a.y * b.y;
#endif
}

Vector2D Vector2DSubtract(Vector2D a, Vector2D b)
{
#if 0
    Vector2D v;
    v.x = a.x - b.x;
    v.y = a.y - b.y;
    return v;
#else
    return (Vector2D) {.x = a.x - b.x, .y = a.y - b.y};
#endif
}

See: Compound Literals.

Source Link
Madagascar
  • 10.1k
  • 1
  • 16
  • 52

Use the standard library where possible:

stdlib.h defines these functions:

int        abs( int n );
long       labs( long n );
long long llabs( long long n ); // (since C99)

Consider using them instead of rolling out your own. See also: Pitfalls of function-like macros