Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions datafusion/common/src/scalar/consts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,24 +17,36 @@

// Constants defined for scalar construction.

// Next F16 value above π (upper bound)
pub(super) const PI_UPPER_F16: half::f16 = half::f16::from_bits(0x4249);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How/where are these constants sourced from?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These are the IEEE 754 binary16 half-precision bit representations closest to pi.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I converted std::f64::consts::PI to f16 which results in 0x4248 (approx 3.1406). Since this value is slightly lower than the actual Pi. I used the next representable bit pattern (0x4249) to ensure a strict upper bound.


// Next f32 value above π (upper bound)
pub(super) const PI_UPPER_F32: f32 = std::f32::consts::PI.next_up();

// Next f64 value above π (upper bound)
pub(super) const PI_UPPER_F64: f64 = std::f64::consts::PI.next_up();

// Next f16 value below -π (lower bound)
pub(super) const NEGATIVE_PI_LOWER_F16: half::f16 = half::f16::from_bits(0xC249);

// Next f32 value below -π (lower bound)
pub(super) const NEGATIVE_PI_LOWER_F32: f32 = (-std::f32::consts::PI).next_down();

// Next f64 value below -π (lower bound)
pub(super) const NEGATIVE_PI_LOWER_F64: f64 = (-std::f64::consts::PI).next_down();

// Next f16 value above π/2 (upper bound)
pub(super) const FRAC_PI_2_UPPER_F16: half::f16 = half::f16::from_bits(0x3E49);

// Next f32 value above π/2 (upper bound)
pub(super) const FRAC_PI_2_UPPER_F32: f32 = std::f32::consts::FRAC_PI_2.next_up();

// Next f64 value above π/2 (upper bound)
pub(super) const FRAC_PI_2_UPPER_F64: f64 = std::f64::consts::FRAC_PI_2.next_up();

// Next f32 value below -π/2 (lower bound)
pub(super) const NEGATIVE_FRAC_PI_2_LOWER_F16: half::f16 = half::f16::from_bits(0xBE49);

// Next f32 value below -π/2 (lower bound)
pub(super) const NEGATIVE_FRAC_PI_2_LOWER_F32: f32 =
(-std::f32::consts::FRAC_PI_2).next_down();
Expand Down
18 changes: 10 additions & 8 deletions datafusion/common/src/scalar/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1342,8 +1342,7 @@ impl ScalarValue {
/// Returns a [`ScalarValue`] representing PI's upper bound
pub fn new_pi_upper(datatype: &DataType) -> Result<ScalarValue> {
match datatype {
// TODO: half::f16 doesn't seem to have equivalent
// https://github.com/apache/datafusion/issues/19465
DataType::Float16 => Ok(ScalarValue::Float16(Some(consts::PI_UPPER_F16))),
DataType::Float32 => Ok(ScalarValue::from(consts::PI_UPPER_F32)),
DataType::Float64 => Ok(ScalarValue::from(consts::PI_UPPER_F64)),
_ => {
Expand All @@ -1355,8 +1354,9 @@ impl ScalarValue {
/// Returns a [`ScalarValue`] representing -PI's lower bound
pub fn new_negative_pi_lower(datatype: &DataType) -> Result<ScalarValue> {
match datatype {
// TODO: half::f16 doesn't seem to have equivalent
// https://github.com/apache/datafusion/issues/19465
DataType::Float16 => {
Ok(ScalarValue::Float16(Some(consts::NEGATIVE_PI_LOWER_F16)))
}
DataType::Float32 => Ok(ScalarValue::from(consts::NEGATIVE_PI_LOWER_F32)),
DataType::Float64 => Ok(ScalarValue::from(consts::NEGATIVE_PI_LOWER_F64)),
_ => {
Expand All @@ -1368,8 +1368,9 @@ impl ScalarValue {
/// Returns a [`ScalarValue`] representing FRAC_PI_2's upper bound
pub fn new_frac_pi_2_upper(datatype: &DataType) -> Result<ScalarValue> {
match datatype {
// TODO: half::f16 doesn't seem to have equivalent
// https://github.com/apache/datafusion/issues/19465
DataType::Float16 => {
Ok(ScalarValue::Float16(Some(consts::FRAC_PI_2_UPPER_F16)))
}
DataType::Float32 => Ok(ScalarValue::from(consts::FRAC_PI_2_UPPER_F32)),
DataType::Float64 => Ok(ScalarValue::from(consts::FRAC_PI_2_UPPER_F64)),
_ => {
Expand All @@ -1381,8 +1382,9 @@ impl ScalarValue {
// Returns a [`ScalarValue`] representing FRAC_PI_2's lower bound
pub fn new_neg_frac_pi_2_lower(datatype: &DataType) -> Result<ScalarValue> {
match datatype {
// TODO: half::f16 doesn't seem to have equivalent
// https://github.com/apache/datafusion/issues/19465
DataType::Float16 => Ok(ScalarValue::Float16(Some(
consts::NEGATIVE_FRAC_PI_2_LOWER_F16,
))),
DataType::Float32 => {
Ok(ScalarValue::from(consts::NEGATIVE_FRAC_PI_2_LOWER_F32))
}
Expand Down