A possible issue with untyped literal numbers

64 views
Skip to first unread message

Marc Vertes

unread,
Oct 31, 2019, 1:13:48 PM10/31/19
to golang-nuts
Hello,

I see that the following code

func main() { println(100 % 1e2) }

is rejected by the compiler with "invalid operation: operator % not defined on untyped float"

but the following

func main() { i := 100; println(i % 1e2) }

is happily accepted. Should I open an issue on this ? What should the normal behaviour be ?

Thanks!

alanfo

unread,
Oct 31, 2019, 2:07:34 PM10/31/19
to golang-nuts
This in fact is correct behavior.

As 100 is an untyped integer constant, 1e2 can not be implicitly converted to the same type and so the % operation fails.

You can fix it with:

func main() { println(int(100) % 1e2) }

Now 1e2 is converted to 'int' and all is well :)

Alan

Marc Vertes

unread,
Oct 31, 2019, 2:44:56 PM10/31/19
to golang-nuts
Ok, thank you for your response.

Marc
Reply all
Reply to author
Forward
0 new messages