Skip to content

math/perimeter: parallelogram_perimeter uses perpendicular height instead of the side length (wrong perimeter) #3174

Description

@aimasteracc

Description

math/perimeter.cpp::parallelogram_perimeter(base, height) computes the perimeter from the parallelogram's perpendicular height, which is geometrically incorrect. The perimeter of a parallelogram is 2 * (base + side), where side is the length of the slanted side — not the height.

/**
 * @param base is the length of the bottom side of the parallelogram
 * @param height is the length of the tallest point in the parallelogram   // <- perpendicular height
 * @returns perimeter of the parallelogram
 */
template <typename T>
T parallelogram_perimeter(T base, T height) {
    return 2 * (base + height);   // BUG: uses height instead of the slant side length
}

The docstring explicitly defines height as "the length of the tallest point" (the perpendicular height). For any non-rectangular parallelogram the slant side is strictly longer than the height, so the function under-reports the perimeter. The two are equal only when the parallelogram is a rectangle.

Note the sibling math/area.cpp::parallelogram_area correctly uses base * height (area genuinely uses the perpendicular height). The perimeter function appears to have copied that (base, height) signature, but perimeter needs the side length, not the height.

Steps to reproduce

A parallelogram with base = 6, perpendicular height = 4, and slant side = 5:

  • True perimeter: 2 * (6 + 5) = 22
  • parallelogram_perimeter(6, 4) returns 2 * (6 + 4) = 20

(The height 4 and side 5 are consistent — e.g. a horizontal offset of 3 gives height sqrt(5² − 3²) = 4.)

Expected behavior

The function should take the side length and return 2 * (base + side), yielding 22 for the example above.

Actual behavior

It takes the perpendicular height and returns 2 * (base + height) = 20, which is not the perimeter of the parallelogram.

Suggested fix

Take the slant side length instead of the height and update the docstring/parameter name accordingly:

/**
 * @param base is the length of the bottom side of the parallelogram
 * @param side is the length of the slanted side of the parallelogram
 */
template <typename T>
T parallelogram_perimeter(T base, T side) {
    return 2 * (base + side);
}

(The corresponding test in the file's test()/main() should be updated to supply a side length and assert 2 * (base + side).)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions