Golang中的负零文字 [英] Negative zero literal in golang

查看:78
本文介绍了Golang中的负零文字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

IEEE754支持负零. 但是这段代码

IEEE754 supports the negative zero. But this code

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

输出

0 +Inf

我期望的地方

-0 -Inf

其他基于float格式基于IEEE754的语言,您可以创建负零文字

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:

Javascript :

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

但是我在Go中找不到对应的内容.

But I couldn't find the equivalent in Go.

如何在go中编写一个负零文字?

How do you write a negative zero literal in go ?

推荐答案

已注册的问题.

碰巧提供了一种解决方案:

And it happens to give a kind of solution :

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

这还不错,因为它显然是指IEEE754定义的标准copysign函数.

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.

这篇关于Golang中的负零文字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆