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

g format differs from Python #88

Open
jariji opened this issue Oct 21, 2024 · 1 comment · May be fixed by #91
Open

g format differs from Python #88

jariji opened this issue Oct 21, 2024 · 1 comment · May be fixed by #91

Comments

@jariji
Copy link

jariji commented Oct 21, 2024

I think this might be a bug.

# Format.jl
julia> pyfmt(".3g", 12.34)
"12.340"

# Python 
In: format(12.34, ".3g")
Out: '12.3'
@R3G3N3R4T0R
Copy link

Yes, the implementation is plain wrong. Currently in fmtcore.jl

Format.jl/src/fmtcore.jl

Lines 337 to 341 in 37c24b9

if 1.0e-4 <= ax < 1.0e6
_pfmt_f(out, fs, x)
else
_pfmt_e(out, fs, x)
end

fs is passed without regard on how to handle significant digits. The correct behavior is documented in Python's Format String documentation this repo referenced to.
I will see if I can do anything, currently this gives the correct behavior but I am not sure this is how they do it.

function _pfmt_g(out::IO, fs::FormatSpec, x::AbstractFloat)
    # number decomposition
    ax = abs(x)
    if 1.0e-4 <= ax < 1.0e6
        newprec = fs.prec - floor(Int, log10(x)) - 1
        _pfmt_f(out, FormatSpec(fs ;prec=newprec), x)
    else
        newprec = fs.prec - 1
        _pfmt_e(out, FormatSpec(fs ;prec=newprec), x)
    end
end

@R3G3N3R4T0R R3G3N3R4T0R linked a pull request Mar 3, 2025 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants