-
Notifications
You must be signed in to change notification settings - Fork 1k
[Variant] Allow lossless casting from integer to floating point #8357
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -28,7 +28,7 @@ use crate::decoder::{ | |
self, get_basic_type, get_primitive_type, VariantBasicType, VariantPrimitiveType, | ||
}; | ||
use crate::path::{VariantPath, VariantPathElement}; | ||
use crate::utils::{first_byte_from_slice, slice_from_slice}; | ||
use crate::utils::{first_byte_from_slice, fits_precision, slice_from_slice}; | ||
use std::ops::Deref; | ||
|
||
use arrow_schema::ArrowError; | ||
|
@@ -1082,8 +1082,8 @@ impl<'m, 'v> Variant<'m, 'v> { | |
|
||
/// Converts this variant to an `f16` if possible. | ||
/// | ||
/// Returns `Some(f16)` for float and double variants, | ||
/// `None` for non-floating-point variants. | ||
/// Returns `Some(f16)` for floating point values, and integers with up to 11 bits of | ||
/// precision. `None` otherwise. | ||
/// | ||
/// # Example | ||
/// | ||
|
@@ -1099,21 +1099,29 @@ impl<'m, 'v> Variant<'m, 'v> { | |
/// let v2 = Variant::from(std::f64::consts::PI); | ||
/// assert_eq!(v2.as_f16(), Some(f16::from_f64(std::f64::consts::PI))); | ||
/// | ||
/// // and from integers with no more than 11 bits of precision | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not sure if we need to add an overflow example here(e.g, Variant::from(2048) for f16) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The unit test for There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe we need to update the doc(the comment said that -- Returns I don't have a strong preference here, it's just an idea that popped into my mind when I saw this There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Oh! Good catch on the doc comment. Updated all three. |
||
/// let v3 = Variant::from(2047); | ||
/// assert_eq!(v3.as_f16(), Some(f16::from_f32(2047.0))); | ||
/// | ||
/// // but not from other variants | ||
/// let v3 = Variant::from("hello!"); | ||
/// assert_eq!(v3.as_f16(), None); | ||
/// let v4 = Variant::from("hello!"); | ||
/// assert_eq!(v4.as_f16(), None); | ||
pub fn as_f16(&self) -> Option<f16> { | ||
match *self { | ||
Variant::Float(i) => Some(f16::from_f32(i)), | ||
Variant::Double(i) => Some(f16::from_f64(i)), | ||
Variant::Int8(i) => Some(i.into()), | ||
Variant::Int16(i) if fits_precision::<11>(i) => Some(f16::from_f32(i as _)), | ||
Variant::Int32(i) if fits_precision::<11>(i) => Some(f16::from_f32(i as _)), | ||
Variant::Int64(i) if fits_precision::<11>(i) => Some(f16::from_f32(i as _)), | ||
_ => None, | ||
} | ||
} | ||
|
||
/// Converts this variant to an `f32` if possible. | ||
/// | ||
/// Returns `Some(f32)` for float and double variants, | ||
/// `None` for non-floating-point variants. | ||
/// Returns `Some(f32)` for floating point values, and integer values with up to 24 bits of | ||
/// precision. `None` otherwise. | ||
/// | ||
/// # Examples | ||
/// | ||
|
@@ -1128,23 +1136,31 @@ impl<'m, 'v> Variant<'m, 'v> { | |
/// let v2 = Variant::from(std::f64::consts::PI); | ||
/// assert_eq!(v2.as_f32(), Some(std::f32::consts::PI)); | ||
/// | ||
/// // and from integers with no more than 24 bits of precision | ||
/// let v3 = Variant::from(16777215i64); | ||
/// assert_eq!(v3.as_f32(), Some(16777215.0)); | ||
/// | ||
/// // but not from other variants | ||
/// let v3 = Variant::from("hello!"); | ||
/// assert_eq!(v3.as_f32(), None); | ||
/// let v4 = Variant::from("hello!"); | ||
/// assert_eq!(v4.as_f32(), None); | ||
/// ``` | ||
#[allow(clippy::cast_possible_truncation)] | ||
pub fn as_f32(&self) -> Option<f32> { | ||
match *self { | ||
Variant::Float(i) => Some(i), | ||
Variant::Double(i) => Some(i as f32), | ||
Variant::Int8(i) => Some(i.into()), | ||
Variant::Int16(i) => Some(i.into()), | ||
Variant::Int32(i) if fits_precision::<24>(i) => Some(i as _), | ||
Variant::Int64(i) if fits_precision::<24>(i) => Some(i as _), | ||
_ => None, | ||
} | ||
} | ||
|
||
/// Converts this variant to an `f64` if possible. | ||
/// | ||
/// Returns `Some(f64)` for float and double variants, | ||
/// `None` for non-floating-point variants. | ||
/// Returns `Some(f64)` for floating point values, and integer values with up to 53 bits of | ||
/// precision. `None` otherwise. | ||
/// | ||
/// # Examples | ||
/// | ||
|
@@ -1159,14 +1175,22 @@ impl<'m, 'v> Variant<'m, 'v> { | |
/// let v2 = Variant::from(std::f64::consts::PI); | ||
/// assert_eq!(v2.as_f64(), Some(std::f64::consts::PI)); | ||
/// | ||
/// // and from integers with no more than 53 bits of precision | ||
/// let v3 = Variant::from(9007199254740991i64); | ||
/// assert_eq!(v3.as_f64(), Some(9007199254740991.0)); | ||
/// | ||
/// // but not from other variants | ||
/// let v3 = Variant::from("hello!"); | ||
/// assert_eq!(v3.as_f64(), None); | ||
/// let v4 = Variant::from("hello!"); | ||
/// assert_eq!(v4.as_f64(), None); | ||
/// ``` | ||
pub fn as_f64(&self) -> Option<f64> { | ||
match *self { | ||
Variant::Float(i) => Some(i.into()), | ||
Variant::Double(i) => Some(i), | ||
Variant::Int8(i) => Some(i.into()), | ||
Variant::Int16(i) => Some(i.into()), | ||
Variant::Int32(i) => Some(i.into()), | ||
Variant::Int64(i) if fits_precision::<53>(i) => Some(i as _), | ||
_ => None, | ||
} | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Put
N
into the generic, not the parameter, because this can yield better performance, do I understand correctly?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It was mostly out of habit for small integer manipulation utilities... But now that you mention, it probably doesn't matter in the slightest -- the compiler will anyway inline aggressively and the constant arg will be folded in regardless of whether it's a generic arg or a function arg.
That said, there's one potential advantage to keeping the generic arg: Otherwise, it could be ambiguous which of two integer args is the precision and which is the actual value. Any preferences?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the detailed reply, I'm fine with the current implementation