Bug #1566

sprintf with %e or %E Format Specifer Incorrectly Capitalises Inf and NaN

Added by Run Paint Run Run almost 5 years ago. Updated about 1 year ago.

Assignee:Akinori MUSHA
Target version:Ruby 1.8.8
ruby -v:ruby 1.8.7 (2008-08-11 patchlevel 72) [i486-linux]


sprintf/String#% use the %e and %E format specifier to determine the case of results which are NaN or Inf. For example:

$ ruby1.8 -ve 'p sprintf("%e", 0.0/0)'
ruby 1.8.7 (2008-08-11 patchlevel 72) [i486-linux]

$ ruby1.8 -ve 'p sprintf("%E", 0.0/0)'
ruby 1.8.7 (2008-08-11 patchlevel 72) [i486-linux]

I suspect this is wrong for the following reasons:

  • 1.9 doesn't capitalise these values, i.e. it always returns Inf or NaN.
  • The rdoc description for the %E field is "Equivalent to `e', but uses an uppercase E to indicate the exponent." Neither Inf or NaN are exponents.
  • The description for the %e field doesn't make any mention of it's output case.
  • JRuby, and I believe Rubinius, behave as 1.9 does.
  • NaN and Inf have specific mathematical meanings; it doesn't make sense to subject them to English capitalisation rules. =end

fix-nan-and-inf-in-sprintf.diff Magnifier - Fix NaN and Inf capitalization in sprintf (507 Bytes) m ., 09/08/2009 07:53 AM


#1 Updated by m . over 4 years ago

The attached patch fixes this by removing the Windows-only ifdef that currently guards the portion of the code doing the formatting of the resulting string.

I'm not sure of why it was guarded in the first place (the code works fine in OS X) so please test this on Linux/other platforms and apply if you consider this change is worth making.

#2 Updated by Nobuyoshi Nakada over 4 years ago

  • Status changed from Open to Assigned
  • Assignee set to Akinori MUSHA
  • Target version changed from Ruby 1.8.6 to Ruby 1.8.8



#3 Updated by Yui NARUSE about 1 year ago

  • Description updated (diff)
  • Status changed from Assigned to Rejected

1.8's sprintf is a simple wrapper of sprintf(3).
sprintf(3)'s e/E behaves as you said.
See also http://pubs.opengroup.org/onlinepubs/009695399/functions/fprintf.html

Also available in: Atom PDF