float16#

class numpy::float16#

numpy C++ class.

Example#

#include <numpy/np_ndarray.h>
using namespace numpy;

// Use float16
float16 obj;
// ... operations ...

Constructors#

Signature

Location

Example

float16(floatvalue)

NP_FLOAT16.H:33

View

float16(doublevalue)

NP_FLOAT16.H:35

View

float16(intvalue)

NP_FLOAT16.H:37

View

Operators#

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#

Signature

Return Type

Location

Example

float16 from_bits(uint16_tbits)

float16

NP_FLOAT16.H:40

View

Logical#

Signature

Return Type

Location

Example

bool isfinite()

bool

NP_FLOAT16.H:151

View

bool isinf()

bool

NP_FLOAT16.H:145

View

bool isnan()

bool

NP_FLOAT16.H:139

View

Other Methods#

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

View

float16 quiet_NaN()

float16

NP_FLOAT16.H:130

View

uint16_t raw()

uint16_t

NP_FLOAT16.H:160

View

float16 signaling_NaN()

float16

NP_FLOAT16.H:134

bool signbit()

bool

NP_FLOAT16.H:156

View

Code Examples#

The following examples are extracted from the test suite.

float16 (np_test_1_all.cpp:7666)
7656    // std::cout << "  float128: " << sizeof(float128) << " bytes" << std::endl;
7657
7658    std::cout << " -> tests passed" << std::endl;
7659}
7660
7661// Float16 Functions (merged from np_test_float16.cpp)
7662void testFloat16CreationFloat16() {
7663    std::cout << "========= testFloat16CreationFloat16 =======================";
7664
7665    // Test basic array creation
7666    auto float16_array = createFloat16Array({2, 2}, float16(3.14f));
7667    // std::cout << "Float16 Array (2x2, filled with pi):";
7668    //float16_array.printArray();
7669    // std::cout << "Float16 size: " << sizeof(float16) << " bytes" << std::endl;
7670
7671    // Test different constructors
7672    float16 from_float(1.5f);
7673    float16 from_double(2.5);
7674    float16 from_int(42);
7675
7676    // std::cout << "From float: " << from_float << std::endl;
float16 (np_test_1_all.cpp:7666)
7656    // std::cout << "  float128: " << sizeof(float128) << " bytes" << std::endl;
7657
7658    std::cout << " -> tests passed" << std::endl;
7659}
7660
7661// Float16 Functions (merged from np_test_float16.cpp)
7662void testFloat16CreationFloat16() {
7663    std::cout << "========= testFloat16CreationFloat16 =======================";
7664
7665    // Test basic array creation
7666    auto float16_array = createFloat16Array({2, 2}, float16(3.14f));
7667    // std::cout << "Float16 Array (2x2, filled with pi):";
7668    //float16_array.printArray();
7669    // std::cout << "Float16 size: " << sizeof(float16) << " bytes" << std::endl;
7670
7671    // Test different constructors
7672    float16 from_float(1.5f);
7673    float16 from_double(2.5);
7674    float16 from_int(42);
7675
7676    // std::cout << "From float: " << from_float << std::endl;
float16 (np_test_1_all.cpp:7666)
7656    // std::cout << "  float128: " << sizeof(float128) << " bytes" << std::endl;
7657
7658    std::cout << " -> tests passed" << std::endl;
7659}
7660
7661// Float16 Functions (merged from np_test_float16.cpp)
7662void testFloat16CreationFloat16() {
7663    std::cout << "========= testFloat16CreationFloat16 =======================";
7664
7665    // Test basic array creation
7666    auto float16_array = createFloat16Array({2, 2}, float16(3.14f));
7667    // std::cout << "Float16 Array (2x2, filled with pi):";
7668    //float16_array.printArray();
7669    // std::cout << "Float16 size: " << sizeof(float16) << " bytes" << std::endl;
7670
7671    // Test different constructors
7672    float16 from_float(1.5f);
7673    float16 from_double(2.5);
7674    float16 from_int(42);
7675
7676    // std::cout << "From float: " << from_float << std::endl;
from_bits (np_test_1_all.cpp:7833)
7823    // std::cout << "  digits: " << std::numeric_limits<float16>::digits << std::endl;
7824    // std::cout << "  digits10: " << std::numeric_limits<float16>::digits10 << std::endl;
7825
7826    std::cout << " -> tests passed" << std::endl;
7827}
7828
7829void testFloat16BitPatternsFloat16() {
7830    std::cout << "========= testFloat16BitPatternsFloat16 =======================";
7831
7832    // Test some known bit patterns
7833    float16 zero = float16::from_bits(0x0000);
7834    float16 one = float16::from_bits(0x3C00);
7835    float16 two = float16::from_bits(0x4000);
7836    float16 half = float16::from_bits(0x3800);
7837
7838    // std::cout << "Zero (0x0000): " << zero << std::endl;
7839    // std::cout << "One (0x3C00): " << one << std::endl;
7840    // std::cout << "Two (0x4000): " << two << std::endl;
7841    // std::cout << "Half (0x3800): " << half << std::endl;
7842
7843    // Test raw bit access
isfinite (np_test_1_all.cpp:7769)
7759    float16 zero(0.0f);
7760
7761    // std::cout << "Positive infinity: " << pos_inf << std::endl;
7762    // std::cout << "Negative infinity: " << neg_inf << std::endl;
7763    // std::cout << "Quiet NaN: " << qnan << std::endl;
7764    // std::cout << "Zero: " << zero << std::endl;
7765
7766    // std::cout << "Is pos_inf infinite? " << pos_inf.isinf() << std::endl;
7767    // std::cout << "Is neg_inf infinite? " << neg_inf.isinf() << std::endl;
7768    // std::cout << "Is qnan NaN? " << qnan.isnan() << std::endl;
7769    // std::cout << "Is zero finite? " << zero.isfinite() << std::endl;
7770
7771    // Test NaN comparison behavior
7772    // std::cout << "NaN == NaN: " << (qnan == qnan) << std::endl;  // Should be false
7773    // std::cout << "NaN != NaN: " << (qnan != qnan) << std::endl;  // Should be true
7774
7775    // std::cout << " -> tests passed" << std::endl;
7776}
7777
7778void testFloat16PrecisionFloat16() {
7779    std::cout << "========= testFloat16PrecisionFloat16 =======================";
isinf (np_test_1_all.cpp:7766)
7756    float16 pos_inf = float16::infinity();
7757    float16 neg_inf = -float16::infinity();
7758    float16 qnan = float16::quiet_NaN();
7759    float16 zero(0.0f);
7760
7761    // std::cout << "Positive infinity: " << pos_inf << std::endl;
7762    // std::cout << "Negative infinity: " << neg_inf << std::endl;
7763    // std::cout << "Quiet NaN: " << qnan << std::endl;
7764    // std::cout << "Zero: " << zero << std::endl;
7765
7766    // std::cout << "Is pos_inf infinite? " << pos_inf.isinf() << std::endl;
7767    // std::cout << "Is neg_inf infinite? " << neg_inf.isinf() << std::endl;
7768    // std::cout << "Is qnan NaN? " << qnan.isnan() << std::endl;
7769    // std::cout << "Is zero finite? " << zero.isfinite() << std::endl;
7770
7771    // Test NaN comparison behavior
7772    // std::cout << "NaN == NaN: " << (qnan == qnan) << std::endl;  // Should be false
7773    // std::cout << "NaN != NaN: " << (qnan != qnan) << std::endl;  // Should be true
7774
7775    // std::cout << " -> tests passed" << std::endl;
7776}
isnan (np_test_1_all.cpp:7768)
7758    float16 qnan = float16::quiet_NaN();
7759    float16 zero(0.0f);
7760
7761    // std::cout << "Positive infinity: " << pos_inf << std::endl;
7762    // std::cout << "Negative infinity: " << neg_inf << std::endl;
7763    // std::cout << "Quiet NaN: " << qnan << std::endl;
7764    // std::cout << "Zero: " << zero << std::endl;
7765
7766    // std::cout << "Is pos_inf infinite? " << pos_inf.isinf() << std::endl;
7767    // std::cout << "Is neg_inf infinite? " << neg_inf.isinf() << std::endl;
7768    // std::cout << "Is qnan NaN? " << qnan.isnan() << std::endl;
7769    // std::cout << "Is zero finite? " << zero.isfinite() << std::endl;
7770
7771    // Test NaN comparison behavior
7772    // std::cout << "NaN == NaN: " << (qnan == qnan) << std::endl;  // Should be false
7773    // std::cout << "NaN != NaN: " << (qnan != qnan) << std::endl;  // Should be true
7774
7775    // std::cout << " -> tests passed" << std::endl;
7776}
7777
7778void testFloat16PrecisionFloat16() {
infinity (np_test_1_all.cpp:1067)
1057    std::cout << " -> tests passed" << std::endl;
1058}
1059
1060void testSpecialValueHandling() {
1061    std::cout << "========= testSpecialValueHandling =======================";
1062
1063    auto arr = createFloat64Array({4});
1064    arr.setElementAt({0}, 1.0);
1065    arr.setElementAt({1}, std::numeric_limits<double>::quiet_NaN());
1066    arr.setElementAt({2}, std::numeric_limits<double>::infinity());
1067    arr.setElementAt({3}, -std::numeric_limits<double>::infinity());
1068
1069    auto scalar_arr = createFloat64Array({4});
1070    scalar_arr.setElementAt({0}, 2.0);
1071    scalar_arr.setElementAt({1}, 1.0);
1072    scalar_arr.setElementAt({2}, 1.0);
1073    scalar_arr.setElementAt({3}, 1.0);
1074
1075    // Test NaN behavior - NaN comparisons should be false
1076    auto gt_result = greater(arr, scalar_arr);
quiet_NaN (np_test_1_all.cpp:1066)
1056    // std::cout << "[OK] equal and not_equal functions" << std::endl;
1057
1058    std::cout << " -> tests passed" << std::endl;
1059}
1060
1061void testSpecialValueHandling() {
1062    std::cout << "========= testSpecialValueHandling =======================";
1063
1064    auto arr = createFloat64Array({4});
1065    arr.setElementAt({0}, 1.0);
1066    arr.setElementAt({1}, std::numeric_limits<double>::quiet_NaN());
1067    arr.setElementAt({2}, std::numeric_limits<double>::infinity());
1068    arr.setElementAt({3}, -std::numeric_limits<double>::infinity());
1069
1070    auto scalar_arr = createFloat64Array({4});
1071    scalar_arr.setElementAt({0}, 2.0);
1072    scalar_arr.setElementAt({1}, 1.0);
1073    scalar_arr.setElementAt({2}, 1.0);
1074    scalar_arr.setElementAt({3}, 1.0);
1075
1076    // Test NaN behavior - NaN comparisons should be false
raw (np_test_1_all.cpp:7845)
7835    float16 two = float16::from_bits(0x4000);
7836    float16 half = float16::from_bits(0x3800);
7837
7838    // std::cout << "Zero (0x0000): " << zero << std::endl;
7839    // std::cout << "One (0x3C00): " << one << std::endl;
7840    // std::cout << "Two (0x4000): " << two << std::endl;
7841    // std::cout << "Half (0x3800): " << half << std::endl;
7842
7843    // Test raw bit access
7844    float16 test_val(3.14159f);
7845    // std::cout << "Pi as float16: " << test_val << std::hex << " (raw: 0x" << test_val.raw() << ")" << std::dec << std::endl;
7846
7847    std::cout << " -> tests passed" << std::endl;
7848}
7849
7850void testFloat16ArraysFloat16() {
7851    std::cout << "========= testFloat16ArraysFloat16 =======================";
7852
7853    // Test creating arrays with different values
7854    std::vector<size_t> shape = {2, 3};
7855    auto array = createFloat16Array(shape, float16(1.5f));
signbit (np_test_1_all.cpp:10279)
10269  using namespace numpy;
10270
10271  using namespace numpy::special;
10272
10273  namespace {
10274
10275
10276    bool isApproxEqual(double a, double b, double tol) {
10277      if (std::isnan(a) && std::isnan(b)) return true;
10278      if (std::isinf(a) && std::isinf(b) && std::signbit(a) == std::signbit(b)) return true;
10279      return std::abs(a - b) < tol || std::abs(a - b) < tol * std::max(std::abs(a), std::abs(b));
10280    }
10281  }
10282
10283
10284
10285  void testGammaFunctions() {
10286    std::cout << "========= testGammaFunctions ====";
10287
10288    // Test gamma function for known values using NDArray with MKL backend