float type and math functions

132 views
Skip to first unread message

Gabor

unread,
Nov 1, 2010, 3:16:18 PM11/1/10
to golang-nuts
I am impressed with Go and would love to use it for scientific
computing
but unfortunately, I am still hitting strange limitations.

Here I mention only the issue of float types and the math package.
This question was raised several times in this mailing-list.
I can only join and ask again:
It would be so much easier if float would be an alias for float64
and functions of the math package could be used with floats
in a straightforward way.

Namegduf

unread,
Nov 1, 2010, 3:33:54 PM11/1/10
to golan...@googlegroups.com
On Mon, 1 Nov 2010 12:16:18 -0700 (PDT)
Gabor <g...@szfki.hu> wrote:
> It would be so much easier if float would be an alias for float64
> and functions of the math package could be used with floats
> in a straightforward way.

Go does not have types that are "aliases" for each other; even types
differing only in name require conversions between each other.

You might be thinking of C.

Gabor

unread,
Nov 1, 2010, 3:38:49 PM11/1/10
to golang-nuts
OK, then it reads without alias:

IIt would be so much easier if the default float would be 64bit
and functions of the math package could be used with floats
in a straightforward way.


John Asmuth

unread,
Nov 1, 2010, 10:42:35 PM11/1/10
to golang-nuts
I just use float64 as my type when doing anything interesting.

Andrew Gerrand

unread,
Nov 1, 2010, 10:48:28 PM11/1/10
to Gabor, golang-nuts
On 2 November 2010 06:38, Gabor <g...@szfki.hu> wrote:
> OK, then it reads without alias:
>
> IIt would be so much easier if the default float would be 64bit
> and functions of the math package could be used with floats
> in a straightforward way.

Is this just so that you can declare your types as float instead of float64?

Andrew

Jessta

unread,
Nov 2, 2010, 2:06:40 AM11/2/10
to Andrew Gerrand, Gabor, golang-nuts

I think the problem they are pointing out is that
a := 4.0 makes a float.
this float really isn't that useful because most of the math functions
require a float64, so you have to do a lot of casting or explicitly
declare it as a float64.


- jessta

--
=====================
http://jessta.id.au

Cory Mainwaring

unread,
Nov 2, 2010, 10:04:06 AM11/2/10
to Jessta, Andrew Gerrand, Gabor, golang-nuts
float itself seems a bit nonsensical. It's not used in any form of lazy or non-important type declarations, and causes issues with libraries that need a static amount of bits to work with. If you use a float, chances are you need a float64/128 declaration rather than a float declaraction. So, it may make more sense to have a := 4.0 be a float64. (getting rid of architecture dependent types could also be desirable, though that would mess with things too much to be feasible I think)

Gabor

unread,
Nov 6, 2010, 3:27:20 PM11/6/10
to golang-nuts
Of course, one solution is to use float64 everywhere if any math
is needed, and forget about the simple float type completely.

But why should we forget about it, if the main reason of
using floats is to use them as arguments of math functions ?
That way we also lose the elegant x:=value style declarations
in a number of situations (initialization in for, if, switch).
The same applies to the complex type and the cmath package.

I have also found an excellent tutorial by Rob Pike.
In all his function examples ( GoCourseDay1, pages 44-47 )
math.Sqrt(f) was calculated with simple float arguments,
exactly the thing I would feel natural today.
So at least at some point this was acceptable.
Reply all
Reply to author
Forward
0 new messages