Skip to content
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

BigFloat prints differently from Float(32|64) in scientific notation #56941

Open
fredrikekre opened this issue Jan 3, 2025 · 3 comments
Open
Labels
bignums BigInt and BigFloat display and printing Aesthetics and correctness of printed representations of objects.

Comments

@fredrikekre
Copy link
Member

fredrikekre commented Jan 3, 2025

BigFloats always include the + for the exponent and leading 0s but e.g. Float64 does not:

julia> big"1e6"
1.0e+06

julia> 1e6
1.0e6

Noticed by @pjaap in fredrikekre/Runic.jl#13 (comment).

@fredrikekre fredrikekre added bignums BigInt and BigFloat display and printing Aesthetics and correctness of printed representations of objects. labels Jan 3, 2025
@tgross35
Copy link

There is also a discrepancy with %a formatting:

julia> @printf "%a" 4.5
0x1.2p+2
julia> @printf "%a" big(4.5)
0x4.8p+0

It would be nicer if bigfloat normalized the same as regular floats, i.e. always starting with a leading 0x1 rather than any hex digit.

@Seelengrab
Copy link
Contributor

Do we do our own printing for BigFloat, or do we forward to GMP?

@tgross35
Copy link

Ah, it looks like it is MPFR doing the formatting

_snprintf(ptr, siz, str, arg) =
@ccall "libmpfr".mpfr_snprintf(ptr::Ptr{UInt8}, siz::Csize_t, str::Ptr{UInt8};
arg::Ref{BigFloat})::Cint
# Arbitrary constant for a maximum number of bytes we want to output for a BigFloat.
# 8KiB seems like a reasonable default. Larger BigFloat representations should probably
# use a custom printing routine. Printing values with results larger than this ourselves
# seems like a dangerous thing to do.
const __BIG_FLOAT_MAX__ = 8192
function fmt(buf, pos, arg, spec::Spec{T}) where {T <: Floats}
leftalign, plus, space, zero, hash, width, prec =
spec.leftalign, spec.plus, spec.space, spec.zero, spec.hash, spec.width, spec.precision
x = tofloat(arg)
if x isa BigFloat
if isfinite(x)
GC.@preserve buf begin
siz = length(buf) - pos + 1
str = string(spec; modifier="R")
required_length = _snprintf(pointer(buf, pos), siz, str, x)
if required_length > siz
required_length > __BIG_FLOAT_MAX__ &&
throw(ArgumentError("The given BigFloat requires $required_length bytes to be printed, which is more than the maximum of $__BIG_FLOAT_MAX__ bytes supported."))
resize!(buf, required_length + 1)
required_length = _snprintf(pointer(buf, pos), required_length + 1, str, x)
end
required_length > 0 || throw(ArgumentError("The given BigFloat would produce less than the maximum allowed number of bytes $__BIG_FLOAT_MAX__, but still couldn't be printed fully for an unknown reason."))
return pos + required_length
end
end
x = Float64(x)
end
, the dotnet docs have an example that confirms mpfr's behavior https://machinecognitis.github.io/Math.Mpfr.Native/html/8c4f26fd-509c-6557-1907-76d9d7ed7475.htm.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bignums BigInt and BigFloat display and printing Aesthetics and correctness of printed representations of objects.
Projects
None yet
Development

No branches or pull requests

3 participants