-.- --. .-. --..

Minus zero in Ruby and JavaScript

From Daniel Lemire’s recent-ish blog post on this topic:

The ubiquitous IEEE floating-point standard defines two numbers to represent zero, the positive and the negative zeros. You also have the positive and negative infinity. If you compute the inverse of the positive zero, you get the positive infinity. If you compute the inverse of the negative zero, you get the negative infinity.

I wanted to check this out for the most frequent languages I tend to use—Ruby and JavaScript.

First up, Ruby:

minus_zero = -0.0
plus_zero = 0.0

converted = "-0.0".to_f

puts 1.0 / minus_zero
puts 1.0 / plus_zero
puts 1.0 / converted

Output: -Infinity, Infinity, -Infinity

Next, JavaScript:

const minus_zero = -0.0
const plus_zero = 0.0

const converted = parseFloat("-0.0", 10)

console.log(1.0 / minus_zero)
console.log(1.0 / plus_zero)
console.log(1.0 / converted)

Output: -Infinity, Infinity, -Infinity

Both languages (Ruby v. 2.7.1, JavaScript(NodeJS) v. 12.14.x & Chrome 91.x) return the values as expected in the post.