From ddd683bd793a11e73d776300cc80bf7dd1854b6c Mon Sep 17 00:00:00 2001 From: Tommy Hofmann Date: Mon, 26 Feb 2024 11:03:21 +0100 Subject: [PATCH] feat: readd ad hoc binary operations - We need ZZRingElem * AbstractFloat - We need QQFieldElem * AbstractFloat --- src/HeckeMiscInteger.jl | 16 ++++++++++++++++ test/flint/fmpq-test.jl | 11 +++++++++++ test/flint/fmpz-test.jl | 10 ++++++++++ 3 files changed, 37 insertions(+) diff --git a/src/HeckeMiscInteger.jl b/src/HeckeMiscInteger.jl index 0afa634f6b..9f005c4d9b 100644 --- a/src/HeckeMiscInteger.jl +++ b/src/HeckeMiscInteger.jl @@ -61,6 +61,22 @@ end #end +function *(a::ZZRingElem, b::AbstractFloat) + return BigInt(a) * b +end + +function *(a::AbstractFloat, b::ZZRingElem) + return a * BigInt(b) +end + +function *(a::QQFieldElem, b::AbstractFloat) + return Rational(a) * b +end + +function *(a::AbstractFloat, b::QQFieldElem) + return a * Rational(b) +end + function convert(R::Type{Rational{Base.GMP.BigInt}}, a::ZZRingElem) return R(BigInt(a)) end diff --git a/test/flint/fmpq-test.jl b/test/flint/fmpq-test.jl index 74414814f0..e1f7b508a3 100644 --- a/test/flint/fmpq-test.jl +++ b/test/flint/fmpq-test.jl @@ -320,6 +320,17 @@ end @test QQFieldElem(1, 2) // (BigInt(1)//BigInt(2)) == 1 @test QQFieldElem(1, 2) // (1//2) == 1 + + a = QQ(1//2) + @test a * 1.5 isa BigFloat + @test isapprox(a * 1.5, 0.75) + @test 1.5 * a isa BigFloat + @test isapprox(1.5 * a, 0.75) + @test a * big"1.5" isa BigFloat + @test isapprox(a * big"1.5", big"0.75") + @test big"1.5" * a isa BigFloat + @test isapprox(big"0.75", big"1.5" * a) + end @testset "QQFieldElem.comparison" begin diff --git a/test/flint/fmpz-test.jl b/test/flint/fmpz-test.jl index 2dae7c3d4f..85eaa7a65f 100644 --- a/test/flint/fmpz-test.jl +++ b/test/flint/fmpz-test.jl @@ -419,6 +419,16 @@ end @test 5*a == -60 @test a%5 == -2 + + a = one(ZZ) + @test a * 1.5 isa BigFloat + @test isapprox(a * 1.5, 1.5) + @test 1.5 * a isa BigFloat + @test isapprox(1.5 * a, 1.5) + @test a * big"1.5" isa BigFloat + @test isapprox(a * big"1.5", big"1.5") + @test big"1.5" * a isa BigFloat + @test isapprox(big"1.5" * a, big"1.5") end @testset "ZZRingElem.adhoc_division" begin