FAQ
Hi,

I want to define this linear function y=f(x), so that

y = (y1+y2)/(x1+x2)*(x-x1)+y1

So you can see, to properly defined the y=f(x), four more parameters are
needed, x1, y1, x2, & y2.

I can define this linear function to take them all, apart from the x,
however, my concern is that then those x1, y1, x2, & y2 will be treated
exactly as x. I mean, the x1, y1, x2, & y2, are fixed value when the
program starts, and passed in from variables. Will they be calculated each
time when I call f()?

Is there any mechanize to tell this linear function that the x1, y1, x2, &
y2 are "constants" (so that (y1+y2)/(x1+x2) will be only calculated once
between each calls)?

Thanks

--
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+unsubscribe@googlegroups.com.

## Search Discussions

•  at Feb 22, 2015 at 3:25 pm ⇧
you can use a closure:

http://play.golang.org/p/R59_F87f_A

func NewFunc(x1, y1, x2, y2 float64) func(float64) float64 {
y12 := y1 + y2
x12 := x1 + x2
r := y12 / x12
return func(x float64) float64 {
return r*(x-x1) + y1
}
}

func main() {
f := NewFunc(1, 2, 3, 4)
fmt.Printf("f(2) = %v\n", f(2))
}

hth,
-s

On Sun, Feb 22, 2015 at 4:12 PM, Tong Sun wrote:
Hi,

I want to define this linear function y=f(x), so that

y = (y1+y2)/(x1+x2)*(x-x1)+y1

So you can see, to properly defined the y=f(x), four more parameters are
needed, x1, y1, x2, & y2.

I can define this linear function to take them all, apart from the x,
however, my concern is that then those x1, y1, x2, & y2 will be treated
exactly as x. I mean, the x1, y1, x2, & y2, are fixed value when the program
starts, and passed in from variables. Will they be calculated each time when
I call f()?

Is there any mechanize to tell this linear function that the x1, y1, x2, &
y2 are "constants" (so that (y1+y2)/(x1+x2) will be only calculated once
between each calls)?

Thanks

--
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
--
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+unsubscribe@googlegroups.com.
•  at Feb 22, 2015 at 3:28 pm ⇧
No, but you can use a closure:
func F(x1, x2, y1, y2 float64) func(float64) float64 {
return func(x float64) float64 {
return (x1-x)....
}
}

--
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+unsubscribe@googlegroups.com.

## Related Discussions

Discussion Overview
 group golang-nuts categories go posted Feb 22, '15 at 3:12p active Feb 22, '15 at 3:28p posts 3 users 3 website golang.org

### 3 users in discussion

Content

People

Support

Translate

site design / logo © 2022 Grokbase