Assigning +Inf to a float32 ..

1,491 views
Skip to first unread message

xiio...@gmail.com

unread,
Sep 10, 2016, 9:55:32 PM9/10/16
to golang-nuts
ok I want to assign +Inf as a float32.

Currently I work around like this :

package main

import (
"fmt"
)

func main() {
var a,b,c float32
a=1
b=0
//c=float32(1.0)/float32(0.0) //can't do it
//c=+Inf //doesn't work - not a keyword
c=a/b //kludge
fmt.Println (a,b,c)
}

Couldn't find a way to do this - not even in the math package .. (I see that +Inf is in the math package but it is not exported) ie lines 8-10 in math/bits.go

Am I missing something obvious.?

In https://golang.org/ref/spec#Numeric_types it states "float32   the set of all IEEE-754 32-bit floating-point numbers" 

If not is a request that +Inf etc be exported from math reasonable  ? (also signed zeros etc)

Brad Fitzpatrick

unread,
Sep 11, 2016, 1:58:04 AM9/11/16
to xiio...@gmail.com, golang-nuts
Not beautiful, but...


func main() {
i32 := math.Float32frombits(0x7F800000)
fmt.Printf("%T = %v", i32, i32)
}

// float32 = +Inf



--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Dave Cheney

unread,
Sep 11, 2016, 1:59:58 AM9/11/16
to golang-nuts, xiio...@gmail.com
https://play.golang.org/p/RthMnILvkP

func main() {
inf := float32(math.Inf(+1))
fmt.Println(inf)
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts...@googlegroups.com.

John Souvestre

unread,
Sep 11, 2016, 3:40:24 AM9/11/16
to golang-nuts

This does beg the question:  Why is there no math.NaN() function?

 

John

    John Souvestre - New Orleans LA

John Souvestre

unread,
Sep 11, 2016, 4:37:02 AM9/11/16
to golang-nuts

Ignore.  math.NaN() is there!  J

 

John

    John Souvestre - New Orleans LA

 

From: John Souvestre [mailto:Jo...@Souvestre.com]
Sent: 2016 September 11, Sun 02:39
To: 'golang-nuts'
Subject: RE: [go-nuts] Assigning +Inf to a float32 ..

 

This does beg the question:  Why is there no math.NaN() function?

 

John

    John Souvestre - New Orleans LA

 

From: golan...@googlegroups.com [mailto:golan...@googlegroups.com] On Behalf Of Dave Cheney
Sent: 2016 September 11, Sun 01:00
To: golang-nuts
Cc: xiio...@gmail.com
Subject: Re: [go-nuts] Assigning +Inf to a float32 ..

 

xiio...@gmail.com

unread,
Sep 11, 2016, 8:46:39 AM9/11/16
to golang-nuts, xiio...@gmail.com
afaict there is no support in /math to generate a signed zero. ?

Ian Lance Taylor

unread,
Sep 11, 2016, 8:50:55 AM9/11/16
to Xio Fen, golang-nuts
On Sun, Sep 11, 2016 at 5:46 AM, <xiio...@gmail.com> wrote:
> afaict there is no support in /math to generate a signed zero. ?

math.Copysign(0, -1)

Ian

xiio...@gmail.com

unread,
Sep 11, 2016, 9:07:05 AM9/11/16
to golang-nuts, xiio...@gmail.com
ok thanks - that seems to be them all

I think the library would benefit from more 'obvious' , built in constant, or single parameterless function to generate these - more commonly as values to test output against, rather than inputs.

I'll put in that request and see if it gets picked up.

Also curious as why the implementation is as math.NaN() rather than just math.NaN (function/method not constant). It doesn't seem to help with a potential interfaces.  

Ian Lance Taylor

unread,
Sep 11, 2016, 9:25:29 AM9/11/16
to Xio Fen, golang-nuts
On Sun, Sep 11, 2016 at 6:06 AM, <xiio...@gmail.com> wrote:
>
> Also curious as why the implementation is as math.NaN() rather than just
> math.NaN (function/method not constant). It doesn't seem to help with a
> potential interfaces.

Go constants are untyped, more or less unlimited, and do not support
NaN or infinity or negative zero. There would be no simple way to
make math.NaN a constant.

Ian

xiio...@gmail.com

unread,
Sep 11, 2016, 9:32:13 AM9/11/16
to golang-nuts, xiio...@gmail.com
Sorry my terminology error.

I meant exported variable, not constant.

Jakob Borg

unread,
Sep 11, 2016, 11:34:23 AM9/11/16
to golang-nuts, xiio...@gmail.com
Exported variables can be changed, which is unnecessary to allow in this case.

//jb

--

Michael Jones

unread,
Sep 12, 2016, 6:39:09 PM9/12/16
to xiio...@gmail.com, golang-nuts

Actually, you would also want a way to say “which type of NaN” as there are two classes and several subclasses. I wrote code for this a while back and posted it here (IIRC).

 


Date: Sunday, September 11, 2016 at 6:32 AM
To: golang-nuts <golan...@googlegroups.com>
Cc: <xiio...@gmail.com>

--

xiio...@gmail.com

unread,
Sep 12, 2016, 8:22:54 PM9/12/16
to golang-nuts, xiio...@gmail.com


On Monday, 12 September 2016 23:39:09 UTC+1, Michael Jones wrote:

Actually, you would also want a way to say “which type of NaN” as there are two classes and several subclasses. I wrote code for this a while back and posted it here (IIRC).



yes  -I'm aware of 'signalling' and 'quiet' NaNs at least

It seems that Golang does currently fully implement/expose all of the IEE754 standards .. the problem is a little deeper than my original issue about being able to simply assign -Inf, -0 etc.

Short of doing my own bit tests there doesn't seem to be any way to detect different NaNs in go ?

I think these should be core language features, just as float32 and float64 are core language features.
Reply all
Reply to author
Forward
0 new messages