@@ -91,6 +91,105 @@ def test_create_empty_array(self):
9191 assert result .dtype .name == "QuadPrecDType128"
9292 assert result .shape == (0 ,)
9393
94+ def test_create_from_numpy_int_scalars (self ):
95+ """Test that QuadPrecision can create scalars from numpy integer types."""
96+ # Test np.int32
97+ result = QuadPrecision (np .int32 (42 ))
98+ assert isinstance (result , QuadPrecision )
99+ assert float (result ) == 42.0
100+
101+ # Test np.int64
102+ result = QuadPrecision (np .int64 (100 ))
103+ assert isinstance (result , QuadPrecision )
104+ assert float (result ) == 100.0
105+
106+ # Test np.uint32
107+ result = QuadPrecision (np .uint32 (255 ))
108+ assert isinstance (result , QuadPrecision )
109+ assert float (result ) == 255.0
110+
111+ # Test np.int8
112+ result = QuadPrecision (np .int8 (- 128 ))
113+ assert isinstance (result , QuadPrecision )
114+ assert float (result ) == - 128.0
115+
116+ def test_create_from_numpy_float_scalars (self ):
117+ """Test that QuadPrecision can create scalars from numpy floating types."""
118+ # Test np.float64
119+ result = QuadPrecision (np .float64 (3.14 ))
120+ assert isinstance (result , QuadPrecision )
121+ assert abs (float (result ) - 3.14 ) < 1e-10
122+
123+ # Test np.float32
124+ result = QuadPrecision (np .float32 (2.71 ))
125+ assert isinstance (result , QuadPrecision )
126+ # Note: float32 has limited precision, so we use a looser tolerance
127+ assert abs (float (result ) - 2.71 ) < 1e-5
128+
129+ # Test np.float16
130+ result = QuadPrecision (np .float16 (1.5 ))
131+ assert isinstance (result , QuadPrecision )
132+ assert abs (float (result ) - 1.5 ) < 1e-3
133+
134+ def test_create_from_zero_dimensional_array (self ):
135+ """Test that QuadPrecision can create from 0-d numpy arrays."""
136+ # 0-d array from scalar
137+ arr_0d = np .array (5.5 )
138+ result = QuadPrecision (arr_0d )
139+ assert isinstance (result , np .ndarray )
140+ assert result .shape == () # 0-d array
141+ assert result .dtype .name == "QuadPrecDType128"
142+
143+ # Another test with integer
144+ arr_0d = np .array (42 )
145+ result = QuadPrecision (arr_0d )
146+ assert isinstance (result , np .ndarray )
147+ assert result .shape == ()
148+
149+ def test_numpy_scalar_with_backend (self ):
150+ """Test that numpy scalars respect the backend parameter."""
151+ # Test with sleef backend
152+ result = QuadPrecision (np .int32 (10 ), backend = 'sleef' )
153+ assert isinstance (result , QuadPrecision )
154+ assert "backend='sleef'" in repr (result )
155+
156+ # Test with longdouble backend
157+ result = QuadPrecision (np .float64 (3.14 ), backend = 'longdouble' )
158+ assert isinstance (result , QuadPrecision )
159+ assert "backend='longdouble'" in repr (result )
160+
161+ def test_numpy_scalar_types_coverage (self ):
162+ """Test a comprehensive set of numpy scalar types."""
163+ # Integer types
164+ int_types = [
165+ (np .int8 , 10 ),
166+ (np .int16 , 1000 ),
167+ (np .int32 , 100000 ),
168+ (np .int64 , 10000000 ),
169+ (np .uint8 , 200 ),
170+ (np .uint16 , 50000 ),
171+ (np .uint32 , 4000000000 ),
172+ ]
173+
174+ for dtype , value in int_types :
175+ result = QuadPrecision (dtype (value ))
176+ assert isinstance (result , QuadPrecision ), f"Failed for { dtype .__name__ } "
177+ assert float (result ) == float (value ), f"Value mismatch for { dtype .__name__ } "
178+
179+ # Float types
180+ float_types = [
181+ (np .float16 , 1.5 ),
182+ (np .float32 , 2.5 ),
183+ (np .float64 , 3.5 ),
184+ ]
185+
186+ for dtype , value in float_types :
187+ result = QuadPrecision (dtype (value ))
188+ assert isinstance (result , QuadPrecision ), f"Failed for { dtype .__name__ } "
189+ # Use appropriate tolerance based on dtype precision
190+ expected = float (dtype (value ))
191+ assert abs (float (result ) - expected ) < 1e-5 , f"Value mismatch for { dtype .__name__ } "
192+
94193
95194def test_string_roundtrip ():
96195 # Test with various values that require full quad precision
0 commit comments