Conversion to Complex numbers

72 views
Skip to first unread message

stephen.i...@gmail.com

unread,
Mar 27, 2020, 11:42:08 AM3/27/20
to golang-nuts
Hello,

Conversion to complex numbers should be done with the complex builtin function, specifying a real and imaginary part. However, is casting to a complex type supported or not? Empirically, it seems to me that sometimes it is and sometimes it isn't. More confusing is that this behaviour changed between version 1.13.6 and 1.14.

Using this test program.

package main

import "testing"

// int -> complex

const constInt = 1

func TestConstInt(t *testing.T) {
    _ = complex64(constInt)
}

const constIntExplicit = int(1)

func TestConstIntExplicit(t *testing.T) {
    _ = complex64(constIntExplicit)
}

func TestVarInt(t *testing.T) {
    varInt := 1
    _ = complex64(varInt)
}

func TestVarIntExplicit(t *testing.T) {
    varIntExplicit := int(1)
    _ = complex64(varIntExplicit)
}

// float -> complex

const constFloat = 1

func TestConstFloat(t *testing.T) {
    _ = complex64(constFloat)
}

const constFloatExplicit = float64(1)

func TestConstFloatExplicit(t *testing.T) {
    _ = complex64(constFloatExplicit)
}

func TestVarFloat(t *testing.T) {
    varFloat := 1
    _ = complex64(varFloat)
}

func TestVarFloatExplicit(t *testing.T) {
    varFloatExplicit := float64(1)
    _ = complex64(varFloatExplicit)
}


Summary of results:


1.13.6     
1.14.1
const int y y
const int (explicit type) y n
variable int n n
variable int (explicit type) n n
const float y y
const float (explicit type) y n
variable float n n
variable float (explicit type)      
n n


I wouldn't want to argue too strongly on this point but for consistency, should casting be allowed at all?


Regards

Stephen Illingworth

Brian Candler

unread,
Mar 27, 2020, 11:58:19 AM3/27/20
to golang-nuts
On Friday, 27 March 2020 15:42:08 UTC, stephen.i...@gmail.com wrote:
// float -> complex

const constFloat = 1


Did you mean const constFloat = 1.0 ?

stephen.i...@gmail.com

unread,
Mar 27, 2020, 12:16:18 PM3/27/20
to golang-nuts
Yes. Results are the same.

stephen.i...@gmail.com

unread,
Mar 27, 2020, 12:29:52 PM3/27/20
to golang-nuts
Corrected test program. Results are the same:

package main

import "testing"

// int -> complex

const constInt = 1

func TestConstInt(t *testing.T) {
    _ = complex64(constInt)
}

const constIntExplicit = int(1)

func TestConstIntExplicit(t *testing.T) {
    _ = complex64(constIntExplicit)
}

func TestVarInt(t *testing.T) {
    varInt := 1
    _ = complex64(varInt)
}

func TestVarIntExplicit(t *testing.T) {
    varIntExplicit := int(1)
    _ = complex64(varIntExplicit)
}

// float -> complex

const constFloat = 1.0


func TestConstFloat(t *testing.T) {
    _ = complex64(constFloat)
}

const constFloatExplicit = float64(1.0)


func TestConstFloatExplicit(t *testing.T) {
    _ = complex64(constFloatExplicit)
}

func TestVarFloat(t *testing.T) {
    varFloat := 1.0

    _ = complex64(varFloat)
}

func TestVarFloatExplicit(t *testing.T) {
    varFloatExplicit := float64(1.0)
    _ = complex64(varFloatExplicit)
}

 

Ian Lance Taylor

unread,
Mar 27, 2020, 12:41:43 PM3/27/20
to stephen.i...@gmail.com, golang-nuts
The language spec does not support conversions between ints/floats and
complex numbers. The exact rules are written down at
https://golang.org/ref/spec#Conversions.

But it's interesting that the behavior of typed constants changed
between 1.13 and 1.14. I suspect that was changed in
https://golang.org/cl/187657. The spec is not precisely clear on how
typed constant values should be handled. Please open a bug report for
that case at https://golang.org/issue. Thanks.

Ian
Reply all
Reply to author
Forward
0 new messages