### Initialize Fixed64Random Generator Source: https://context7.com/skynetnext/fixed64/llms.txt Demonstrates initializing the Fixed64Random generator with a specific seed for reproducibility or a random seed. Shows how to get and set the seed. ```cpp #include "fixed64.h" #include "fixed64_random.h" using math::fp::Fixed64_16; using math::fp::Fixed64Random; int main() { // Create random generator with specific seed for reproducibility Fixed64Random rng(12345); // Create random generator with random seed Fixed64Random rng_random(0); // 0 means use random_device // Get/set seed int32_t seed = rng.getSeed(); // 12345 rng.setSeed(67890); // Change seed return 0; } ``` -------------------------------- ### Fixed64 Standard Library Integration Example Source: https://context7.com/skynetnext/fixed64/llms.txt Showcases std::numeric_limits, hash support for unordered_map, string conversions, standard math function overloads, and special value checks with Fixed64 types. ```cpp #include "fixed64.h" #include "fixed64_math.h" #include #include #include using math::fp::Fixed64_16; using math::fp::Fixed64_32; int main() { // std::numeric_limits support auto max_val = std::numeric_limits::max(); auto min_val = std::numeric_limits::min(); auto epsilon = std::numeric_limits::epsilon(); auto infinity_val = std::numeric_limits::infinity(); auto nan_val = std::numeric_limits::quiet_NaN(); bool is_signed = std::numeric_limits::is_signed; // true bool is_integer = std::numeric_limits::is_integer; // false bool is_exact = std::numeric_limits::is_exact; // true bool has_infinity = std::numeric_limits::has_infinity; // true // Hash support for use in containers std::unordered_map value_map; value_map[Fixed64_16(1.5)] = "one point five"; value_map[Fixed64_16(2.5)] = "two point five"; auto it = value_map.find(Fixed64_16(1.5)); if (it != value_map.end()) { std::cout << "Found: " << it->second << std::endl; } // String conversion Fixed64_16 num(123.456); std::string str = std::to_string(num); // "123.456" Fixed64_16 parsed = std::stof64<16>("123.456"); // 123.456 // Standard math function overloads (using Fixed64_32 for trig) Fixed64_32 angle(0.5); auto sin_val = std::sin(angle); auto cos_val = std::cos(angle); auto exp_val = std::exp(Fixed64_32(1.0)); auto sqrt_val = std::sqrt(Fixed64_32(4.0)); auto abs_val = std::abs(Fixed64_32(-5.0)); auto floor_val = std::floor(Fixed64_32(3.7)); auto ceil_val = std::ceil(Fixed64_32(3.3)); // Special value checks Fixed64_16 inf = Fixed64_16::Infinity(); Fixed64_16 nan = Fixed64_16::NaN(); Fixed64_16 normal(5.0); bool is_inf = std::isinf(inf); // true bool is_nan = std::isnan(nan); // true bool is_finite = std::isfinite(normal); // true bool is_negative = std::signbit(Fixed64_16(-1)); // true // Copy sign auto copied = std::copysign(Fixed64_16(5.0), Fixed64_16(-1.0)); // -5.0 // Modulo operation auto mod_result = std::fmod(Fixed64_16(7.5), Fixed64_16(2.5)); // 0.0 std::cout << "Max: " << max_val << std::endl; std::cout << "Epsilon: " << epsilon << std::endl; return 0; } ``` -------------------------------- ### Fixed64 CMake Configuration Source: https://github.com/skynetnext/fixed64/blob/main/CMakeLists.txt The primary CMake configuration file for the project, including standard settings, library definitions, and conditional build logic for tests, examples, and benchmarks. ```cmake cmake_minimum_required(VERSION 3.10) project(Fixed64 VERSION 1.0.0 LANGUAGES CXX) # Set C++ standard set(CMAKE_CXX_STANDARD 20) set(CMAKE_CXX_STANDARD_REQUIRED ON) set(CMAKE_CXX_EXTENSIONS OFF) # Add include directory include_directories(${PROJECT_SOURCE_DIR}/math/fp) add_library(Fixed64 INTERFACE) target_include_directories(Fixed64 INTERFACE ${PROJECT_SOURCE_DIR}/math/fp) # Enable testing enable_testing() # Add CTest support, this will generate DartConfiguration.tcl include(CTest) set(CTEST_PROJECT_NAME "Fixed64") set(CTEST_NIGHTLY_START_TIME "00:00:00 UTC") set(gtest_force_shared_crt ON CACHE BOOL "Use shared (DLL) run-time lib even when Google Test is built as static lib" FORCE) # Add Google Test add_subdirectory(third_party/googletest) # Configure CTest to show output from all tests set(CMAKE_CTEST_ARGUMENTS "--output-on-failure" "--verbose") # Add tests directory add_subdirectory(tests) # Create example programs (if example code exists) if(EXISTS "${PROJECT_SOURCE_DIR}/examples") file(GLOB EXAMPLE_SOURCES "examples/*.cpp") foreach(example_source ${EXAMPLE_SOURCES}) get_filename_component(example_name ${example_source} NAME_WE) add_executable(${example_name} ${example_source}) target_link_libraries(${example_name} Fixed64) # Set compiler-specific warning flags if(MSVC) # MSVC compiler flags set(COMPILER_WARNINGS /W4 /WX- # Don't treat warnings as errors /wd4127 # Disable warning C4127: conditional expression is constant /wd4244 # Disable warning C4244: conversion from 'int64_t' to 'int', possible loss of data /wd4146 # Disable warning C4146: unary minus operator applied to unsigned type /wd4996 # Disable specific warnings, e.g., deprecated function warnings ) else() # GCC/Clang compiler flags set(COMPILER_WARNINGS -Wall -Wextra -O3 ) endif() # Apply conditional compiler flags target_compile_options(${example_name} PRIVATE ${COMPILER_WARNINGS}) endforeach() endif() set(CMAKE_EXPORT_COMPILE_COMMANDS ON) # Option to enable soft_double benchmarks option(ENABLE_BENCHMARK "Enable benchmarks " ON) if(ENABLE_BENCHMARK) # Add soft_double headers include_directories(third_party/soft_double) add_subdirectory(benchmarks) endif() ``` -------------------------------- ### Outputting Random Values Source: https://context7.com/skynetnext/fixed64/llms.txt Example of printing generated random numbers to the console using std::cout. ```cpp std::cout << "Random [0,1): " << rand_01 << std::endl; std::cout << "Random [10,20): " << rand_range << std::endl; std::cout << "Weighted selection: " << selected_index << std::endl; ``` -------------------------------- ### Perform Basic Arithmetic and Comparisons with Fixed64 Source: https://github.com/skynetnext/fixed64/blob/main/docs/examples.md Demonstrates instantiation, basic arithmetic operations, and comparison operators for Fixed64 types. ```cpp #include "fixed64.h" using math::fp::Fixed64<16>; int main() { // Create fixed-point numbers Fixed64<16> a(5.5); Fixed64<16> b(2.25); // Basic arithmetic Fixed64<16> sum = a + b; // 7.75 Fixed64<16> diff = a - b; // 3.25 Fixed64<16> product = a * b; // 12.375 Fixed64<16> quotient = a / b; // 2.444... // Comparison bool isGreater = (a > b); // true // Constants Fixed64<16> pi = Fixed64<16>::Pi(); std::cout << "Result: " << quotient << std::endl; return 0; } ``` -------------------------------- ### Perform basic arithmetic with Fixed64 Source: https://github.com/skynetnext/fixed64/blob/main/README.md Demonstrates initializing Fixed64 objects and performing addition and multiplication operations. ```cpp #include "fixed64.h" using math::fp::Fixed64<32>; // 32-bit fractional precision int main() { Fixed64<32> a(5.5); Fixed64<32> b(2.25); Fixed64<32> sum = a + b; // 7.75 Fixed64<32> product = a * b; // 12.375 std::cout << "Result: " << sum << std::endl; return 0; } ``` -------------------------------- ### Perform Interpolation with Fixed64Math Source: https://context7.com/skynetnext/fixed64/llms.txt Demonstrates linear interpolation, unclamped interpolation, inverse interpolation, and angle-aware blending using Fixed64 types. ```cpp #include "fixed64.h" #include "fixed64_math.h" using math::fp::Fixed64_16; using math::fp::Fixed64_32; using math::fp::Fixed64Math; int main() { Fixed64_16 start(0.0); Fixed64_16 end(100.0); // Linear interpolation (clamped t to [0, 1]) auto lerp_0 = Fixed64Math::Lerp(start, end, Fixed64_16(0.0)); // 0.0 auto lerp_half = Fixed64Math::Lerp(start, end, Fixed64_16(0.5)); // 50.0 auto lerp_1 = Fixed64Math::Lerp(start, end, Fixed64_16(1.0)); // 100.0 auto lerp_over = Fixed64Math::Lerp(start, end, Fixed64_16(1.5)); // 100.0 (clamped) // Unclamped linear interpolation (allows extrapolation) auto lerp_unclamped = Fixed64Math::LerpUnclamped(start, end, Fixed64_16(1.5)); // 150.0 auto lerp_neg = Fixed64Math::LerpUnclamped(start, end, Fixed64_16(-0.5)); // -50.0 // Inverse lerp - find t given value auto inv_lerp = Fixed64Math::InverseLerp(start, end, Fixed64_16(25.0)); // 0.25 auto inv_lerp_mid = Fixed64Math::InverseLerp(start, end, Fixed64_16(50.0)); // 0.5 // Angle interpolation (takes shortest path) Fixed64_32 angle_start = Fixed64_32::Zero(); // 0 radians Fixed64_32 angle_end = Fixed64_32::Deg2Rad() * Fixed64_32(90); // π/2 radians auto angle_mid = Fixed64Math::LerpAngle(angle_start, angle_end, Fixed64_32(0.5)); // Result: π/4 radians (45 degrees) // Angle interpolation handles wrap-around Fixed64_32 angle_350 = Fixed64_32::Deg2Rad() * Fixed64_32(350); // Near 360° Fixed64_32 angle_10 = Fixed64_32::Deg2Rad() * Fixed64_32(10); // Near 0° auto angle_wrap = Fixed64Math::LerpAngle(angle_350, angle_10, Fixed64_32(0.5)); // Takes shortest path through 360°/0° // Repeat function (useful for looping values) Fixed64_16 value(7.5); Fixed64_16 length(3.0); auto repeated = Fixed64Math::Repeat(value, length); // 1.5 // Practical example: animation blending Fixed64_16 frame_a(0.0); Fixed64_16 frame_b(100.0); Fixed64_16 blend_factor(0.3); // 30% blend auto blended = Fixed64Math::Lerp(frame_a, frame_b, blend_factor); // 30.0 std::cout << "Lerp(0, 100, 0.5): " << lerp_half << std::endl; // Output: 50 std::cout << "InverseLerp(0, 100, 25): " << inv_lerp << std::endl; // Output: 0.25 return 0; } ``` -------------------------------- ### Perform Arithmetic Operations with Fixed64 Source: https://context7.com/skynetnext/fixed64/llms.txt Demonstrates basic arithmetic, compound assignments, unary operators, and native type interactions. Division by zero returns Infinity or NegInfinity. ```cpp #include "fixed64.h" using math::fp::Fixed64_16; int main() { Fixed64_16 a(3.75); Fixed64_16 b(1.25); Fixed64_16 one = Fixed64_16::One(); // Basic arithmetic Fixed64_16 sum = a + b; // 5.0 Fixed64_16 diff = a - b; // 2.5 Fixed64_16 product = a * b; // 4.6875 Fixed64_16 quotient = a / b; // 3.0 Fixed64_16 remainder = a % b; // 0.0 // Compound assignment operators Fixed64_16 result = a; result += b; // 5.0 result -= one; // 4.0 result *= Fixed64_16(2); // 8.0 result /= Fixed64_16(4); // 2.0 // Increment/decrement Fixed64_16 counter(5); ++counter; // 6.0 (prefix increment) counter++; // 7.0 (postfix increment) --counter; // 6.0 (prefix decrement) // Unary operators Fixed64_16 neg = -a; // -3.75 Fixed64_16 pos = +a; // 3.75 // Operations with native types Fixed64_16 scaled = a * 2; // 7.5 (integer multiplication) Fixed64_16 divided = a / 2; // 1.875 (integer division) Fixed64_16 added = a + 1.5; // 5.25 (floating-point addition) // Division by zero handling Fixed64_16 zero(0); Fixed64_16 div_zero = a / zero; // Returns Infinity Fixed64_16 neg_div = (-a) / zero; // Returns NegInfinity if (div_zero == Fixed64_16::Infinity()) { std::cout << "Division by zero results in Infinity" << std::endl; } std::cout << "Sum: " << sum << ", Product: " << product << std::endl; return 0; } ``` -------------------------------- ### Fixed64 Min, Max, Clamp, and Abs Functions Source: https://context7.com/skynetnext/fixed64/llms.txt Illustrates utility functions for value manipulation, including Min, Max, Abs, and Clamp. Demonstrates clamping to arbitrary ranges and the specific [0, 1] range, along with the Sign function. ```cpp #include "fixed64.h" #include "fixed64_math.h" using math::fp::Fixed64_16; using math::fp::Fixed64Math; int main() { Fixed64_16 a(5.5); Fixed64_16 b(3.25); Fixed64_16 neg(-7.5); // Min and Max auto minimum = Fixed64Math::Min(a, b); // 3.25 auto maximum = Fixed64Math::Max(a, b); // 5.5 // Absolute value auto abs_neg = Fixed64Math::Abs(neg); // 7.5 auto abs_pos = Fixed64Math::Abs(a); // 5.5 // Clamp to arbitrary range Fixed64_16 value(7.5); Fixed64_16 min_val(2.0); Fixed64_16 max_val(5.0); auto clamped = Fixed64Math::Clamp(value, min_val, max_val); // 5.0 Fixed64_16 low_value(0.5); auto clamped_low = Fixed64Math::Clamp(low_value, min_val, max_val); // 2.0 // Clamp to [0, 1] range (useful for interpolation factors) Fixed64_16 over_one(1.5); Fixed64_16 under_zero(-0.5); auto clamp01_over = Fixed64Math::Clamp01(over_one); // 1.0 auto clamp01_under = Fixed64Math::Clamp01(under_zero); // 0.0 auto clamp01_valid = Fixed64Math::Clamp01(Fixed64_16(0.75)); // 0.75 // Sign function int sign_pos = Fixed64Math::Sign(a); // 1 int sign_neg = Fixed64Math::Sign(neg); // -1 int sign_zero = Fixed64Math::Sign(Fixed64_16::Zero()); // 0 // std:: overloads auto std_abs = std::abs(neg); // 7.5 auto std_fabs = std::fabs(neg); // 7.5 std::cout << "Min: " << minimum << ", Max: " << maximum << std::endl; std::cout << "Clamped(7.5 to [2,5]): " << clamped << std::endl; return 0; } ``` -------------------------------- ### Weighted Random Selection with Fixed64Random Source: https://context7.com/skynetnext/fixed64/llms.txt Demonstrates how to perform weighted random selection from a list of items using provided weights. Includes selecting a random element and a weighted random element from an array. ```cpp // Weighted random selection std::vector weights = { Fixed64_16(1.0), // 10% chance (1/10) Fixed64_16(3.0), // 30% chance (3/10) Fixed64_16(6.0) // 60% chance (6/10) }; int selected_index = rng.randomWeights(weights); // Returns 0, 1, or 2 // Random element from array std::vector items = {100, 200, 300, 400}; int random_item_index = rng.randomArray(items); // Returns 0-3 // Random element with weights std::vector item_weights = { Fixed64_16(1.0), Fixed64_16(2.0), Fixed64_16(3.0), Fixed64_16(4.0) }; int weighted_index = rng.randomArray(items, item_weights); ``` -------------------------------- ### Probability-Based Decisions with Fixed64Random Source: https://context7.com/skynetnext/fixed64/llms.txt Shows how to use the Fixed64Random generator for probability-based decisions using a 0-1 scale or a 0-100 scale. ```cpp // Probability-based decisions (0-1 scale) bool success_01 = rng.result01(Fixed64_16(0.75)); // 75% chance of true // Probability-based decisions (0-100 scale) bool success_100 = rng.result(Fixed64_16(75)); // 75% chance of true ``` -------------------------------- ### Fixed64 Type Declaration and Construction Source: https://context7.com/skynetnext/fixed64/llms.txt Demonstrates how to declare Fixed64 types with specified precision and construct them from various data types including integers, floating-point numbers, and strings. Also shows precision conversion and raw value access. ```cpp #include "fixed64.h" using math::fp::Fixed64; using math::fp::Fixed64_16; using math::fp::Fixed64_32; using math::fp::Fixed64_40; int main() { // Construction from different types Fixed64_16 from_int(42); // From integer: 42.0 Fixed64_16 from_double(3.14159); // From double: 3.14159 Fixed64_16 from_float(2.5f); // From float: 2.5 // Using template directly with custom precision Fixed64<16> a(5.5); // 16 fractional bits Fixed64<32> b(5.5); // 32 fractional bits (higher precision) Fixed64<40> c(5.5); // 40 fractional bits (highest precision) // Precision conversion Fixed64_32 high_precision(3.14159265359); Fixed64_16 medium_precision(high_precision); // Converts precision // String parsing and conversion Fixed64_16 from_string = Fixed64_16::FromString("123.456"); Fixed64_16 scientific = Fixed64_16::FromString("1.5e-3"); // 0.0015 std::string str = from_string.ToString(); // "123.456" // Raw value access for serialization int64_t raw = a.value(); // Get underlying int64_t value std::cout << "from_double: " << from_double << std::endl; // Output: 3.14159 std::cout << "from_string: " << from_string << std::endl; // Output: 123.456 return 0; } ``` -------------------------------- ### Perform Trigonometric Calculations in C++ Source: https://context7.com/skynetnext/fixed64/llms.txt Demonstrates basic sine, cosine, and tangent operations, identity verification, and point rotation using Fixed64_32. ```cpp #include "fixed64.h" #include "fixed64_math.h" using math::fp::Fixed64_32; using math::fp::Fixed64Math; int main() { // Angle in radians Fixed64_32 angle_45 = Fixed64_32::Deg2Rad() * Fixed64_32(45); // π/4 Fixed64_32 angle_90 = Fixed64_32::HalfPi(); // π/2 Fixed64_32 angle_180 = Fixed64_32::Pi(); // π // Sine function auto sin_45 = Fixed64Math::Sin(angle_45); // ~0.70710678 auto sin_90 = Fixed64Math::Sin(angle_90); // ~1.0 auto sin_0 = Fixed64Math::Sin(Fixed64_32::Zero()); // 0.0 // Cosine function auto cos_45 = Fixed64Math::Cos(angle_45); // ~0.70710678 auto cos_90 = Fixed64Math::Cos(angle_90); // ~0.0 auto cos_0 = Fixed64Math::Cos(Fixed64_32::Zero()); // 1.0 // Tangent function auto tan_45 = Fixed64Math::Tan(angle_45); // ~1.0 auto tan_0 = Fixed64Math::Tan(Fixed64_32::Zero()); // 0.0 // Verify identity: sin²(x) + cos²(x) = 1 auto identity = sin_45 * sin_45 + cos_45 * cos_45; // ~1.0 // std:: function overloads auto std_sin = std::sin(angle_45); // ~0.70710678 auto std_cos = std::cos(angle_45); // ~0.70710678 auto std_tan = std::tan(angle_45); // ~1.0 // Practical example: rotating a point Fixed64_32 x(10.0); Fixed64_32 y(0.0); Fixed64_32 rotation = Fixed64_32::Deg2Rad() * Fixed64_32(90); Fixed64_32 new_x = x * Fixed64Math::Cos(rotation) - y * Fixed64Math::Sin(rotation); Fixed64_32 new_y = x * Fixed64Math::Sin(rotation) + y * Fixed64Math::Cos(rotation); // Result: new_x ≈ 0, new_y ≈ 10 std::cout << "Sin(45°): " << sin_45 << std::endl; // Output: ~0.7071 std::cout << "Cos(45°): " << cos_45 << std::endl; // Output: ~0.7071 std::cout << "Tan(45°): " << tan_45 << std::endl; // Output: ~1.0 std::cout << "Rotated point: (" << new_x << ", " << new_y << ")" << std::endl; return 0; } ``` -------------------------------- ### Apply Mathematical Functions to Fixed64 Source: https://github.com/skynetnext/fixed64/blob/main/docs/examples.md Covers rounding, clamping, and basic mathematical operations like square root using the Fixed64Math utility class. ```cpp #include "fixed64.h" #include "fixed64_math.h" using Fixed = math::fp::Fixed64<32>; using math::fp::Fixed64Math; int main() { Fixed value(3.75); // Rounding operations Fixed floor = Fixed64Math::Floor(value); // 3.0 Fixed ceil = Fixed64Math::Ceil(value); // 4.0 Fixed round = Fixed64Math::Round(value); // 4.0 // Min/Max and clamping Fixed min = Fixed64Math::Min(Fixed(1.5), Fixed(0.5)); // 0.5 Fixed clamp = Fixed64Math::Clamp(value, Fixed(2.0), Fixed(3.5)); // 3.5 // Square root and exponential Fixed sqrt = Fixed64Math::Sqrt(Fixed(16.0)); // 4.0 std::cout << "Square root of 16: " << sqrt << std::endl; return 0; } ``` -------------------------------- ### Generate Random Numbers with Fixed64Random Source: https://context7.com/skynetnext/fixed64/llms.txt Illustrates various methods for generating random numbers within specified ranges, including [0, 1), [0, max), and [min, max). Also shows integer generation. ```cpp // Random number in [0, 1) auto rand_01 = rng.random(); // e.g., 0.7342... // Random number in [0, max) auto rand_max = rng.random(Fixed64_16(100)); // e.g., 73.42... // Random number in [min, max) auto rand_range = rng.random(Fixed64_16(10), Fixed64_16(20)); // e.g., 17.34... // Random integer in [0, max) int32_t rand_int = rng.randomInteger(Fixed64_16(100)); // e.g., 73 // Random integer in [min, max) int32_t rand_int_range = rng.randomInteger(Fixed64_16(10), Fixed64_16(20)); // e.g., 17 ``` -------------------------------- ### Fixed64 Cross-Precision Operations and Conversions Source: https://context7.com/skynetnext/fixed64/llms.txt Illustrates creating Fixed64 values with different precisions, explicit conversions between them, arithmetic operations where the result precision is determined by the left operand, and comparisons across precisions which automatically convert to the higher precision. Also shows clamped casting with range checking. ```cpp #include "fixed64.h" #include "fixed64_math.h" using math::fp::Fixed64; using math::fp::Fixed64_16; using math::fp::Fixed64_32; using math::fp::Fixed64_40; using math::fp::Fixed64Math; int main() { // Create values with different precisions Fixed64_16 low_prec(3.14159); // Q47.16 - wider range, lower precision Fixed64_32 mid_prec(3.14159); // Q31.32 - balanced Fixed64_40 high_prec(3.14159); // Q23.40 - narrower range, higher precision // Explicit precision conversion Fixed64_32 converted = Fixed64_32(low_prec); // Convert 16-bit to 32-bit Fixed64_16 downgraded = Fixed64_16(mid_prec); // Convert 32-bit to 16-bit // Arithmetic between different precisions Fixed64_16 a(5.5); Fixed64_32 b(2.25); // Addition with different precisions (result has precision of left operand) Fixed64_16 sum_16 = a + Fixed64_16(b); // Convert b to 16-bit first Fixed64_32 sum_32 = Fixed64_32(a) + b; // Convert a to 32-bit first // Comparison across precisions (automatic conversion) bool equal = (low_prec == mid_prec); // Compares at higher precision bool less = (low_prec < high_prec); // Clamped cast with range checking double large_value = 1e15; auto clamped = Fixed64Math::ClampedCast(large_value); // Returns Max() if value exceeds representable range auto clamped_small = Fixed64Math::ClampedCast(-1e15); // Returns Min() if value is below representable range // Precision trade-offs demonstration // Fixed64_16: Range ±1.4×10¹⁴, Precision 1.5×10⁻⁵ // Fixed64_32: Range ±2.1×10⁹, Precision 2.3×10⁻¹⁰ // Fixed64_40: Range ±8.3×10⁶, Precision 9.1×10⁻¹³ Fixed64_16 large_range(1e10); // Works in Fixed64_16 Fixed64_40 small_range(1e10); // May overflow in Fixed64_40 Fixed64_40 precise(0.000000001); // Representable in Fixed64_40 Fixed64_16 less_precise(0.000000001); // Rounds in Fixed64_16 std::cout << "Low precision (16): " << low_prec << std::endl; std::cout << "High precision (40): " << high_prec << std::endl; std::cout << "Equal across precision: " << equal << std::endl; return 0; } ``` -------------------------------- ### Configure Fixed64 Benchmark Build Source: https://github.com/skynetnext/fixed64/blob/main/benchmarks/CMakeLists.txt Defines the source files and executable target for the benchmark suite. Requires the Fixed64 library and SoftFloat headers to be available. ```cmake set(BENCHMARK_SOURCES benchmark_utils.h basic_operations_benchmark.h basic_operations_benchmark.cpp advanced_math_benchmark.h advanced_math_benchmark.cpp benchmark_main.cpp ) # Main benchmark executable add_executable(fixed64_benchmark ${BENCHMARK_SOURCES}) target_include_directories(fixed64_benchmark PRIVATE ${PROJECT_SOURCE_DIR}/include ${SOFTFLOAT_INCLUDES} ) target_link_libraries(fixed64_benchmark PRIVATE Fixed64 ) # Use precise trigonometric functions add_compile_definitions(FIXED64_MATH_USE_FAST_TRIG=0) # Add compiler options for benchmarks target_compile_options(fixed64_benchmark PRIVATE ${COMPILER_WARNINGS}) ``` -------------------------------- ### Execute Trigonometric Functions Source: https://github.com/skynetnext/fixed64/blob/main/docs/examples.md Demonstrates trigonometric calculations and angle conversions using Fixed64 types. ```cpp #include "fixed64.h" #include "fixed64_math.h" using Fixed = math::fp::Fixed64<32>; using math::fp::Fixed64Math; int main() { // Calculate trigonometric functions at π/4 Fixed angle = Fixed::Pi() / Fixed(4); Fixed sinValue = Fixed64Math::Sin(angle); // ~0.7071 Fixed cosValue = Fixed64Math::Cos(angle); // ~0.7071 Fixed tanValue = Fixed64Math::Tan(angle); // ~1.0 // Inverse trigonometric function Fixed atan2Value = Fixed64Math::Atan2(Fixed(1.0), Fixed(1.0)); // π/4 // Angle conversion Fixed degrees = Fixed(45); Fixed radians = degrees * Fixed::Pi() / Fixed(180); std::cout << "Sin(π/4): " << sinValue << std::endl; return 0; } ``` -------------------------------- ### Fixed64 Rounding Functions Source: https://context7.com/skynetnext/fixed64/llms.txt Demonstrates various rounding operations like Floor, Ceil, Round, Truncate, and Fractions for Fixed64_16 type. Includes standard library overloads for common math functions. ```cpp #include "fixed64.h" #include "fixed64_math.h" using math::fp::Fixed64_16; using math::fp::Fixed64Math; int main() { Fixed64_16 positive(3.7); Fixed64_16 negative(-3.7); Fixed64_16 half_pos(2.5); Fixed64_16 half_neg(-2.5); // Floor - rounds toward negative infinity auto floor_pos = Fixed64Math::Floor(positive); // 3.0 auto floor_neg = Fixed64Math::Floor(negative); // -4.0 // Ceiling - rounds toward positive infinity auto ceil_pos = Fixed64Math::Ceil(positive); // 4.0 auto ceil_neg = Fixed64Math::Ceil(negative); // -3.0 // Round - rounds to nearest, ties away from zero auto round_pos = Fixed64Math::Round(positive); // 4.0 auto round_neg = Fixed64Math::Round(negative); // -4.0 auto round_half = Fixed64Math::Round(half_pos); // 3.0 (away from zero) auto round_half_neg = Fixed64Math::Round(half_neg); // -3.0 (away from zero) // Truncate - rounds toward zero auto trunc_pos = Fixed64Math::Trunc(positive); // 3.0 auto trunc_neg = Fixed64Math::Trunc(negative); // -3.0 // Extract fractional part auto frac_pos = Fixed64Math::Fractions(positive); // 0.7 auto frac_neg = Fixed64Math::Fractions(negative); // 0.3 (always positive) // std:: function overloads also available auto std_floor = std::floor(positive); // 3.0 auto std_ceil = std::ceil(positive); // 4.0 auto std_round = std::round(positive); // 4.0 auto std_trunc = std::trunc(positive); // 3.0 std::cout << "Floor(3.7): " << floor_pos << std::endl; // Output: 3 std::cout << "Floor(-3.7): " << floor_neg << std::endl; // Output: -4 std::cout << "Trunc(-3.7): " << trunc_neg << std::endl; // Output: -3 return 0; } ``` -------------------------------- ### Fixed64 Predefined Constants Source: https://context7.com/skynetnext/fixed64/llms.txt Illustrates the usage of various predefined mathematical constants and special values provided by Fixed64 for different precision levels. These constants are computed at compile-time. ```cpp #include "fixed64.h" using math::fp::Fixed64_32; int main() { // Integer constants auto zero = Fixed64_32::Zero(); // 0 auto one = Fixed64_32::One(); // 1 auto two = Fixed64_32::Two(); // 2 auto hundred = Fixed64_32::Hundred(); // 100 auto thousand = Fixed64_32::Thousand(); // 1000 // Fractional constants auto half = Fixed64_32::Half(); // 0.5 auto quarter = Fixed64_32::Quarter(); // 0.25 auto point1 = Fixed64_32::Point1(); // 0.1 auto neg_one = Fixed64_32::NegOne(); // -1 // Mathematical constants auto pi = Fixed64_32::Pi(); // 3.14159265358979... auto two_pi = Fixed64_32::TwoPi(); // 6.28318530717958... auto half_pi = Fixed64_32::HalfPi(); // 1.57079632679489... auto quarter_pi = Fixed64_32::QuarterPi();// 0.78539816339744... auto inv_pi = Fixed64_32::InvPi(); // 0.31830988618379... (1/π) auto e = Fixed64_32::E(); // 2.71828182845904... auto ln2 = Fixed64_32::Ln2(); // 0.69314718055994... auto log2e = Fixed64_32::Log2E(); // 1.44269504088896... // Angle conversion constants auto deg2rad = Fixed64_32::Deg2Rad(); // π/180 auto rad2deg = Fixed64_32::Rad2Deg(); // 180/π // Special values auto epsilon = Fixed64_32::Epsilon(); // Smallest representable increment auto max_val = Fixed64_32::Max(); // Maximum representable value auto min_val = Fixed64_32::Min(); // Minimum representable value auto infinity = Fixed64_32::Infinity(); // Positive infinity auto neg_inf = Fixed64_32::NegInfinity(); // Negative infinity auto nan = Fixed64_32::NaN(); // Not a number std::cout << "Pi: " << pi << std::endl; // Output: 3.1415926535... std::cout << "E: " << e << std::endl; // Output: 2.7182818284... return 0; } ``` -------------------------------- ### Perform Inverse Trigonometric Calculations in C++ Source: https://context7.com/skynetnext/fixed64/llms.txt Demonstrates arc sine, arc cosine, arc tangent, and two-argument arc tangent (atan2) functions, including quadrant handling. ```cpp #include "fixed64.h" #include "fixed64_math.h" using math::fp::Fixed64_32; using math::fp::Fixed64Math; int main() { // Arc cosine (acos) - returns angle in [0, π] auto acos_0 = Fixed64Math::Acos(Fixed64_32::Zero()); // π/2 (~1.5708) auto acos_1 = Fixed64Math::Acos(Fixed64_32::One()); // 0.0 auto acos_neg1 = Fixed64Math::Acos(-Fixed64_32::One()); // π (~3.1416) auto acos_half = Fixed64Math::Acos(Fixed64_32::Half()); // π/3 (~1.0472) // Arc sine (asin) - returns angle in [-π/2, π/2] auto asin_0 = Fixed64Math::Asin(Fixed64_32::Zero()); // 0.0 auto asin_1 = Fixed64Math::Asin(Fixed64_32::One()); // π/2 (~1.5708) auto asin_neg1 = Fixed64Math::Asin(-Fixed64_32::One()); // -π/2 (~-1.5708) // Arc tangent (atan) - returns angle in [-π/2, π/2] auto atan_0 = Fixed64Math::Atan(Fixed64_32::Zero()); // 0.0 auto atan_1 = Fixed64Math::Atan(Fixed64_32::One()); // π/4 (~0.7854) auto atan_neg1 = Fixed64Math::Atan(-Fixed64_32::One()); // -π/4 (~-0.7854) // Two-argument arc tangent (atan2) - returns angle in [-π, π] Fixed64_32 y(1.0); Fixed64_32 x(1.0); auto atan2_result = Fixed64Math::Atan2(y, x); // π/4 (~0.7854) // Convert atan2 result to degrees auto angle_degrees = atan2_result * Fixed64_32::Rad2Deg(); // 45.0 // Atan2 quadrant handling auto q1 = Fixed64Math::Atan2(Fixed64_32(1), Fixed64_32(1)); // π/4 (first quadrant) auto q2 = Fixed64Math::Atan2(Fixed64_32(1), Fixed64_32(-1)); // 3π/4 (second quadrant) auto q3 = Fixed64Math::Atan2(Fixed64_32(-1), Fixed64_32(-1)); // -3π/4 (third quadrant) auto q4 = Fixed64Math::Atan2(Fixed64_32(-1), Fixed64_32(1)); // -π/4 (fourth quadrant) // Special cases auto atan2_y_only = Fixed64Math::Atan2(Fixed64_32::One(), Fixed64_32::Zero()); // π/2 auto atan2_origin = Fixed64Math::Atan2(Fixed64_32::Zero(), Fixed64_32::Zero()); // 0 (by convention) // std:: overloads auto std_asin = std::asin(Fixed64_32::Half()); // π/6 auto std_acos = std::acos(Fixed64_32::Half()); // π/3 auto std_atan = std::atan(Fixed64_32::One()); // π/4 auto std_atan2 = std::atan2(y, x); // π/4 std::cout << "Acos(0): " << acos_0 << std::endl; // Output: ~1.5708 std::cout << "Atan2(1,1) in degrees: " << angle_degrees << std::endl; // Output: 45 return 0; } ``` -------------------------------- ### Utilize Advanced Fixed64 Features Source: https://github.com/skynetnext/fixed64/blob/main/docs/examples.md Shows interpolation, precision conversion between different Fixed64 types, and integration with standard library functions. ```cpp #include "fixed64.h" #include "fixed64_math.h" #include using Fixed16 = math::fp::Fixed64<16>; using Fixed32 = math::fp::Fixed64<32>; using math::fp::Fixed64Math; int main() { // Interpolation Fixed16 lerp = Fixed64Math::Lerp(Fixed16(2), Fixed16(3), Fixed16(0.5)); // 2.5 // Precision conversion Fixed32 highPrecision(3.14159265359); Fixed16 mediumPrecision(highPrecision); // Converts precision // Standard library integration Fixed16 value(-5.5); Fixed16 abs = std::abs(value); // 5.5 // Special values Fixed16 inf = Fixed16::Infinity(); bool isInf = std::isinf(inf); // true std::cout << "Interpolated value: " << lerp << std::endl; return 0; } ```