From: Robert Klemme Date: 2009-04-29T06:55:02+09:00 Subject: Re: Detecting -0.0 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? > end > > [ 0.0, # false > -0.0, # true > -1.0, # false > 1.0 # false > ].each do |f| > puts negative_zero?(f) > end My 0.02 EUR: normally there should not be any distinction between 0.0 and -0.0. Even though computer math is not the same as real math, the distinction does not seem to make sense to me. Kind regards robert