@@ -475,9 +475,28 @@ const __BIG_FLOAT_MAX__ = 8192
475475 elseif T == Val{' f' } || T == Val{' F' }
476476 newpos = Ryu. writefixed (buf, pos, x, prec, plus, space, hash, UInt8 (' .' ))
477477 elseif T == Val{' g' } || T == Val{' G' }
478+ # C11-compliant general format
478479 prec = prec == 0 ? 1 : prec
479- x = round (x, sigdigits= prec)
480- newpos = Ryu. writeshortest (buf, pos, x, plus, space, hash, prec, T == Val{' g' } ? UInt8 (' e' ) : UInt8 (' E' ), true , UInt8 (' .' ))
480+ # format the value in scientific notation and parse the exponent part
481+ exp = let p = Ryu. writeexp (buf, pos, x, prec)
482+ b1, b2, b3, b4 = buf[p- 4 ], buf[p- 3 ], buf[p- 2 ], buf[p- 1 ]
483+ Z = UInt8 (' 0' )
484+ if b1 == UInt8 (' e' )
485+ # two-digit exponent
486+ sign = b2 == UInt8 (' +' ) ? 1 : - 1
487+ exp = 10 * (b3 - Z) + (b4 - Z)
488+ else
489+ # three-digit exponent
490+ sign = b1 == UInt8 (' +' ) ? 1 : - 1
491+ exp = 100 * (b2 - Z) + 10 * (b3 - Z) + (b4 - Z)
492+ end
493+ flipsign (exp, sign)
494+ end
495+ if - 4 ≤ exp < prec
496+ newpos = Ryu. writefixed (buf, pos, x, prec - (exp + 1 ), plus, space, hash, UInt8 (' .' ), ! hash)
497+ else
498+ newpos = Ryu. writeexp (buf, pos, x, prec - 1 , plus, space, hash, T == Val{' g' } ? UInt8 (' e' ) : UInt8 (' E' ), UInt8 (' .' ), ! hash)
499+ end
481500 elseif T == Val{' a' } || T == Val{' A' }
482501 x, neg = x < 0 || x === - Base. zero (x) ? (- x, true ) : (x, false )
483502 newpos = pos
0 commit comments