How% g works in printf

Description %gsays

Use the shortest view: %eor%f

for example, it is 544666.678written as 544667if used %.6g, which is excellent.

But the same number is written as 5.4467E+5when used %.5g.

Why use exponential notation ( %e) here, and 544670( %f) is shorter in length.

Can anyone help me understand? This is mistake?

Similarly, it is 44.35written as 44.4when used %.1f, which is excellent. but is 44.55recorded as 44.5when used %.1f. Why is it not written how 44.6? This is mistake?

+4
source share
1

:% e % f .

C11 7.21.6.1 fprintf

A double , , f e ( f e G), . P if, , 6, , 1, . , E X:

- P > X ≥ −4, f ( f) P − (X + 1).

- e ( e) P − 1.

, #, , , .

, %.5g 544666.678, P 5, X 5, e, P > X ≥ −4 .

+3

Source: https://habr.com/ru/post/1623378/


All Articles