Why is `math.Sin` disallowed in a Go constant?

Phlarx :

According to Effective Go, the function math.Sin cannot be used to define a constant because that function must happen at run-time.

What is the reasoning behind this limitation? Floating-point consistency? Quirk of the Sin implementation? Something else?


There is support for this sort of thing in other languages. In C, for example: as of version 4.3, GCC supports compile-time calculation of the sine function. (See section "General Optimizer Improvements").

However, as noted in this blog post by Bruce Dawson, this can cause unexpected issues. (See section "Compile-time versus run-time sin").

Is this a relevant concern in Go? Or is this usage restricted for a different reason?

hobbs :

Go simply lacks the concept. There is no way of marking a function as pure (its return value depends only on its arguments, and it doesn't alter any kind of mutable state or perform I/O), there is no way for the compiler to infer pureness, and there's no attempt to evaluate any expression containing a function call at compile-time (because doing so for anything except a pure function of constant arguments would be a source of weird behavior and bugs, and because adding the machinery needed to make it work right would introduce quite a bit of complexity).

Yes, this is a substantial loss, which forces a tradeoff between code with bad runtime behavior, and code which is flat-out ugly. Go partisans will choose the ugly code and tell you that you are a bad human being for not finding it beautiful.

The best thing you have available to you is code generation. The integration of go generate into the toolchain and the provision of a complete Go parser in the standard library makes it relatively easy to munge code at build time, and one of the things that you can do with this ability is create more advanced constant-folding if you so choose. You still get all of the debuggability peril of code generation, but it's something.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Why is initialization of a constant dependent type in a template parameter list disallowed by the standard?

Why Math.sin`1` works and Math.sin'1' not

Why does Math.sin() delegate to StrictMath.sin()?

Why my client.go cannot access math.go?

Why are single type constraints disallowed in Python?

Why are logical assignment (&=) operators disallowed in Typescript?

Why inheritance from a class with private constructor is disallowed

Why this constant expression is not constant

Why does C define M_2_SQRTPI constant in <math.h>?

Powering a Math constant in Julia is slow

Using math.h for sin function and getting errors don't know why

Why does removing multiprocessing from my program throws an "A float is needed" error in math.sin() function?

Math.radians inside Math.sin

math functions sin and cos for angularjs

Constant struct in Go

Find address of constant in go

Why is $ allowed but $$, or <$> disallowed as an operator (FS0035) and what makes $ special?

Why is an extra qualification for inline definitions of member functions disallowed?

Why is the import of `*.so` files from ZIP files disallowed in Python?

Trying to understand this function from Go, why make a function that always run in constant time and how does this work?

Why does math.Nextafter(2,3) in Go increment by 0.0000000000000004 instead of 0.0000000000000001?

Java: byte to int math sum conversion. Identical operation results in both allowed and disallowed action

When I cast math.NaN() and math.MaxFloat64 to int, Why the result is different between go1.14.2 and go1.17.2?

Why is this considered constant?

Why is enum not a constant expression?

Why is this an illegal constant expression?

Why is this expression not a constant expression?

Why is parameter not a constant expression?

Laravel -- Why the `::class` Constant