Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Negative zero literal in golang

IEEE754 supports the negative zero. But this code

a  := -0.0
fmt.Println(a, 1/a)

outputs

0 +Inf

where I would have expected

-0 -Inf

Other languages whose float format is based on IEEE754 let you create negative zero literals

Java :

float a = -0f;
System.out.printf("%f %f", a, 1/a); // outputs "-0,000000 -Infinity"

C# :

var a = -0d;
Console.WriteLine(1/a); // outputs "-Infinity"

Javascript :

​var a = -0;
console.log(a, 1/a);​ // logs "0 -Infinity"

But I couldn't find the equivalent in Go.

How do you write a negative zero literal in go ?

like image 905
Denys Séguret Avatar asked Dec 10 '12 15:12

Denys Séguret


People also ask

How can it be negative zero degrees?

At zero kelvin (minus 273 degrees Celsius) the particles stop moving and all disorder disappears. Thus, nothing can be colder than absolute zero on the Kelvin scale. Physicists have now created an atomic gas in the laboratory that nonetheless has negative Kelvin values.

How do you make a negative zero?

One may obtain negative zero as the result of certain computations, for instance as the result of arithmetic underflow on a negative number (other results may also be possible), or −1.0×0.0 , or simply as −0.0 .

Does negative 0 exist?

There is a negative 0, it just happens to be equal to the normal zero. For each real number a, we have a number −a such that a+(−a)=0. So for 0, we have 0+(−0)=0. However, 0 also has the property that 0+b=b for any b.


1 Answers

There is a registered issue.

And it happens to give a kind of solution :

a := math.Copysign(0, -1)

It's not so bad as it obviously refers to the standard copysign function defined by IEEE754.

But this means you need to import a package and this still looks much too heavy for the (admittedly minor and rare) need.

like image 196
Denys Séguret Avatar answered Sep 19 '22 14:09

Denys Séguret