Site Guide

Find your way around the Engineering Club.

Know what you need? Search for a component, concept or project.

Around the site

Start

Labs

TestingExperimental

Build

Club

LearnExperimental

Go a little deeper

Entry points to larger collections. Use Search for individual records.

Learning paths & reference

Inside PiCrawlerExperimental

Inside Inventory

← C++ Worked Programs

Learn / Worked program

Piecewise Function Table

Equality can be correct when the domain is deliberate

BeginnerDesktop C++17 · runnable source

What you will learn

The complete source

main.cpp

main.cpp
#include <iomanip>
#include <iostream>

double functionF(double x) {
    return x * x - 16.0;
}

double functionG(double x, double fValue) {
    if (fValue < 0.0) return 1.0;
    if (fValue == 0.0) return x * x + 16.0;
    return 0.0;
}

int main() {
    std::cout << "x f g sum\n" << std::fixed << std::setprecision(2);
    // Half-integers are exactly representable in binary; the grid contains 4 exactly.
    for (int halfSteps = 2; halfSteps <= 20; ++halfSteps) {
        const double x = halfSteps / 2.0;
        const double f = functionF(x);
        const double g = functionG(x, f);
        std::cout << x << ' ' << f << ' ' << g << ' ' << f + g << '\n';
    }
}
Source integrity

SHA-256 b968c49ee6aaf4957d5f9eb74ca441b7e934c122978ccacd7ed5997f34f45d44. Display, copy and approved download use the same source.

What it does and why it was selected

This is the small mathematical seed that later grew into Functionator. It has a real discontinuous branch at x=4 on the displayed domain, making it a useful test of how a mathematical definition maps to C++ rather than a generic function-call exercise.

Actual execution

main loops an integer halfSteps value from 2 through 20. Dividing by 2.0 creates x values 1,1.5,...,10. functionF computes xx-16 once. That value is passed with x to functionG. If f is negative, g returns 1; if f is exactly zero, g returns xx+16; otherwise it returns zero. The table prints x, f, g and their sum, so each branch’s contribution is visible. Immediately around the root, rows are x=3.5 with sum -2.75, x=4 with sum 32, and x=4.5 with sum 4.25.

Mathematical or state model

f(x)=x^2-16. g(x)=1 when f(x)<0, x^2+16 when f(x)=0, and 0 when f(x)>0. Since x^2-16 vanishes at x=-4 and x=4, the full mathematical function has two root locations. The chosen display interval includes only +4. The root branch is discontinuous and is part of the supplied definition, not a numeric instability to be smoothed away.

Design and C++ mechanics

Half-integers are exactly representable in binary floating point, and this bounded grid produces x=4 and f=0 exactly. Therefore equality to zero is justified here. Automatically replacing that comparison with a broad tolerance would change the mathematical branch to a neighborhood around the root. Passing fValue avoids repeating the same f calculation three times inside g. It also establishes a consistency precondition: functionG expects fValue to correspond to the supplied x.

Historical differences and limits

That equality policy should not be copied indiscriminately to calculations from measured inputs or decimal steps. The historical optional game stub uses int i with i+=0.5, which converts each result back to int and prevents progress; it is not a functioning game loop. The canonical table uses an integer index with explicit division, making the grid and termination auditable. A generalized evaluator may need a different interface so x and fValue cannot be supplied inconsistently.

Useful extensions

Include negative x values and verify the -4 root as well as +4. If adapting the table to user-selected spacing, document whether exact mathematical equality, approximate numerical equality or measurement tolerance controls the branch. Those policies describe different programs and should not be hidden inside a convenient epsilon constant.

Source-linked explanations

Follow each line reference to its marked source region. The explanations below are bound to the reviewed source hash and preserve the supplied teaching layers.

Define the first mathematical function

main.cpp, lines 4–6 →

x*x is multiplication rather than exponent notation.

Technical explanation
x*x is multiplication rather than exponent notation. Subtracting 16 places roots at +/-4. The floating literals and double parameter keep this calculation in floating arithmetic even though the displayed grid is constructed from an integer counter.
Syntax
The floating literals and double parameter keep this calculation in floating arithmetic even though the displayed grid is constructed from an integer counter.
Effect on the program
The floating literals and double parameter keep this calculation in floating arithmetic even though the displayed grid is constructed from an integer counter.
Common mistake
The C++ ^ operator does not mean exponentiation. x^2 would be an integer bitwise operation, not this function.

functionsfloating point

Translate a piecewise definition directly

main.cpp, lines 8–12 →

The negative branch returns 1; the zero branch uses the SAME x to return x*x+16; the positive fallback returns zero.

Technical explanation
The negative branch returns 1; the zero branch uses the SAME x to return x*x+16; the positive fallback returns zero. fValue was calculated once by the caller. The supplied finite half-grid makes exact zero at x=4 meaningful.
Syntax
The supplied finite half-grid makes exact zero at x=4 meaningful.
Effect on the program
The supplied finite half-grid makes exact zero at x=4 meaningful.
Common mistake
Adding a broad epsilon would turn one exact root into an interval and alter the piecewise mathematical definition.

conditionalsparameters

A binary-exact grid with visible branches

main.cpp, lines 17–23 →

halfSteps/2.0 creates half-integers, which have exact binary representations in this bounded range.

Technical explanation
halfSteps/2.0 creates half-integers, which have exact binary representations in this bounded range. Nineteen iterations include x=4 exactly. Printing f and g separately exposes the large root-branch jump instead of hiding it inside the sum.
Syntax
Printing f and g separately exposes the large root-branch jump instead of hiding it inside the sum.
Effect on the program
Printing f and g separately exposes the large root-branch jump instead of hiding it inside the sum.
Common mistake
The historical game stub used int i += 0.5; assigning each result back to int prevented the loop from advancing.

loopsfloating point

Recorded input and output

Captured from the compiled canonical program in the supplied verification package. These are recorded terminal sessions, not an interactive browser runner.

Sample run

Arguments
None
Recorded exit status
0

Program input

No standard input.

Program output

x f g sum
1.00 -15.00 1.00 -14.00
1.50 -13.75 1.00 -12.75
2.00 -12.00 1.00 -11.00
2.50 -9.75 1.00 -8.75
3.00 -7.00 1.00 -6.00
3.50 -3.75 1.00 -2.75
4.00 0.00 32.00 32.00
4.50 4.25 0.00 4.25
5.00 9.00 0.00 9.00
5.50 14.25 0.00 14.25
6.00 20.00 0.00 20.00
6.50 26.25 0.00 26.25
7.00 33.00 0.00 33.00
7.50 40.25 0.00 40.25
8.00 48.00 0.00 48.00
8.50 56.25 0.00 56.25
9.00 65.00 0.00 65.00
9.50 74.25 0.00 74.25
10.00 84.00 0.00 84.00

Build and run

From this folder, with a configured C++17 compiler:

sh
g++ -std=c++17 -Wall -Wextra -Wpedantic main.cpp -o program
./program

On Windows GCC, build with -o program.exe and use .\program.exe. These are desktop examples; VS Code is an editor, the compiler builds an executable, and the terminal runs it.

Windows run command from the supplied metadata: .\program.exe.

Desktop compiler setup and platform limits. How headers and translation units fit together.

Verification and provenance

  • Source inspected
  • Compile verified
  • Warning reviewed
  • Behavior verified
  • Sanitizer checked

Supplied evidence: 1 recorded behavior/error cases and 1 address/undefined-behavior sanitizer cases for this exact source. These are tested cases, not an exhaustive correctness claim.

Recorded compiler: g++ (Ubuntu 13.3.0-6ubuntu2~24.04) 13.3.0.

LeakSanitizer: disabled because unavailable under runtime tracing. Windows/MSVC and macOS execution are not certified by this evidence.

Source origin and publication treatment

Modernized user supplied source

User-supplied origin; no independent authorship or license determination. Instructor code is not included.

New or rewritten source, limited sanitized historical excerpts when useful

No new license is asserted by this publication.