• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

JuliaLang / julia / #37527

pending completion
#37527

push

local

web-flow
make `IRShow.method_name` inferrable (#49607)

18 of 18 new or added lines in 3 files covered. (100.0%)

68710 of 81829 relevant lines covered (83.97%)

33068903.12 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

59.26
/base/ryu/Ryu.jl
1
module Ryu
2

3
import .Base: significand_bits, significand_mask, exponent_bits, exponent_mask, exponent_bias, exponent_max, uinttype
4

5
include("utils.jl")
6
include("shortest.jl")
7
include("fixed.jl")
8
include("exp.jl")
9

10
"""
11
    Ryu.neededdigits(T)
12

13
Number of digits necessary to represent type `T` in fixed-precision decimal.
14
"""
15
neededdigits(::Type{Float64}) = 309 + 17
×
16
neededdigits(::Type{Float32}) = 39 + 9 + 2
×
17
neededdigits(::Type{Float16}) = 9 + 5 + 9
×
18

19
"""
20
    Ryu.writeshortest(x, plus=false, space=false, hash=true, precision=-1, expchar=UInt8('e'), padexp=false, decchar=UInt8('.'), typed=false, compact=false)
21
    Ryu.writeshortest(buf::Vector{UInt8}, pos::Int, x, args...)
22

23
Convert a float value `x` into its "shortest" decimal string, which can be parsed back to the same value.
24
This function allows achieving the `%g` printf format.
25
Note the 2nd method allows passing in a byte buffer and position directly; callers must ensure the buffer has sufficient room to hold the entire decimal string.
26

27
Various options for the output format include:
28
  * `plus`: for positive `x`, prefix decimal string with a `'+'` character
29
  * `space`: for positive `x`, prefix decimal string with a `' '` character; overridden if `plus=true`
30
  * `hash`: whether the decimal point should be written, even if no additional digits are needed for precision
31
  * `precision`: minimum number of digits to be included in the decimal string; extra `'0'` characters will be added for padding if necessary
32
  * `expchar`: character to use exponent component in scientific notation
33
  * `padexp`: whether two digits should always be written, even for single-digit exponents (e.g. `e+1` becomes `e+01`)
34
  * `decchar`: decimal point character to be used
35
  * `typed`: whether additional type information should be printed for `Float16` / `Float32`
36
  * `compact`: output will be limited to 6 significant digits
37
"""
38
function writeshortest(x::T,
×
39
        plus::Bool=false,
40
        space::Bool=false,
41
        hash::Bool=true,
42
        precision::Integer=-1,
43
        expchar::UInt8=UInt8('e'),
44
        padexp::Bool=false,
45
        decchar::UInt8=UInt8('.'),
46
        typed::Bool=false,
47
        compact::Bool=false) where {T <: Base.IEEEFloat}
48
    buf = Base.StringVector(neededdigits(T))
×
49
    pos = writeshortest(buf, 1, x, plus, space, hash, precision, expchar, padexp, decchar, typed, compact)
×
50
    return String(resize!(buf, pos - 1))
×
51
end
52

53
"""
54
    Ryu.writefixed(x, precision, plus=false, space=false, hash=false, decchar=UInt8('.'), trimtrailingzeros=false)
55
    Ryu.writefixed(buf::Vector{UInt8}, pos::Int, x, args...)
56

57
Convert a float value `x` into a "fixed" size decimal string of the provided precision.
58
This function allows achieving the `%f` printf format.
59
Note the 2nd method allows passing in a byte buffer and position directly; callers must ensure the buffer has sufficient room to hold the entire decimal string.
60

61
Various options for the output format include:
62
  * `plus`: for positive `x`, prefix decimal string with a `'+'` character
63
  * `space`: for positive `x`, prefix decimal string with a `' '` character; overridden if `plus=true`
64
  * `hash`: whether the decimal point should be written, even if no additional digits are needed for precision
65
  * `precision`: minimum number of significant digits to be included in the decimal string; extra `'0'` characters will be added for padding if necessary
66
  * `decchar`: decimal point character to be used
67
  * `trimtrailingzeros`: whether trailing zeros of fractional part should be removed
68
"""
69
function writefixed(x::T,
72✔
70
    precision::Integer,
71
    plus::Bool=false,
72
    space::Bool=false,
73
    hash::Bool=false,
74
    decchar::UInt8=UInt8('.'),
75
    trimtrailingzeros::Bool=false) where {T <: Base.IEEEFloat}
76
    buf = Base.StringVector(precision + neededdigits(T))
214✔
77
    pos = writefixed(buf, 1, x, precision, plus, space, hash, decchar, trimtrailingzeros)
34✔
78
    return String(resize!(buf, pos - 1))
34✔
79
end
80

81
"""
82
    Ryu.writeexp(x, precision, plus=false, space=false, hash=false, expchar=UInt8('e'), decchar=UInt8('.'), trimtrailingzeros=false)
83
    Ryu.writeexp(buf::Vector{UInt8}, pos::Int, x, args...)
84

85
Convert a float value `x` into a scientific notation decimal string.
86
This function allows achieving the `%e` printf format.
87
Note the 2nd method allows passing in a byte buffer and position directly; callers must ensure the buffer has sufficient room to hold the entire decimal string.
88

89
Various options for the output format include:
90
  * `plus`: for positive `x`, prefix decimal string with a `'+'` character
91
  * `space`: for positive `x`, prefix decimal string with a `' '` character; overridden if `plus=true`
92
  * `hash`: whether the decimal point should be written, even if no additional digits are needed for precision
93
  * `precision`: minimum number of significant digits to be included in the decimal string; extra `'0'` characters will be added for padding if necessary
94
  * `expchar`: character to use exponent component in scientific notation
95
  * `decchar`: decimal point character to be used
96
  * `trimtrailingzeros`: whether trailing zeros should be removed
97
"""
98
function writeexp(x::T,
×
99
    precision::Integer,
100
    plus::Bool=false,
101
    space::Bool=false,
102
    hash::Bool=false,
103
    expchar::UInt8=UInt8('e'),
104
    decchar::UInt8=UInt8('.'),
105
    trimtrailingzeros::Bool=false) where {T <: Base.IEEEFloat}
106
    buf = Base.StringVector(precision + neededdigits(T))
×
107
    pos = writeexp(buf, 1, x, precision, plus, space, hash, expchar, decchar, trimtrailingzeros)
×
108
    return String(resize!(buf, pos - 1))
×
109
end
110

111
function Base.show(io::IO, x::T, forceuntyped::Bool=false, fromprint::Bool=false) where {T <: Base.IEEEFloat}
916,161✔
112
    compact = get(io, :compact, false)::Bool
2,067,721✔
113
    buf = Base.StringVector(neededdigits(T))
476,522✔
114
    typed = !forceuntyped && !compact && get(io, :typeinfo, Any) != typeof(x)
485,225✔
115
    pos = writeshortest(buf, 1, x, false, false, true, -1,
476,522✔
116
        (x isa Float32 && !fromprint) ? UInt8('f') : UInt8('e'), false, UInt8('.'), typed, compact)
117
    write(io, resize!(buf, pos - 1))
953,044✔
118
    return
476,522✔
119
end
120

121
function Base.string(x::T) where {T <: Base.IEEEFloat}
712✔
122
    buf = Base.StringVector(neededdigits(T))
712✔
123
    pos = writeshortest(buf, 1, x, false, false, true, -1,
712✔
124
        UInt8('e'), false, UInt8('.'), false, false)
125
    return String(resize!(buf, pos - 1))
712✔
126
end
127

128
Base.print(io::IO, x::Union{Float16, Float32}) = show(io, x, true, true)
10✔
129

130
end # module
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc