From: Robert Klemme Date: 2009-04-30T04:35:06+09:00 Subject: Re: Detecting -0.0 On 29.04.2009 17:39, Jeff Schwab wrote: > Robert Klemme wrote: >> On 28.04.2009 22:27, Jeff Schwab wrote: >>> Thomas B. wrote: >>>> Hello. >>>> >>>> I have a simple question - how to detect if a variable contains a Float >>>> of value -0.0? Is there any special method for this, or any comparison >>>> that distinguishes negative zero from a the regular zero? >>> >>> NB: I'm not sure how portable this is. >>> >>> def negative_zero?(number) >>> inverse = 1 / number.to_f >>> inverse < 0 && inverse.infinite? ? true : false >> >> Why do you use the ternary operator to convert a boolean into a boolean? > > I don't. Float#infinite? can return nil, -1, or +1, but never true or > false. Without the ternary, the original client code produces the > following, far less meaningful output, including the blank line: > > false > -1 > > false Well, but all these are perfectly boolean values in Ruby. There is no need to convert the expression other than for display maybe. But in that case I'd do the conversion outside the method as it does not add any semantics to the method implementation but costs time. Kind regards robert -- remember.guy do |as, often| as.you_can - without end http://blog.rubybestpractices.com/