|
1 | 1 | module Quadmath |
2 | 2 | using Compat: @assume_effects |
3 | 3 |
|
4 | | -export Float128, ComplexF128, Inf128 |
| 4 | +export Float128, ComplexF128, Inf128, NaN128 |
5 | 5 |
|
6 | 6 | import Base: (*), +, -, /, <, <=, ==, ^, convert, |
7 | 7 | reinterpret, sign_mask, exponent_mask, exponent_one, exponent_half, |
@@ -341,6 +341,13 @@ const Inf128 = reinterpret(Float128, exponent_mask(Float128)) |
341 | 341 | typemax(::Type{Float128}) = Inf128 |
342 | 342 | typemin(::Type{Float128}) = -Inf128 |
343 | 343 |
|
| 344 | +""" |
| 345 | + NaN128 |
| 346 | +
|
| 347 | +A not-a-number value of type [`Float128`](@ref). |
| 348 | +""" |
| 349 | +const NaN128 = reinterpret(Float128, UInt128(0x7fff8)<<108) |
| 350 | + |
344 | 351 | @static if !Sys.iswindows() |
345 | 352 | # disable fma on Windows until rounding mode issue fixed |
346 | 353 | # https://github.com/JuliaMath/Quadmath.jl/issues/31 |
@@ -551,15 +558,26 @@ function parse(::Type{Float128}, s::AbstractString) |
551 | 558 | Float128(@quad_ccall(libquadmath.strtoflt128(s::Cstring, C_NULL::Ptr{Ptr{Cchar}})::Cfloat128)) |
552 | 559 | end |
553 | 560 |
|
| 561 | +using Base.Ryu: writeshortest |
554 | 562 | function string(x::Float128) |
555 | | - lng = 64 |
556 | | - buf = Array{UInt8}(undef, lng + 1) |
557 | | - lng = @quad_ccall(libquadmath.quadmath_snprintf(buf::Ptr{UInt8}, (lng+1)::Csize_t, "%.35Qe"::Ptr{UInt8}, x::(Cfloat128...))::Cint) |
558 | | - return String(resize!(buf, lng)) |
| 563 | + buf = Base.StringVector(64) |
| 564 | + pos = writeshortest(buf, 1, x) |
| 565 | + return String(resize!(buf, pos - 1)) |
| 566 | +end |
| 567 | +function show(io::IO, x::Float128) |
| 568 | + buf = Base.StringVector(64) |
| 569 | + pos = writeshortest(buf, 1, x) |
| 570 | + if isfinite(x) |
| 571 | + write(io, "Float128(") |
| 572 | + write(io, resize!(buf, pos - 1)) |
| 573 | + write(io, ')') |
| 574 | + else |
| 575 | + write(io, resize!(buf, pos - 1)) |
| 576 | + write(io, "128") |
| 577 | + end |
| 578 | + return |
559 | 579 | end |
560 | | - |
561 | 580 | print(io::IO, b::Float128) = print(io, string(b)) |
562 | | -show(io::IO, b::Float128) = print(io, string(b)) |
563 | 581 |
|
564 | 582 | include("printf.jl") |
565 | 583 |
|
|
0 commit comments