Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions doc/modules/ROOT/pages/examples.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,35 @@ Match: true
----
====

[#examples_rollover]
== Rollover Behavior

.This https://github.com/cppalliance/int128/blob/develop/examples/rollover.cpp[example] demonstrates the rollover behavior of both the unsigned and signed type
====
[source, c++]
----
include::example$rollover.cpp[]
----

.Expected Output
[listing]
----
=== uint128_t behavior ===
Max of uint128_t: 340282366920938463463374607431768211455
Max + 1U: 0

Min of uint128_t: 0
Min - 1U: 340282366920938463463374607431768211455

=== int128_t behavior ===
Max of int128_t: 170141183460469231731687303715884105727
Max + 1: -170141183460469231731687303715884105728

Min of int128_t: -170141183460469231731687303715884105728
Min - 1: 170141183460469231731687303715884105727
----
====

[#examples_bit]
== Bitwise Functions (<bit>)

Expand Down
38 changes: 38 additions & 0 deletions examples/rollover.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
// Copyright 2025 Matt Borland
// Distributed under the Boost Software License, Version 1.0.
// https://www.boost.org/LICENSE_1_0.txt
//
// This example demonstrates the rollover behavior for both signed and unsigned int128

#include <boost/int128.hpp>
#include <iostream>
#include <iomanip>
#include <limits>

int main()
{
using boost::int128::uint128_t;
using boost::int128::int128_t;

constexpr uint128_t max_unsigned_value {std::numeric_limits<uint128_t>::max()};
constexpr uint128_t min_unsigned_value {std::numeric_limits<uint128_t>::min()};

std::cout << "=== uint128_t behavior ===" << std::endl;

std::cout << "Max of uint128_t: " << max_unsigned_value << '\n'
<< "Max + 1U: " << max_unsigned_value + 1U << "\n\n";

std::cout << "Min of uint128_t: " << min_unsigned_value << '\n'
<< "Min - 1U: " << min_unsigned_value - 1U << "\n\n";

constexpr int128_t max_signed_value {std::numeric_limits<int128_t>::max()};
constexpr int128_t min_signed_value {std::numeric_limits<int128_t>::min()};

std::cout << "=== int128_t behavior ===" << std::endl;

std::cout << "Max of int128_t: " << max_signed_value << '\n'
<< "Max + 1: " << max_signed_value + 1 << "\n";

std::cout << "\nMin of int128_t: " << min_signed_value << '\n'
<< "Min - 1: " << min_signed_value - 1 << '\n' << std::endl;
}
1 change: 1 addition & 0 deletions test/Jamfile
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ run ../examples/charconv.cpp ;
run ../examples/fmt_format.cpp ;
run ../examples/cstdlib.cpp ;
run ../examples/numeric_algorithms.cpp ;
run ../examples/rollover.cpp ;

run limits_link_1.cpp limits_link_2.cpp limits_link_3.cpp ;

Expand Down