FloatingDtype ============= .. cpp:class:: numpy::FloatingDtype numpy C++ class. Example ------- .. code-block:: cpp #include using namespace numpy; // Use FloatingDtype FloatingDtype obj; // ... operations ... Constructors ------------ .. list-table:: :widths: 55 25 20 :header-rows: 1 * - Signature - Location - Example * - ``FloatingDtype() = default`` - df_floating_dtype.h:31 - Indexing / Selection -------------------- .. list-table:: :widths: 40 20 15 25 :header-rows: 1 * - Signature - Return Type - Location - Example * - ``size_t itemsize() const override`` - size_t - df_floating_dtype.h:53 - :ref:`View ` Statistics ---------- .. list-table:: :widths: 40 20 15 25 :header-rows: 1 * - Signature - Return Type - Location - Example * - ``T max_value() const`` - T - df_floating_dtype.h:82 - * - ``T min_value() const`` - T - df_floating_dtype.h:75 - Type Checking ------------- .. list-table:: :widths: 40 20 15 25 :header-rows: 1 * - Signature - Return Type - Location - Example * - ``static bool is_finite(T value)`` - static bool - df_floating_dtype.h:96 - * - ``static bool is_inf(T value)`` - static bool - df_floating_dtype.h:110 - * - ``static bool is_nan(T value)`` - static bool - df_floating_dtype.h:103 - Other Methods ------------- .. list-table:: :widths: 40 20 15 25 :header-rows: 1 * - Signature - Return Type - Location - Example * - ``T epsilon() const`` - T - df_floating_dtype.h:89 - :ref:`View ` * - ``std::string kind() const override`` - std::string - df_floating_dtype.h:61 - * - ``bool matches(const std::string& dtype_name) const`` - bool - df_floating_dtype.h:119 - :ref:`View ` * - ``std::string name() const override`` - std::string - df_floating_dtype.h:38 - :ref:`View ` * - ``numpy::DType numpy_dtype() const override`` - numpy::DType - df_floating_dtype.h:46 - * - ``const std::type_info& type() const override`` - const std::type_info& - df_floating_dtype.h:68 - :ref:`View ` Code Examples ------------- The following examples are extracted from the test suite. .. _example-floatingdtype-itemsize-0: .. dropdown:: itemsize (np_test_2_all.cpp:3540) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 3530 :emphasize-lines: 11 } // Test size if (!(size(arr) == 24)) { std::string description = std::string("testArrayInfo():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(size(arr) == 24)"; std::cout << std::string("[FAIL] ") + description << std::endl; throw std::runtime_error(description); } // Test itemsize if (!(itemsize(arr) == sizeof(double))) { std::string description = std::string("testArrayInfo():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(itemsize(arr) == sizeof(double))"; std::cout << std::string("[FAIL] ") + description << std::endl; throw std::runtime_error(description); } // Test nbytes if (!(nbytes(arr) == 24 * sizeof(double))) { std::string description = std::string("testArrayInfo():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(nbytes(arr) == 24 * sizeof(double))"; std::cout << std::string("[FAIL] ") + description << std::endl; throw std::runtime_error(description); .. _example-floatingdtype-epsilon-1: .. 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-floatingdtype-matches-2: .. dropdown:: matches (np_test_1_all.cpp:26699) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 26689 :emphasize-lines: 11 std::cout << std::string("[FAIL] ") + description << std::endl; throw std::runtime_error(description); } // std::cout << " ✓ load_library() (stub)" << std::endl; } // Test ndpointer { auto ptr_desc = numpy::ctypeslib::ndpointer(2, { 10, 10 }); auto arr = NDArray({ 10, 10 }, 0.0); bool matches = ptr_desc.matches(arr); if (!(matches)) { std::string description = std::string("test_ctypes_interop():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(matches)"; std::cout << std::string("[FAIL] ") + description << std::endl; throw std::runtime_error(description); } // std::cout << " ✓ ndpointer()" << std::endl; } // std::cout << " [PASS] Sub-phase 6D complete (4 functions tested)" << std::endl; } .. _example-floatingdtype-name-3: .. dropdown:: name (np_test_1_all.cpp:24865) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 24855 :emphasize-lines: 11 uint64_t before_restore = rng.next_uint64(); rng.set_state(state); uint64_t after_restore = rng.next_uint64(); if (before_restore != after_restore) { std::cout << " [FAIL] : in np_test_bitgen_mt19937() : state restore failed"; throw std::runtime_error("np_test_bitgen_mt19937 failed: state"); } // Test name if (rng.name() != "MT19937") { std::cout << " [FAIL] : in np_test_bitgen_mt19937() : incorrect name"; throw std::runtime_error("np_test_bitgen_mt19937 failed: name"); } std::cout << " -> tests passed" << std::endl; } // ============================================================================ // PCG64 TESTS // ============================================================================ .. _example-floatingdtype-type-4: .. dropdown:: type (np_test_1_all.cpp:2240) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 2230 :emphasize-lines: 11 std::string description = std::string("testArrayEdgeCases():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(multi_array.getElementAt({0, 1}).is_type())"; std::cout << std::string("[FAIL] ") + description << std::endl; throw std::runtime_error(description); } if (!(multi_array.getElementAt({0, 2}).is_type())) { std::string description = std::string("testArrayEdgeCases():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(multi_array.getElementAt({0, 2}).is_type())"; std::cout << std::string("[FAIL] ") + description << std::endl; throw std::runtime_error(description); } // Test 4: Arrays with all same type (homogeneous) NDArray homo_array = createObjectZeros({5}); for (size_t i = 0; i < 5; ++i) { homo_array.setElementAt({i}, object_(static_cast(i * 10))); } // Verify all are same type for (size_t i = 0; i < 5; ++i) { object_ obj = homo_array.getElementAt({i}); if (!(obj.is_type())) { std::string description = std::string("testArrayEdgeCases():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(obj.is_type())";