开发者

Is this sprintf call broken in 1.9.2?

At the bottom of Kernel#sprintf's documentation it has this example:

sprintf("%u", -123) #=> "..4294967173"

When I do this on 1.8.7 I get this result:

"..18446744073709551493"

That's similar to the expected output, but is definitely not it.

When I do it on 1.9.2 however, I get the same number back as a string:

ruby-1.9.2-p136 :001 > sprintf("%u", -123)
 => "-123"开发者_StackOverflow中文版

So there's actually two questions here.

  1. Why would I be getting a different output to what the documentation says I would and

  2. Why would Ruby 1.9.2 behave differently to 1.8.7, even when the documentation is quite specific about the output type?


The 1.8 documentation says that %u is for a 32-bit unsigned integer, and you're seeing it on a 64-bit unsigned integer, so that behavior is off. The 1.9 documentation says that %u is the same as %d, so that behavior is correct.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜