@@ -5577,82 +5577,86 @@ def test_same_value_cast_strings_narrow_width(self, dtype):
55775577 with pytest .raises (ValueError ):
55785578 q .astype (dtype , casting = "same_value" )
55795579
5580- # @pytest.mark.parametrize("src_backend,dst_backend", [
5581- # ("sleef", "longdouble"),
5582- # ("longdouble", "sleef"),
5583- # ("sleef", "sleef"),
5584- # ("longdouble", "longdouble")
5585- # ])
5586- # def test_quad_to_quad_same_value_casting_passing(self, src_backend, dst_backend):
5587- # """Test values that should roundtrip exactly between backends."""
5588- # # Values exactly representable in both backends (and in double, since
5589- # # inter-backend conversion goes through double)
5590- # passing_values = [
5591- # " 0.0", " -0.0", " 1.0", " -1.0" ,
5592- # " 0.5", " 0.25", " 0.125" ,
5593- # " 2.0", " 4.0", " 8.0" ,
5594- # "inf", "-inf", "nan",
5595- # " 1e100", " -1e-100" ,
5596- # str(2**52), # Largest consecutive integer in double
5597- # ]
5598-
5599- # for val in passing_values:
5600- # src = np.array([val], dtype=QuadPrecDType(backend=src_backend))
5601- # result = src.astype(QuadPrecDType(backend=dst_backend), casting="same_value")
5580+ @pytest .mark .parametrize ("src_backend,dst_backend" , [
5581+ ("sleef" , "longdouble" ),
5582+ ("longdouble" , "sleef" ),
5583+ ("sleef" , "sleef" ),
5584+ ("longdouble" , "longdouble" )
5585+ ])
5586+ def test_quad_to_quad_same_value_casting_passing (self , src_backend , dst_backend ):
5587+ """Test values that should roundtrip exactly between backends."""
5588+ # Values exactly representable in both backends (and in double, since
5589+ # inter-backend conversion goes through double)
5590+ passing_values = [
5591+ 0.0 , - 0.0 , 1.0 , - 1.0 ,
5592+ 0.5 , 0.25 , 0.125 ,
5593+ 2.0 , 4.0 , 8.0 ,
5594+ "inf" , "-inf" , "nan" ,
5595+ 1e100 , - 1e-100 ,
5596+ str (2 ** 52 ), # Largest consecutive integer in double
5597+ ]
5598+
5599+ for val in passing_values :
5600+ src = np .array ([val ], dtype = QuadPrecDType (backend = src_backend ))
5601+ result = src .astype (QuadPrecDType (backend = dst_backend ), casting = "same_value" )
56025602
5603- # # Verify value is preserved
5604- # if val == "nan":
5605- # assert np.isnan(result[0])
5606- # else:
5607- # assert result[0] == src[0], f"Value {val} failed for {src_backend} -> {dst_backend}"
5608-
5609-
5610- # @pytest.mark.parametrize("src_backend,dst_backend", [
5611- # ("sleef", "longdouble"),
5612- # ("longdouble", "sleef"),
5613- # ])
5614- # def test_quad_to_quad_interbackend_same_value_casting_failing(self, src_backend, dst_backend):
5615- # """Test values that cannot roundtrip exactly between backends."""
5616-
5617- # # Inter-backend conversion goes through double, so values outside
5618- # # double's precision should fail
5619- # ld_info = np.finfo(np.longdouble)
5620-
5621- # # Skip if longdouble has same precision as quad (PowerPC binary128)
5622- # if ld_info.nmant >= 112:
5623- # pytest.skip("longdouble has same precision as quad on this platform")
5624-
5625- # # Also need to consider that conversion goes through double
5626- # # So precision is limited by double (~52 bit mantissa)
5627- # double_info = np.finfo(np.float64)
5628-
5629- # # Values that exceed double precision
5630- # failing_values = [
5631- # str(2**53 + 1), # First integer not exactly representable in double
5632- # "1.0000000000000001", # 1 + small epsilon beyond double precision
5633- # "3.141592653589793238462643383279502884197", # Pi with more than double precision
5634- # ]
5635-
5636- # for val in failing_values:
5637- # src = np.array([val], dtype=QuadPrecDType(backend=src_backend))
5638- # with pytest.raises(ValueError):
5639- # src.astype(QuadPrecDType(backend=dst_backend), casting="same_value")
5640-
5641-
5642- # @pytest.mark.parametrize("backend", ["sleef", "longdouble"])
5643- # def test_quad_to_quad_same_backend_always_passes(self, backend):
5644- # """Same backend conversion should always pass same_value."""
5645- # # Even high-precision values should pass when backend is the same
5646- # values = [
5647- # "3.141592653589793238462643383279502884197",
5648- # "2.718281828459045235360287471352662497757",
5649- # str(2**113), # Large integer
5650- # "1e4000", # Large exponent (within quad range)
5651- # ]
5652-
5653- # for val in values:
5654- # src = np.array([val], dtype=QuadPrecDType(backend=backend))
5655- # result = src.astype(QuadPrecDType(backend=backend), casting="same_value")
5656- # # Should not raise, and value should be unchanged
5657- # assert str(result[0]) == str(src[0])
5603+ # Verify value is preserved
5604+ if val == "nan" :
5605+ assert np .isnan (result [0 ])
5606+ else :
5607+ assert float (result [0 ]) == float (src [0 ]), f"Value { val } failed for { src_backend } -> { dst_backend } "
5608+
5609+
5610+ @pytest .mark .parametrize ("src_backend,dst_backend" , [
5611+ ("sleef" , "longdouble" ),
5612+ ("longdouble" , "sleef" ),
5613+ ])
5614+ def test_quad_to_quad_interbackend_same_value_casting_failing (self , src_backend , dst_backend ):
5615+ """Test values that cannot roundtrip exactly between backends.
5616+
5617+ Inter-backend conversion goes through double, so values exceeding
5618+ double's precision (~53 bits mantissa) will fail same_value casting.
5619+ """
5620+ ld_info = np .finfo (np .longdouble )
5621+ double_info = np .finfo (np .float64 )
5622+
5623+ # Skip if longdouble has same precision as quad (PowerPC binary128)
5624+ # In that case, sleef <-> longdouble might use a direct path
5625+ if ld_info .nmant >= 112 :
5626+ pytest .skip ("longdouble has same precision as quad on this platform" )
5627+
5628+ # For longdouble -> sleef: only fails if longdouble has more precision than double
5629+ if src_backend == "longdouble" and ld_info .nmant <= double_info .nmant :
5630+ pytest .skip ("longdouble has same or less precision than double on this platform" )
5631+
5632+ # Values that exceed double precision (53-bit mantissa)
5633+ # These will lose precision when going through the double conversion
5634+ failing_values = [
5635+ str (2 ** 53 + 1 ), # First integer not exactly representable in double
5636+ "3.141592653589793238462643383279502884197" , # Pi with more than double precision
5637+ "1.00000000000000011" , # 1 + epsilon beyond double precision
5638+ ]
5639+
5640+ for val in failing_values :
5641+ src = np .array ([val ], dtype = QuadPrecDType (backend = src_backend ))
5642+ with pytest .raises (ValueError ):
5643+ src .astype (QuadPrecDType (backend = dst_backend ), casting = "same_value" )
5644+
5645+
5646+ @pytest .mark .parametrize ("backend" , ["sleef" , "longdouble" ])
5647+ def test_quad_to_quad_same_backend_always_passes (self , backend ):
5648+ """Same backend conversion should always pass same_value."""
5649+ # Even high-precision values should pass when backend is the same
5650+ values = [
5651+ "3.141592653589793238462643383279502884197" ,
5652+ "2.718281828459045235360287471352662497757" ,
5653+ str (2 ** 113 ), # Large integer
5654+ "1e4000" , # Large exponent (within quad range)
5655+ ]
5656+
5657+ for val in values :
5658+ src = np .array ([val ], dtype = QuadPrecDType (backend = backend ))
5659+ result = src .astype (QuadPrecDType (backend = backend ), casting = "same_value" )
5660+ # Should not raise, and value should be unchanged
5661+ assert str (result [0 ]) == str (src [0 ])
56585662
0 commit comments