Hexadecimal floats

98 views
Skip to first unread message

Jan Mercl

unread,
Jul 26, 2019, 9:21:41 AM7/26/19
to golang-nuts
In a program that generates C, I'm using big.Float.Text
(https://golang.org/pkg/math/big/#Float.Text) to produce the exact
value of the C floating point literal. As it turns out, the function
does not support Inf values. Instead of encoding the exact bits of the
Inf value, the resulting string is simply "Inf".

I'm looking for possibly existing, open source Go code that does the
conversion properly both for C floats (Go float32) and C doubles (Go
float64). So far I was not able to find anything. I can surely roll my
own code, but I'd very much like to use a well tested code if someone
can point me to such.

TIA

Adrian Ho

unread,
Jul 27, 2019, 1:44:33 AM7/27/19
to golan...@googlegroups.com
Section 7.12.4 of the C99 standard says that <math.h> defines the
*INFINITY* macro to represent positive infinity, and the way it's
defined in the standard suggests that negative infinity can be
represented by simply negating that constant. Doing it this way works
portably across all standard C compilers and architectures.

Is there a reason you can't simply post-process Text() output to replace
"Inf" with "INFINITY"? That's just a simple "if" test.

--
Best Regards,
Adrian

Jan Mercl

unread,
Jul 27, 2019, 1:53:03 AM7/27/19
to Adrian Ho, golang-nuts
There are several NaNs, but only a single bit pattern for IEEE754 infinity (modulo the sign bit). So yes, your suggestion should work well. Thank you, I'll give it a try.


--
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...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/golang-nuts/b6efb830-e465-fe37-8be4-27233e27d60f%4003s.net.

Jan Mercl

unread,
Jul 27, 2019, 3:40:49 AM7/27/19
to Adrian Ho, golang-nuts
Reply all
Reply to author
Forward
0 new messages