reifydb_core/value/encoded/
f64.rs

1// Copyright (c) reifydb.com 2025
2// This file is licensed under the AGPL-3.0-or-later, see license.md file
3
4use std::ptr;
5
6use reifydb_type::Type;
7
8use crate::value::encoded::{EncodedValues, EncodedValuesLayout};
9
10impl EncodedValuesLayout {
11	pub fn set_f64(&self, row: &mut EncodedValues, index: usize, value: impl Into<f64>) {
12		let field = &self.fields[index];
13		debug_assert!(row.len() >= self.total_static_size());
14		debug_assert_eq!(field.r#type, Type::Float8);
15		row.set_valid(index, true);
16		unsafe { ptr::write_unaligned(row.make_mut().as_mut_ptr().add(field.offset) as *mut f64, value.into()) }
17	}
18
19	pub fn get_f64(&self, row: &EncodedValues, index: usize) -> f64 {
20		let field = &self.fields[index];
21		debug_assert!(row.len() >= self.total_static_size());
22		debug_assert_eq!(field.r#type, Type::Float8);
23		unsafe { (row.as_ptr().add(field.offset) as *const f64).read_unaligned() }
24	}
25
26	pub fn try_get_f64(&self, row: &EncodedValues, index: usize) -> Option<f64> {
27		if row.is_defined(index) && self.fields[index].r#type == Type::Float8 {
28			Some(self.get_f64(row, index))
29		} else {
30			None
31		}
32	}
33}
34
35#[cfg(test)]
36#[allow(clippy::approx_constant)]
37mod tests {
38	use reifydb_type::Type;
39
40	use crate::value::encoded::EncodedValuesLayout;
41
42	#[test]
43	fn test_set_get_f64() {
44		let layout = EncodedValuesLayout::new(&[Type::Float8]);
45		let mut row = layout.allocate();
46		layout.set_f64(&mut row, 0, 2.5f64);
47		assert_eq!(layout.get_f64(&row, 0), 2.5f64);
48	}
49
50	#[test]
51	fn test_try_get_f64() {
52		let layout = EncodedValuesLayout::new(&[Type::Float8]);
53		let mut row = layout.allocate();
54
55		assert_eq!(layout.try_get_f64(&row, 0), None);
56
57		layout.set_f64(&mut row, 0, 2.5f64);
58		assert_eq!(layout.try_get_f64(&row, 0), Some(2.5f64));
59	}
60
61	#[test]
62	fn test_special_values() {
63		let layout = EncodedValuesLayout::new(&[Type::Float8]);
64		let mut row = layout.allocate();
65
66		// Test zero
67		layout.set_f64(&mut row, 0, 0.0f64);
68		assert_eq!(layout.get_f64(&row, 0), 0.0f64);
69
70		// Test negative zero
71		let mut row2 = layout.allocate();
72		layout.set_f64(&mut row2, 0, -0.0f64);
73		assert_eq!(layout.get_f64(&row2, 0), -0.0f64);
74
75		// Test infinity
76		let mut row3 = layout.allocate();
77		layout.set_f64(&mut row3, 0, f64::INFINITY);
78		assert_eq!(layout.get_f64(&row3, 0), f64::INFINITY);
79
80		// Test negative infinity
81		let mut row4 = layout.allocate();
82		layout.set_f64(&mut row4, 0, f64::NEG_INFINITY);
83		assert_eq!(layout.get_f64(&row4, 0), f64::NEG_INFINITY);
84
85		// Test NaN
86		let mut row5 = layout.allocate();
87		layout.set_f64(&mut row5, 0, f64::NAN);
88		assert!(layout.get_f64(&row5, 0).is_nan());
89	}
90
91	#[test]
92	fn test_extreme_values() {
93		let layout = EncodedValuesLayout::new(&[Type::Float8]);
94		let mut row = layout.allocate();
95
96		layout.set_f64(&mut row, 0, f64::MAX);
97		assert_eq!(layout.get_f64(&row, 0), f64::MAX);
98
99		let mut row2 = layout.allocate();
100		layout.set_f64(&mut row2, 0, f64::MIN);
101		assert_eq!(layout.get_f64(&row2, 0), f64::MIN);
102
103		let mut row3 = layout.allocate();
104		layout.set_f64(&mut row3, 0, f64::MIN_POSITIVE);
105		assert_eq!(layout.get_f64(&row3, 0), f64::MIN_POSITIVE);
106	}
107
108	#[test]
109	fn test_high_precision() {
110		let layout = EncodedValuesLayout::new(&[Type::Float8]);
111		let mut row = layout.allocate();
112
113		let pi = std::f64::consts::PI;
114		layout.set_f64(&mut row, 0, pi);
115		assert_eq!(layout.get_f64(&row, 0), pi);
116
117		let mut row2 = layout.allocate();
118		let e = std::f64::consts::E;
119		layout.set_f64(&mut row2, 0, e);
120		assert_eq!(layout.get_f64(&row2, 0), e);
121	}
122
123	#[test]
124	fn test_mixed_with_other_types() {
125		let layout = EncodedValuesLayout::new(&[Type::Float8, Type::Int8, Type::Float8]);
126		let mut row = layout.allocate();
127
128		layout.set_f64(&mut row, 0, 3.14159265359);
129		layout.set_i64(&mut row, 1, 9223372036854775807i64);
130		layout.set_f64(&mut row, 2, -2.718281828459045);
131
132		assert_eq!(layout.get_f64(&row, 0), 3.14159265359);
133		assert_eq!(layout.get_i64(&row, 1), 9223372036854775807);
134		assert_eq!(layout.get_f64(&row, 2), -2.718281828459045);
135	}
136
137	#[test]
138	fn test_undefined_handling() {
139		let layout = EncodedValuesLayout::new(&[Type::Float8, Type::Float8]);
140		let mut row = layout.allocate();
141
142		layout.set_f64(&mut row, 0, 2.718281828459045);
143
144		assert_eq!(layout.try_get_f64(&row, 0), Some(2.718281828459045));
145		assert_eq!(layout.try_get_f64(&row, 1), None);
146
147		layout.set_undefined(&mut row, 0);
148		assert_eq!(layout.try_get_f64(&row, 0), None);
149	}
150
151	#[test]
152	fn test_try_get_f64_wrong_type() {
153		let layout = EncodedValuesLayout::new(&[Type::Boolean]);
154		let mut row = layout.allocate();
155
156		layout.set_bool(&mut row, 0, true);
157
158		assert_eq!(layout.try_get_f64(&row, 0), None);
159	}
160}