-
Notifications
You must be signed in to change notification settings - Fork 7
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
Hh/general numbers #52
base: master
Are you sure you want to change the base?
Changes from all commits
d2dafff
3c88392
ed836bd
4a958ee
7d43657
874d042
cf67f99
4479a8a
d07f241
314a4fb
38e386a
82b9b43
befc01c
ab0bdac
7598556
0233e95
9e1d553
0eb2a8c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
# core formatting functions | ||
export fmt_Number | ||
|
||
### auxiliary functions | ||
|
||
|
@@ -262,3 +263,42 @@ function _pfmt_specialf(out::IO, fs::FormatSpec, x::AbstractFloat) | |
end | ||
end | ||
|
||
function _pfmt_Number_f(out::IO, fs::FormatSpec, x::Number, _pf::Function) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. These still have the issues that I mentioned before. |
||
fsi = FormatSpec(fs, width = -1) | ||
f = x->begin | ||
fx = AbstractFloat(x) # not float(x), this should error out, if conversion is not possible | ||
io = IOBuffer() | ||
_pf(io, fsi, fx) | ||
String(take!(io)) | ||
end | ||
s = fmt_Number(x, f) | ||
_pfmt_s(out, FormatSpec(fs, tsep = false), s) | ||
end | ||
|
||
function _pfmt_Number_i(out::IO, fs::FormatSpec, x::Number, op::Op, _pf::Function) where {Op} | ||
fsi = FormatSpec(fs, width = -1) | ||
f = x->begin | ||
ix = Integer(x) | ||
io = IOBuffer() | ||
_pf(io, fsi, ix, op) | ||
String(take!(io)) | ||
end | ||
s = fmt_Number(x, f) | ||
_pfmt_s(out, FormatSpec(fs, tsep = false), s) | ||
end | ||
|
||
function _pfmt_i(out::IO, fs::FormatSpec, x::Number, op::Op) where {Op} | ||
_pfmt_Number_i(out, fs, x, op, _pfmt_i) | ||
end | ||
|
||
function _pfmt_f(out::IO, fs::FormatSpec, x::Number) | ||
_pfmt_Number_f(out, fs, x, _pfmt_f) | ||
end | ||
|
||
function _pfmt_e(out::IO, fs::FormatSpec, x::Number) | ||
_pfmt_Number_f(out, fs, x, _pfmt_e) | ||
end | ||
|
||
function fmt_Number(x::Complex, f::Function) | ||
s = f(real(x)) * (imag(x) >= 0 ? " + " : " - ") * f(abs(imag(x))) * "im" | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -164,20 +164,38 @@ _srepr(x) = repr(x) | |
_srepr(x::AbstractString) = x | ||
_srepr(x::AbstractChar) = string(x) | ||
_srepr(x::Enum) = string(x) | ||
@static if VERSION < v"1.2.0-DEV" | ||
_srepr(x::Irrational{sym}) where {sym} = string(sym) | ||
end | ||
|
||
function printfmt(io::IO, fs::FormatSpec, x) | ||
cls = fs.cls | ||
ty = fs.typ | ||
if cls == 'i' | ||
ix = Integer(x) | ||
local ix | ||
try | ||
ix = Integer(x) | ||
catch | ||
ix = x | ||
end | ||
ty == 'd' || ty == 'n' ? _pfmt_i(io, fs, ix, _Dec()) : | ||
ty == 'x' ? _pfmt_i(io, fs, ix, _Hex()) : | ||
ty == 'X' ? _pfmt_i(io, fs, ix, _HEX()) : | ||
ty == 'o' ? _pfmt_i(io, fs, ix, _Oct()) : | ||
_pfmt_i(io, fs, ix, _Bin()) | ||
elseif cls == 'f' | ||
fx = float(x) | ||
if isfinite(fx) | ||
local fx, nospecialf | ||
try | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same as above - if it can't get converted to something that is |
||
fx = float(x) | ||
catch | ||
fx = x | ||
end | ||
try | ||
nospecialf = isfinite(fx) | ||
catch | ||
nospecialf = true | ||
end | ||
if nospecialf | ||
ty == 'f' || ty == 'F' ? _pfmt_f(io, fs, fx) : | ||
ty == 'e' || ty == 'E' ? _pfmt_e(io, fs, fx) : | ||
error("format for type g or G is not supported yet (use f or e instead).") | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For now, please remove the separate
ComplexInteger
,ComplexFloat
,ComplexRational
default specs.