Skip to content

Commit 7e41ff7

Browse files
RalphASsimonbyrne
authored andcommitted
maxintfloat, basic rand() functionality, and some administrative stuff (#26)
* maxintfloat, basic rand() functionality, and some administrative stuff * suggested edits
1 parent 2bb5175 commit 7e41ff7

File tree

4 files changed

+52
-7
lines changed

4 files changed

+52
-7
lines changed

LICENSE

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,13 @@
1-
Float128 is licensed under the MIT License:
1+
Quadmath.jl is licensed under the MIT License:
22

3-
Copyright (c) 2016: Harald Hofstätter, Simon Byrne
3+
Copyright (c) 2016-2019: Harald Hofstätter, Simon Byrne, Ralph Smith
4+
5+
portions derived from Julia Base and stdlib:
6+
7+
Copyright (c) 2009-2019: Jeff Bezanson, Stefan Karpinski, Viral B. Shah,
8+
and other contributors:
9+
10+
https://github.com/JuliaLang/julia/contributors
411

512
Permission is hereby granted, free of charge, to any person obtaining
613
a copy of this software and associated documentation files (the

Project.toml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
name = "Quadmath"
22
uuid = "be4d8f0f-7fa4-5f49-b795-2f01399ab2dd"
3+
version = "0.2.1"
34

45
[deps]
6+
Random = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c"
57
Requires = "ae029012-a4dd-5104-9daa-d747884805df"
68

79
[extras]
10+
Printf = "de0858da-6303-5e67-8744-51eddeeeb8d7"
811
SpecialFunctions = "276daf66-3868-5448-9aa4-cd146d93841b"
912
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
10-
Printf = "de0858da-6303-5e67-8744-51eddeeeb8d7"
1113

1214
[targets]
1315
test = ["Test", "SpecialFunctions", "Printf"]

src/Quadmath.jl

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,11 @@ import Base: (*), +, -, /, <, <=, ==, ^, convert,
1515
copysign, flipsign, max, min, hypot, abs,
1616
ldexp, frexp, modf, nextfloat, eps,
1717
isinf, isnan, isfinite, isinteger,
18-
floatmin, floatmax, precision, signbit,
18+
floatmin, floatmax, precision, signbit, maxintfloat,
1919
Int32, Int64, Float64, BigFloat, BigInt
2020

21+
using Random
22+
2123
if Sys.isapple()
2224
const quadoplib = "libquadmath.0"
2325
const libquadmath = "libquadmath.0"
@@ -361,6 +363,8 @@ eps(::Type{Float128}) = reinterpret(Float128, 0x3f8f_0000_0000_0000_0000_0000_00
361363
floatmin(::Type{Float128}) = reinterpret(Float128, 0x0001_0000_0000_0000_0000_0000_0000_0000)
362364
floatmax(::Type{Float128}) = reinterpret(Float128, 0x7ffe_ffff_ffff_ffff_ffff_ffff_ffff_ffff)
363365

366+
maxintfloat(::Type{Float128}) = Float128(0x0002_0000_0000_0000_0000_0000_0000_0000)
367+
364368
ldexp(x::Float128, n::Cint) =
365369
Float128(@ccall(libquadmath.ldexpq(x::Cfloat128, n::Cint)::Cfloat128))
366370
ldexp(x::Float128, n::Integer) =
@@ -543,6 +547,14 @@ promote_rule(::Type{Float128}, ::Type{<:Integer}) = Float128
543547
#widen(::Type{Float64}) = Float128
544548
widen(::Type{Float128}) = BigFloat
545549

550+
function Random.rand(rng::AbstractRNG, s::Random.SamplerTrivial{Random.CloseOpen01{Float128}})
551+
u = rand(rng, UInt128)
552+
x = (reinterpret(Float128, u & Base.significand_mask(Float128)
553+
| Base.exponent_one(Float128))
554+
- one(Float128))
555+
return x
556+
end
557+
546558
# TODO: need to do this better
547559
function parse(::Type{Float128}, s::AbstractString)
548560
Float128(@ccall(libquadmath.strtoflt128(s::Cstring, C_NULL::Ptr{Ptr{Cchar}})::Cfloat128))

test/runtests.jl

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
using Test
1+
using Test, Random
22
using Quadmath
33

44
@testset "fp decomp" begin
@@ -10,6 +10,7 @@ using Quadmath
1010
@test z == y
1111
end
1212

13+
@testset "conversions" begin
1314
@testset "conversion $T" for T in (Float32, Float64, Int32, Int64, Int128, UInt32, UInt64, UInt128, BigFloat, BigInt)
1415
@test Float128(T(1)) + Float128(T(2)) == Float128(T(3))
1516
@test Float128(T(1)) + Float128(T(2)) <= Float128(T(3))
@@ -43,8 +44,6 @@ end
4344
@test isinf(T(x-Float128(1)))
4445
end
4546

46-
@test Base.exponent_one(Float128) == reinterpret(UInt128, Float128(1.0))
47-
4847
@testset "BigFloat" begin
4948
x = parse(Float128, "0.1")
5049
y = parse(Float128, "0.2")
@@ -58,6 +57,9 @@ end
5857
@test Float64(x+y) == Float64(BigInt(x) + BigInt(y))
5958
@test x+y == Float128(BigInt(x) + BigInt(y))
6059
end
60+
end
61+
62+
@test Base.exponent_one(Float128) == reinterpret(UInt128, Float128(1.0))
6163

6264
@testset "flipsign" begin
6365
x = Float128( 2.0)
@@ -105,6 +107,28 @@ end
105107
@testset "misc. math" begin
106108
x = sqrt(Float128(2.0))
107109
@test abs(x^(-2) - Float128(0.5)) < 1.0e-32
110+
m = maxintfloat(Float128)
111+
@test m+one(Float128) == m
112+
@test m-one(Float128) != m
113+
end
114+
115+
function hist(X, n)
116+
v = zeros(Int, n)
117+
for x in X
118+
v[floor(Int, x*n) + 1] += 1
119+
end
120+
v
121+
end
122+
123+
@testset "random" begin
124+
# test for sanity and coarse uniformity
125+
@test typeof(rand(Float128)) == Float128
126+
for rng in [MersenneTwister(), RandomDevice()]
127+
counts = hist(rand(rng, Float128, 2000), 4)
128+
@test minimum(counts) > 300
129+
counts = hist([rand(rng, Float128) for i in 1:2000], 4)
130+
@test minimum(counts) > 300
131+
end
108132
end
109133

110134
@testset "string conversion" begin

0 commit comments

Comments
 (0)