Skip to content

add definitions for missing base/float.jl methods #82

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

Merged
merged 2 commits into from
Apr 24, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 15 additions & 2 deletions src/Quadmath.jl
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ export Float128, ComplexF128, Inf128

import Base: (*), +, -, /, <, <=, ==, ^, convert,
reinterpret, sign_mask, exponent_mask, exponent_one, exponent_half,
significand_mask, exponent, significand,
significand_mask, significand_bits, exponent_bits, exponent_bias,
exponent_max, exponent_raw_max, uinttype, inttype, floattype,
exponent, significand,
promote_rule, widen,
string, print, show, parse,
acos, acosh, asin, asinh, atan, atanh, cosh, cos, sincos,
Expand Down Expand Up @@ -127,7 +129,18 @@ exponent_one(::Type{Float128}) = 0x3fff_0000_0000_0000_0000_0000_0000_0000
exponent_half(::Type{Float128}) = 0x3ffe_0000_0000_0000_0000_0000_0000_0000
significand_mask(::Type{Float128}) = 0x0000_ffff_ffff_ffff_ffff_ffff_ffff_ffff

fpinttype(::Type{Float128}) = UInt128
significand_bits(::Type{Float128}) = 112
exponent_bits(::Type{Float128}) = 15
exponent_bias(::Type{Float128}) = 16383
exponent_max(::Type{Float128}) = 16383
exponent_raw_max(::Type{Float128}) = 32767

uinttype(::Type{Float128}) = UInt128
inttype(::Type{Float128}) = Int128
# TODO: Logically speaking, we should also make the following definitions,
# but this would constitute type piracy under Aqua.jl rules.
# floattype(::Type{UInt128}) = Float128
# floattype(::Type{Int128}) = Float128

# conversion
Float128(x::Float128) = x
Expand Down
19 changes: 19 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
@@ -1,6 +1,25 @@
using Test, Random
using Quadmath

@testset "fp properties" begin
# Test that the definitions of sign_mask, exponent_mask, significand_bits,
# etc. are compatible with the definitions in base/float.jl. Compare to:
# https://github.com/JuliaLang/julia/blob/5595d20a2877560583cd4891ce91605d10b1bb75/base/float.jl#L106
@test Base.significand_bits(Float128) ===
trailing_ones(Base.significand_mask(Float128))
@test Base.exponent_bits(Float128) ===
sizeof(Float128)*8 - Base.significand_bits(Float128) - 1
@test Base.exponent_bias(Float128) === Int(
Base.exponent_one(Float128) >> Base.significand_bits(Float128))
@test Base.exponent_max(Float128) ===
Int(Base.exponent_mask(Float128) >> Base.significand_bits(Float128)) -
Base.exponent_bias(Float128) - 1
@test Base.exponent_raw_max(Float128) === Int(
Base.exponent_mask(Float128) >> Base.significand_bits(Float128))
@test Base.uinttype(Float128) === UInt128
@test Base.inttype(Float128) === Int128
end

@testset "fp decomp" begin
y = Float128(2.0)
x,n = frexp(y)
Expand Down
Loading