From: Jeff Schwab Date: 2009-04-30T00:45:35+09:00 Subject: Re: Detecting -0.0 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 >> 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. "Real" math? Whatever you say. In EE college courses, professors often use -0 to represent the limit of an asymptotic function that approaches zero from the negative side, e.g. the voltage decay of a negatively charged capacitor. The use has nothing to do with computers or IEEE floats. Anyway, take it up with the OP; AFAIK, his question was academic, but maybe he has an interesting use case.