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


Groups > comp.lang.ruby > #3587 > unrolled thread

Re: float precision

Started by"Joshua S." <joshsuggs@gmail.com>
First post2011-04-27 17:32 -0500
Last post2011-04-28 12:37 -0500
Articles 3 — 2 participants

Back to article view | Back to comp.lang.ruby

This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by below is the oldest one visible, not the original post.


Contents

  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

#3587 — Re: float precision

From"Joshua S." <joshsuggs@gmail.com>
Date2011-04-27 17:32 -0500
SubjectRe: float precision
Message-ID<d8b1a7254598249b0ae543fb5672545e@ruby-forum.com>
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/.

[toc] | [next] | [standalone]


#3613

FromBrian Candler <b.candler@pobox.com>
Date2011-04-28 03:04 -0500
Message-ID<6a573846f8c628a3ba44998931503b96@ruby-forum.com>
In reply to#3587
Joshua S. wrote in post #995421:
> 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)

You should be suspicious of a result which indicates it's 5 orders of 
magnitude faster.

>   r.report('regex:') {
>     f4 = float.to_s.match(/(^-?\d+(\.\d{1,2})?)/)[1].to_f
>   }

Ahem, you forgot the "times.times do...end" loop in the benchmark :-)

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

[toc] | [prev] | [next] | [standalone]


#3641

From"Joshua S." <joshsuggs@gmail.com>
Date2011-04-28 12:37 -0500
Message-ID<53a431c9a0ae1c4059888783c1fb15f2@ruby-forum.com>
In reply to#3613
Brian Candler wrote in post #995481:
> Joshua S. wrote in post #995421:
>> 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)
>
> You should be suspicious of a result which indicates it's 5 orders of
> magnitude faster.
>
>>   r.report('regex:') {
>>     f4 = float.to_s.match(/(^-?\d+(\.\d{1,2})?)/)[1].to_f
>>   }
>
> Ahem, you forgot the "times.times do...end" loop in the benchmark :-)

Wow. Yea ... scratch that.


      user     system      total        real
spf  :  1.570000   0.000000   1.570000 (  1.569649)
*100 :  0.330000   0.000000   0.330000 (  0.323864)
*.01 :  0.340000   0.000000   0.340000 (  0.342777)
regex:  4.120000   0.010000   4.130000 (  4.127264)

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

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.ruby


csiph-web