Library contents

C++ std::fmin

Choose the smaller of the voucher balance and the amount owed.

A simple example

std::fmin

  1. Start with 2 and 5
  2. Do this Choose the smaller value
  3. Result 2
Comparing the two numbers places 2 below 5.
lesson.cpp
#include <iostream>
#include <cmath>

int main() {
    std::cout << (std::fmin(2.0, 5.0)) << '\n';
}
Output
2

Syntax

C++ syntax
std::fmin(voucher, due)
Run these examples in C++

Save one example at a time as lesson.cpp. Use C++17 and a Unix-style shell.

c++ -std=c++17 -Wall -Wextra lesson.cpp -o lesson
./lesson

Cap a voucher at the total due

A shopper presents store credit at checkout. The till must cap the usable credit at the outstanding bill and handle boundary amounts before it prints the balance.

From the programconst double voucher = 12.0
Result8.000
lesson.cpp
#include <cmath>
#include <cstdio>

int main() {
    const double voucher = 12.0;
    const double due = 8.0;

    std::printf("%.3f\n", std::fmin(voucher, due));
    return 0;
}
Output
8.000

A voucher worth twelve covers eight on an eight-unit bill.

Advertisements

Limit a reservation to available seats

A booking screen receives a requested party size and the current seat inventory. Use the available capacity to decide how many places can actually be assigned.

From the programconst double requested = 18.0
Result12.000
lesson.cpp
#include <cmath>
#include <cstdio>

int main() {
    const double requested = 18.0;
    const double available = 12.0;

    std::printf("%.3f\n", std::fmin(requested, available));
    return 0;
}
Output
12.000

Twelve seats remain, so a request for eighteen can reserve at most twelve.

Limit a workshop booking

A parent requests five seats, but the workshop has only three places left. The booking system chooses the smaller count.

GivenRequested 5 · available 3
Worked result3 seats

The booking never assigns more seats than the venue has available.

What to watch for

Apply the same currency and unit to both values.

Documentation for std::fmin