Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.ruby > #3587

Re: float precision

From "Joshua S." <joshsuggs@gmail.com>
Newsgroups comp.lang.ruby
Subject Re: float precision
Date 2011-04-27 17:32 -0500
Organization Service de news de lacave.net
Message-ID <d8b1a7254598249b0ae543fb5672545e@ruby-forum.com> (permalink)
References (1 earlier) <227e84a0-24ff-4984-a93e-98d40f53aac1@d1g2000hsg.googlegroups.com> <Hbzyk.36400$Mh5.1689@bgtnsc04-news.ops.worldnet.att.net> <b7499cfe-f5a8-4245-9504-970bdccb0148@i76g2000hsf.googlegroups.com> <9e3fd2c80809160355r323f6beeqd953d5ccfb06758c@mail.gmail.com> <4fa64434d7ba9283414f65a68d0837e9@ruby-forum.com>

Show all headers | View raw


Not surprisingly, it's extremely quicker to convert to a string and
match a regular expression.

      user     system      total        real
spf  :  1.560000   0.000000   1.560000 (  1.557252)
*100 :  0.320000   0.000000   0.320000 (  0.322573)
*.01 :  0.350000   0.000000   0.350000 (  0.343289)
regex:  0.000000   0.000000   0.000000 (  0.000019)

The expression
(^-?\d+(\.\d{1,2})?) matches all the following:

12
1.2
1.234567890
12.34567890
123.4567890
1234.567890
-12
-1.2
-1.234567890
-12.34567890
-123.4567890
-1234.567890

-----

require 'benchmark'

times = 1000000
float = 9.234234765765765764
Benchmark.bm do |r|
  r.report('spf  :') {
    times.times do
      f1 = sprintf('%.2f' ,float).to_f
    end
  }
  r.report('*100 :') {
    times.times do
      f2 = (100 * float).round/100.0
    end
  }
  r.report('*.01 :') {
    times.times do
      f3 = (100 * float).round*0.01
    end
  }
  r.report('regex:') {
    f4 = float.to_s.match(/(^-?\d+(\.\d{1,2})?)/)[1].to_f
  }
end

-- 
Posted via http://www.ruby-forum.com/.

Back to comp.lang.ruby | Previous | NextNext in thread | Find similar | Unroll thread


Thread

Re: float precision "Joshua S." <joshsuggs@gmail.com> - 2011-04-27 17:32 -0500
  Re: float precision Brian Candler <b.candler@pobox.com> - 2011-04-28 03:04 -0500
    Re: float precision "Joshua S." <joshsuggs@gmail.com> - 2011-04-28 12:37 -0500

csiph-web