From bug-request at octave dot org Fri Feb 4 13:11:53 2005 Subject: bug in the %g format for sprintf From: "John W. Eaton" To: Thorsten Meyer Cc: bug at octave dot org Date: Fri, 4 Feb 2005 13:16:13 -0600 On 4-Feb-2005, Thorsten Meyer wrote: | Bug report for Octave 2.1.64 configured for i386-pc-linux-gnu | | Description: | ----------- | The %m.ng format string gives the wrong number of digits in sprintf. | | Repeat-By: | --------- | | GNU Octave, version 2.1.64 (i386-pc-linux-gnu). | Copyright (C) 2004 John W. Eaton. | This is free software; see the source code for copying conditions. | There is ABSOLUTELY NO WARRANTY; not even for MERCHANTIBILITY or | FITNESS FOR A PARTICULAR PURPOSE. For details, type `warranty'. | | Additional information about Octave is available at http://www.octave.org. | | Please contribute if you find this software useful. | For more information, visit http://www.octave.org/help-wanted.html | | Report bugs to (but first, please read | http://www.octave.org/bugs.html to learn how to write a helpful report). | | octave:1> sprintf("%5.0g", 10.23) <====================== | ans = 1e+01 | octave:2> sprintf("%5.1g", 10.23) <====================== | ans = 1e+01 | octave:3> sprintf("%5.2g", 10.23) <====================== | ans = 10 | octave:4> sprintf("%5.3g", 10.23) <====================== | ans = 10.2 | octave:5> sprintf("%5.4g", 10.23) <====================== | ans = 10.23 | octave:6> sprintf("%5.5g", 10.23) | ans = 10.23 | octave:7> sprintf("%5.4f", 10.23) | ans = 10.2300 | octave:8> sprintf("%5.3f", 10.23) | ans = 10.230 | octave:9> sprintf("%5.2f", 10.23) | ans = 10.23 | octave:10> sprintf("%5.1f", 10.23) | ans = 10.2 I think you are misunderstanding the meaning of the "precision" number in the "g" format. In that case, it is the number of significant figures to display. This is stated clearly in the "Floating-Point Conversions" section of the Octave manual and is consistent with the way C behaves. jwe ------------------------------------------------------------- Octave is freely available under the terms of the GNU GPL. Octave's home on the web: http://www.octave.org How to fund new projects: http://www.octave.org/funding.html Subscription information: http://www.octave.org/archive.html -------------------------------------------------------------