Jon Snow
Jon Snow

Reputation: 11892

Sprintf floating point number behaviour change from Ruby 2.3 to 2.4

With Ruby 2.3.7,

'%.2g' % 1.05

returns "1.1"

But with Ruby 2.4.0

'%.2g' % 1.05

returns "1.0"

Why is this behaviour different? Thanks

Upvotes: 0

Views: 31

Answers (2)

spickermann
spickermann

Reputation: 106982

In Ruby 2.4 the default strategy of Float#round was changed to Gaussian rounding.

Upvotes: 2

Jon Snow
Jon Snow

Reputation: 11892

"This was an early effort by Nobu to handle cases where floating point numbers rounded inconsistently with Kernel#sprintf in ruby-2.3.3"

https://blog.heroku.com/ruby-2-4-features-hashes-integers-rounding#actual-rounding-changes

Upvotes: 0

Related Questions