syntax Float equality within epsilon

ruby

    Sponsored Links

    Next

  • 1. Finding File source
    Hi, I've been trying to locate the source for File.open and I'm having no luck. I looked under /usr/local/lib/ruby/1.8/ and found no File.rb or even IO.rb. I did a find /usr/local/lib/ruby -name "*.rb" -exec grep "class File" {} \; -print and it only turns up in a pertty print and ftools source. Are the core classes only defined in compiled binary perhaps? If so, I have another question. Using RoR I'm running into a problem due to too many open files. I want to find the File.open source (or IO.open maybe) and add in some sort of debugging so I can log every file being opened to help debug the issue. Is there a way I can do this without going to the actual source? Thanks for any advice.

syntax Float equality within epsilon

Postby unbewusst.sein » Fri, 23 Nov 2007 17:38:38 GMT

i need to test float numbers within an epsilon, then i've extended the
Float clas like that :

class Float
  def ===( aFloat, eps = 1.0e-10)
    begin
      clazz = aFloat.class.to_s
      raise "Argument \"#{aFloat}\" must be a Float (being of
#{clazz})." if clazz != "Float"
      ( self > aFloat - eps ) && ( self < aFloat + eps )
    rescue
      puts "An error occurred: #{$!}"
      nil
    end
  end
end


this works as expected except when i want not to use the default value
for eps where i couldn't find the correct syntax :
a = 1.000000001
b = 1.00000000012
p ( a ===( b, 0.001) ).to_s
gave me :
[...] syntax error, unexpected ')', expecting tCOLON2 or '[' or '.'
p ( a ===( b, 0.001) ).to_s
                    ^

why ???

-- 
Une Bue

Re: syntax Float equality within epsilon

Postby Phrogz » Sat, 24 Nov 2007 13:16:34 GMT

On Nov 22, 1:38 am,  XXXX@XXXXX.COM  (Une



Some operator methods have syntax sugar that prevents them from taking
multiple arguments using simple syntax. You can get around this,
albeit with a slightly less elegant syntax:

irb(main):013:0> class Float
irb(main):014:1>   def ===( a, b )
irb(main):015:2>     p a, b
irb(main):016:2>   end
irb(main):017:1> end
=> nil
irb(main):018:0> a = 5.4
=> 5.4
irb(main):019:0> a.send( :===, 42, 73 )
42
73
=> nil

If this is a case you'd use often, I suggest simply giving it a normal
method name. that you can call without using #send

Re: syntax Float equality within epsilon

Postby yermej » Sat, 24 Nov 2007 15:06:34 GMT




It can still be called without send. In place of a.send... in the
above example use:

a.===(42, 73)

Still not pretty, but a bit shorter.

Jeremy

Re: syntax Float equality within epsilon

Postby unbewusst.sein » Sat, 24 Nov 2007 19:25:06 GMT




fine, thanks this works great to me !

-- 
Une Bue

Similar Threads:

1.Testing Float equality (was: Negative Float conversion to Fixnum)

2.float equality

Hi,

sorry if this is a very naive question (I'm new to ruby), but I
haven't found an explication yet. When comparing to floats in ruby I
came across this:

>> a = 0.1
=> 0.1
>> b = 1 - 0.9
=> 0.1
>> a == b
=> false
>> a > b
=> true
>> a < b
=> false
>> a <=> b
=> 1

I'm a bit lost here, shouldn't (0.1) and (1 - 0.9) be equals regarding
the == operator? I also found that for example 0.3 == (0.2 + 0.1)
returns false, etc.

guille

PD: I'm using ruby 1.8.6 (2008-03-03 patchlevel 114) [universal-darwin9.0]

3.syntax error for a case within a module

4.[rcr] epsilon string escaping in %W()

problem:

often I use the %W(a b c d e f) for quickly making an array of strings.
sometimes I need an empty string in that array, and I have to
change the code into %W(a b c) + [''] + %W(d e f).
which doesn't look nice.. and makes me do silly stuff instead.


suggestion:
It would be really nice if it was easier to make empty strings within %W.

for instance slash E could be the empty string
%W(a \E b)  #=> ["a", "", "b"]

If no spacing in between.. then output would simply be
%W(a\Eb)  #=> ["ab"]



5.converting a signed float into unsigned float

6. floating point problem... floating indeed :(

7. Float to Fixnum (was: Negative Float conversion to Fixnum)

8. Float Arithmetic: Return A Fixnum When Float == Fixnum



Return to ruby

 

Who is online

Users browsing this forum: No registered users and 42 guest