numeric_limits ============== .. cpp:class:: numpy::numeric_limits numpy C++ class. Example ------- .. code-block:: cpp #include using namespace numpy; // Use numeric_limits numeric_limits obj; // ... operations ... Statistics ---------- .. list-table:: :widths: 40 20 15 25 :header-rows: 1 * - Signature - Return Type - Location - Example * - ``numpy::float16 max()`` - numpy::float16 - NP_FLOAT16.H:304 - :ref:`View ` * - ``numpy::float16 min()`` - numpy::float16 - NP_FLOAT16.H:303 - :ref:`View ` Math Operations --------------- .. list-table:: :widths: 40 20 15 25 :header-rows: 1 * - Signature - Return Type - Location - Example * - ``numpy::float16 round_error()`` - numpy::float16 - NP_FLOAT16.H:307 - Other Methods ------------- .. list-table:: :widths: 40 20 15 25 :header-rows: 1 * - Signature - Return Type - Location - Example * - ``numpy::float16 denorm_min()`` - numpy::float16 - NP_FLOAT16.H:311 - * - ``numpy::float16 epsilon()`` - numpy::float16 - NP_FLOAT16.H:306 - :ref:`View ` * - ``numpy::float16 infinity()`` - numpy::float16 - NP_FLOAT16.H:308 - :ref:`View ` * - ``numpy::float16 lowest()`` - numpy::float16 - NP_FLOAT16.H:305 - :ref:`View ` * - ``numpy::float16 quiet_NaN()`` - numpy::float16 - NP_FLOAT16.H:309 - :ref:`View ` * - ``numpy::float16 signaling_NaN()`` - numpy::float16 - NP_FLOAT16.H:310 - Code Examples ------------- The following examples are extracted from the test suite. .. _example-numeric_limits-max-0: .. dropdown:: max (np_test_1_all.cpp:7274) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 7264 :emphasize-lines: 11 if (sizeof(uintp) == sizeof(void*)) { // std::cout << " -> uintp size matches pointer size"; } else { // std::cout << " ✗ uintp size doesn't match pointer size" << std::endl; } // Test range limits // std::cout << "Range Information:" << std::endl; // std::cout << " intp min: " << std::numeric_limits::min() << std::endl; // std::cout << " intp max: " << std::numeric_limits::max() << std::endl; // std::cout << " uintp max: " << std::numeric_limits::max() << std::endl; // std::cout << " longdouble digits: " << std::numeric_limits::digits << std::endl; std::cout << " -> tests passed" << std::endl; } void testComplexArithmeticExtendedTypes() { std::cout << "========= testComplexArithmeticExtendedTypes ======================="; clongdouble c1(3.0L, 4.0L); // 3 + 4i .. _example-numeric_limits-min-1: .. dropdown:: min (np_test_1_all.cpp:2350) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 2340 :emphasize-lines: 11 if (i % 3 == 0) { large_array.setElementAt({i}, object_(static_cast(i))); } else if (i % 3 == 1) { large_array.setElementAt({i}, object_(static_cast(i) * 0.5)); } else { large_array.setElementAt({i}, object_(std::string("str") + std::to_string(i))); } } // Verify pattern for (size_t i = 0; i < std::min(large_size, size_t(100)); ++i) { // Check first 100 object_ obj = large_array.getElementAt({i}); if (i % 3 == 0) { if (!(obj.is_type())) { std::string description = std::string("testArrayEdgeCases():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(obj.is_type())"; std::cout << std::string("[FAIL] ") + description << std::endl; throw std::runtime_error(description); } } else if (i % 3 == 1) { if (!(obj.is_type())) { std::string description = std::string("unknown_function():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(obj.is_type())"; .. _example-numeric_limits-epsilon-2: .. dropdown:: epsilon (np_test_1_all.cpp:7822) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 7812 :emphasize-lines: 11 // std::cout << " float16: " << sizeof(float16) << " bytes" << std::endl; // std::cout << " float32: " << sizeof(float32) << " bytes" << std::endl; // std::cout << " float64: " << sizeof(float64) << " bytes" << std::endl; // std::cout << " float128: " << sizeof(float128) << " bytes" << std::endl; // Test numeric limits // std::cout << "Numeric limits:" << std::endl; // std::cout << " min: " << std::numeric_limits::min() << std::endl; // std::cout << " max: " << std::numeric_limits::max() << std::endl; // std::cout << " lowest: " << std::numeric_limits::lowest() << std::endl; // std::cout << " epsilon: " << std::numeric_limits::epsilon() << std::endl; // std::cout << " digits: " << std::numeric_limits::digits << std::endl; // std::cout << " digits10: " << std::numeric_limits::digits10 << std::endl; std::cout << " -> tests passed" << std::endl; } void testFloat16BitPatternsFloat16() { std::cout << "========= testFloat16BitPatternsFloat16 ======================="; // Test some known bit patterns .. _example-numeric_limits-infinity-3: .. dropdown:: infinity (np_test_1_all.cpp:1067) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 1057 :emphasize-lines: 11 std::cout << " -> tests passed" << std::endl; } void testSpecialValueHandling() { std::cout << "========= testSpecialValueHandling ======================="; auto arr = createFloat64Array({4}); arr.setElementAt({0}, 1.0); arr.setElementAt({1}, std::numeric_limits::quiet_NaN()); arr.setElementAt({2}, std::numeric_limits::infinity()); arr.setElementAt({3}, -std::numeric_limits::infinity()); auto scalar_arr = createFloat64Array({4}); scalar_arr.setElementAt({0}, 2.0); scalar_arr.setElementAt({1}, 1.0); scalar_arr.setElementAt({2}, 1.0); scalar_arr.setElementAt({3}, 1.0); // Test NaN behavior - NaN comparisons should be false auto gt_result = greater(arr, scalar_arr); .. _example-numeric_limits-lowest-4: .. dropdown:: lowest (np_test_1_all.cpp:7821) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 7811 :emphasize-lines: 11 // std::cout << "Size comparison:" << std::endl; // std::cout << " float16: " << sizeof(float16) << " bytes" << std::endl; // std::cout << " float32: " << sizeof(float32) << " bytes" << std::endl; // std::cout << " float64: " << sizeof(float64) << " bytes" << std::endl; // std::cout << " float128: " << sizeof(float128) << " bytes" << std::endl; // Test numeric limits // std::cout << "Numeric limits:" << std::endl; // std::cout << " min: " << std::numeric_limits::min() << std::endl; // std::cout << " max: " << std::numeric_limits::max() << std::endl; // std::cout << " lowest: " << std::numeric_limits::lowest() << std::endl; // std::cout << " epsilon: " << std::numeric_limits::epsilon() << std::endl; // std::cout << " digits: " << std::numeric_limits::digits << std::endl; // std::cout << " digits10: " << std::numeric_limits::digits10 << std::endl; std::cout << " -> tests passed" << std::endl; } void testFloat16BitPatternsFloat16() { std::cout << "========= testFloat16BitPatternsFloat16 ======================="; .. _example-numeric_limits-quiet_nan-5: .. dropdown:: quiet_NaN (np_test_1_all.cpp:1066) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 1056 :emphasize-lines: 11 // std::cout << "[OK] equal and not_equal functions" << std::endl; std::cout << " -> tests passed" << std::endl; } void testSpecialValueHandling() { std::cout << "========= testSpecialValueHandling ======================="; auto arr = createFloat64Array({4}); arr.setElementAt({0}, 1.0); arr.setElementAt({1}, std::numeric_limits::quiet_NaN()); arr.setElementAt({2}, std::numeric_limits::infinity()); arr.setElementAt({3}, -std::numeric_limits::infinity()); auto scalar_arr = createFloat64Array({4}); scalar_arr.setElementAt({0}, 2.0); scalar_arr.setElementAt({1}, 1.0); scalar_arr.setElementAt({2}, 1.0); scalar_arr.setElementAt({3}, 1.0); // Test NaN behavior - NaN comparisons should be false