float16 ======= .. cpp:class:: numpy::float16 numpy C++ class. Example ------- .. code-block:: cpp #include using namespace numpy; // Use float16 float16 obj; // ... operations ... Constructors ------------ .. list-table:: :widths: 55 25 20 :header-rows: 1 * - Signature - Location - Example * - ``float16(floatvalue)`` - NP_FLOAT16.H:33 - :ref:`View ` * - ``float16(doublevalue)`` - NP_FLOAT16.H:35 - :ref:`View ` * - ``float16(intvalue)`` - NP_FLOAT16.H:37 - :ref:`View ` Operators --------- .. list-table:: :widths: 40 25 15 20 :header-rows: 1 * - Signature - Return Type - Location - Example * - ``operator float()`` - - NP_FLOAT16.H:47 - * - ``operator double()`` - - NP_FLOAT16.H:49 - * - ``float16 operator+(const float16 &other)`` - float16 - NP_FLOAT16.H:52 - * - ``float16 operator-(const float16 &other)`` - float16 - NP_FLOAT16.H:56 - * - ``float16 operator\*(const float16 &other)`` - float16 - NP_FLOAT16.H:60 - * - ``float16 operator/(const float16 &other)`` - float16 - NP_FLOAT16.H:64 - * - ``float16 & operator+=(const float16 &other)`` - float16 & - NP_FLOAT16.H:69 - * - ``float16 & operator-=(const float16 &other)`` - float16 & - NP_FLOAT16.H:74 - * - ``float16 & operator\*=(const float16 &other)`` - float16 & - NP_FLOAT16.H:79 - * - ``float16 & operator/=(const float16 &other)`` - float16 & - NP_FLOAT16.H:84 - * - ``bool operator==(const float16 &other)`` - bool - NP_FLOAT16.H:90 - * - ``bool operator!=(const float16 &other)`` - bool - NP_FLOAT16.H:96 - * - ``bool operator<(const float16 &other)`` - bool - NP_FLOAT16.H:100 - * - ``bool operator<=(const float16 &other)`` - bool - NP_FLOAT16.H:104 - * - ``bool operator>(const float16 &other)`` - bool - NP_FLOAT16.H:108 - * - ``bool operator>=(const float16 &other)`` - bool - NP_FLOAT16.H:112 - * - ``float16 operator-()`` - float16 - NP_FLOAT16.H:117 - * - ``float16 operator+()`` - float16 - NP_FLOAT16.H:121 - Construction ------------ .. list-table:: :widths: 40 20 15 25 :header-rows: 1 * - Signature - Return Type - Location - Example * - ``float16 from_bits(uint16_tbits)`` - float16 - NP_FLOAT16.H:40 - :ref:`View ` Logical ------- .. list-table:: :widths: 40 20 15 25 :header-rows: 1 * - Signature - Return Type - Location - Example * - ``bool isfinite()`` - bool - NP_FLOAT16.H:151 - :ref:`View ` * - ``bool isinf()`` - bool - NP_FLOAT16.H:145 - :ref:`View ` * - ``bool isnan()`` - bool - NP_FLOAT16.H:139 - :ref:`View ` Other Methods ------------- .. list-table:: :widths: 40 20 15 25 :header-rows: 1 * - Signature - Return Type - Location - Example * - ``float float16_to_float32(uint16_tvalue)`` - float - NP_FLOAT16.H:226 - * - ``uint16_t float32_to_float16(floatvalue)`` - uint16_t - NP_FLOAT16.H:164 - * - ``float16 infinity()`` - float16 - NP_FLOAT16.H:126 - :ref:`View ` * - ``float16 quiet_NaN()`` - float16 - NP_FLOAT16.H:130 - :ref:`View ` * - ``uint16_t raw()`` - uint16_t - NP_FLOAT16.H:160 - :ref:`View ` * - ``float16 signaling_NaN()`` - float16 - NP_FLOAT16.H:134 - * - ``bool signbit()`` - bool - NP_FLOAT16.H:156 - :ref:`View ` Code Examples ------------- The following examples are extracted from the test suite. .. _example-float16-float16-0: .. dropdown:: float16 (np_test_1_all.cpp:7666) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 7656 :emphasize-lines: 11 // std::cout << " float128: " << sizeof(float128) << " bytes" << std::endl; std::cout << " -> tests passed" << std::endl; } // Float16 Functions (merged from np_test_float16.cpp) void testFloat16CreationFloat16() { std::cout << "========= testFloat16CreationFloat16 ======================="; // Test basic array creation auto float16_array = createFloat16Array({2, 2}, float16(3.14f)); // std::cout << "Float16 Array (2x2, filled with pi):"; //float16_array.printArray(); // std::cout << "Float16 size: " << sizeof(float16) << " bytes" << std::endl; // Test different constructors float16 from_float(1.5f); float16 from_double(2.5); float16 from_int(42); // std::cout << "From float: " << from_float << std::endl; .. _example-float16-float16-1: .. dropdown:: float16 (np_test_1_all.cpp:7666) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 7656 :emphasize-lines: 11 // std::cout << " float128: " << sizeof(float128) << " bytes" << std::endl; std::cout << " -> tests passed" << std::endl; } // Float16 Functions (merged from np_test_float16.cpp) void testFloat16CreationFloat16() { std::cout << "========= testFloat16CreationFloat16 ======================="; // Test basic array creation auto float16_array = createFloat16Array({2, 2}, float16(3.14f)); // std::cout << "Float16 Array (2x2, filled with pi):"; //float16_array.printArray(); // std::cout << "Float16 size: " << sizeof(float16) << " bytes" << std::endl; // Test different constructors float16 from_float(1.5f); float16 from_double(2.5); float16 from_int(42); // std::cout << "From float: " << from_float << std::endl; .. _example-float16-float16-2: .. dropdown:: float16 (np_test_1_all.cpp:7666) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 7656 :emphasize-lines: 11 // std::cout << " float128: " << sizeof(float128) << " bytes" << std::endl; std::cout << " -> tests passed" << std::endl; } // Float16 Functions (merged from np_test_float16.cpp) void testFloat16CreationFloat16() { std::cout << "========= testFloat16CreationFloat16 ======================="; // Test basic array creation auto float16_array = createFloat16Array({2, 2}, float16(3.14f)); // std::cout << "Float16 Array (2x2, filled with pi):"; //float16_array.printArray(); // std::cout << "Float16 size: " << sizeof(float16) << " bytes" << std::endl; // Test different constructors float16 from_float(1.5f); float16 from_double(2.5); float16 from_int(42); // std::cout << "From float: " << from_float << std::endl; .. _example-float16-from_bits-3: .. dropdown:: from_bits (np_test_1_all.cpp:7833) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 7823 :emphasize-lines: 11 // 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 float16 zero = float16::from_bits(0x0000); float16 one = float16::from_bits(0x3C00); float16 two = float16::from_bits(0x4000); float16 half = float16::from_bits(0x3800); // std::cout << "Zero (0x0000): " << zero << std::endl; // std::cout << "One (0x3C00): " << one << std::endl; // std::cout << "Two (0x4000): " << two << std::endl; // std::cout << "Half (0x3800): " << half << std::endl; // Test raw bit access .. _example-float16-isfinite-4: .. dropdown:: isfinite (np_test_1_all.cpp:7769) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 7759 :emphasize-lines: 11 float16 zero(0.0f); // std::cout << "Positive infinity: " << pos_inf << std::endl; // std::cout << "Negative infinity: " << neg_inf << std::endl; // std::cout << "Quiet NaN: " << qnan << std::endl; // std::cout << "Zero: " << zero << std::endl; // std::cout << "Is pos_inf infinite? " << pos_inf.isinf() << std::endl; // std::cout << "Is neg_inf infinite? " << neg_inf.isinf() << std::endl; // std::cout << "Is qnan NaN? " << qnan.isnan() << std::endl; // std::cout << "Is zero finite? " << zero.isfinite() << std::endl; // Test NaN comparison behavior // std::cout << "NaN == NaN: " << (qnan == qnan) << std::endl; // Should be false // std::cout << "NaN != NaN: " << (qnan != qnan) << std::endl; // Should be true // std::cout << " -> tests passed" << std::endl; } void testFloat16PrecisionFloat16() { std::cout << "========= testFloat16PrecisionFloat16 ======================="; .. _example-float16-isinf-5: .. dropdown:: isinf (np_test_1_all.cpp:7766) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 7756 :emphasize-lines: 11 float16 pos_inf = float16::infinity(); float16 neg_inf = -float16::infinity(); float16 qnan = float16::quiet_NaN(); float16 zero(0.0f); // std::cout << "Positive infinity: " << pos_inf << std::endl; // std::cout << "Negative infinity: " << neg_inf << std::endl; // std::cout << "Quiet NaN: " << qnan << std::endl; // std::cout << "Zero: " << zero << std::endl; // std::cout << "Is pos_inf infinite? " << pos_inf.isinf() << std::endl; // std::cout << "Is neg_inf infinite? " << neg_inf.isinf() << std::endl; // std::cout << "Is qnan NaN? " << qnan.isnan() << std::endl; // std::cout << "Is zero finite? " << zero.isfinite() << std::endl; // Test NaN comparison behavior // std::cout << "NaN == NaN: " << (qnan == qnan) << std::endl; // Should be false // std::cout << "NaN != NaN: " << (qnan != qnan) << std::endl; // Should be true // std::cout << " -> tests passed" << std::endl; } .. _example-float16-isnan-6: .. dropdown:: isnan (np_test_1_all.cpp:7768) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 7758 :emphasize-lines: 11 float16 qnan = float16::quiet_NaN(); float16 zero(0.0f); // std::cout << "Positive infinity: " << pos_inf << std::endl; // std::cout << "Negative infinity: " << neg_inf << std::endl; // std::cout << "Quiet NaN: " << qnan << std::endl; // std::cout << "Zero: " << zero << std::endl; // std::cout << "Is pos_inf infinite? " << pos_inf.isinf() << std::endl; // std::cout << "Is neg_inf infinite? " << neg_inf.isinf() << std::endl; // std::cout << "Is qnan NaN? " << qnan.isnan() << std::endl; // std::cout << "Is zero finite? " << zero.isfinite() << std::endl; // Test NaN comparison behavior // std::cout << "NaN == NaN: " << (qnan == qnan) << std::endl; // Should be false // std::cout << "NaN != NaN: " << (qnan != qnan) << std::endl; // Should be true // std::cout << " -> tests passed" << std::endl; } void testFloat16PrecisionFloat16() { .. _example-float16-infinity-7: .. 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-float16-quiet_nan-8: .. 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 .. _example-float16-raw-9: .. dropdown:: raw (np_test_1_all.cpp:7845) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 7835 :emphasize-lines: 11 float16 two = float16::from_bits(0x4000); float16 half = float16::from_bits(0x3800); // std::cout << "Zero (0x0000): " << zero << std::endl; // std::cout << "One (0x3C00): " << one << std::endl; // std::cout << "Two (0x4000): " << two << std::endl; // std::cout << "Half (0x3800): " << half << std::endl; // Test raw bit access float16 test_val(3.14159f); // std::cout << "Pi as float16: " << test_val << std::hex << " (raw: 0x" << test_val.raw() << ")" << std::dec << std::endl; std::cout << " -> tests passed" << std::endl; } void testFloat16ArraysFloat16() { std::cout << "========= testFloat16ArraysFloat16 ======================="; // Test creating arrays with different values std::vector shape = {2, 3}; auto array = createFloat16Array(shape, float16(1.5f)); .. _example-float16-signbit-10: .. dropdown:: signbit (np_test_1_all.cpp:10279) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 10269 :emphasize-lines: 11 using namespace numpy; using namespace numpy::special; namespace { bool isApproxEqual(double a, double b, double tol) { if (std::isnan(a) && std::isnan(b)) return true; if (std::isinf(a) && std::isinf(b) && std::signbit(a) == std::signbit(b)) return true; return std::abs(a - b) < tol || std::abs(a - b) < tol * std::max(std::abs(a), std::abs(b)); } } void testGammaFunctions() { std::cout << "========= testGammaFunctions ===="; // Test gamma function for known values using NDArray with MKL backend