Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
19
Task/Map-range/Go/map-range-1.go
Normal file
19
Task/Map-range/Go/map-range-1.go
Normal file
|
|
@ -0,0 +1,19 @@
|
|||
package main
|
||||
|
||||
import "fmt"
|
||||
|
||||
type rangeBounds struct {
|
||||
b1, b2 float64
|
||||
}
|
||||
|
||||
func mapRange(x, y rangeBounds, n float64) float64 {
|
||||
return y.b1 + (n - x.b1) * (y.b2 - y.b1) / (x.b2 - x.b1)
|
||||
}
|
||||
|
||||
func main() {
|
||||
r1 := rangeBounds{0, 10}
|
||||
r2 := rangeBounds{-1, 0}
|
||||
for n := float64(0); n <= 10; n += 2 {
|
||||
fmt.Println(n, "maps to", mapRange(r1, r2, n))
|
||||
}
|
||||
}
|
||||
37
Task/Map-range/Go/map-range-2.go
Normal file
37
Task/Map-range/Go/map-range-2.go
Normal file
|
|
@ -0,0 +1,37 @@
|
|||
package main
|
||||
|
||||
import "fmt"
|
||||
|
||||
type rangeBounds struct {
|
||||
b1, b2 float64
|
||||
}
|
||||
|
||||
func newRangeMap(xr, yr rangeBounds) func(float64) (float64, bool) {
|
||||
// normalize direction of ranges so that out-of-range test works
|
||||
if xr.b1 > xr.b2 {
|
||||
xr.b1, xr.b2 = xr.b2, xr.b1
|
||||
yr.b1, yr.b2 = yr.b2, yr.b1
|
||||
}
|
||||
// compute slope, intercept
|
||||
m := (yr.b2 - yr.b1) / (xr.b2 - xr.b1)
|
||||
b := yr.b1 - m*xr.b1
|
||||
// return function literal
|
||||
return func(x float64) (y float64, ok bool) {
|
||||
if x < xr.b1 || x > xr.b2 {
|
||||
return 0, false // out of range
|
||||
}
|
||||
return m*x + b, true
|
||||
}
|
||||
}
|
||||
|
||||
func main() {
|
||||
rm := newRangeMap(rangeBounds{0, 10}, rangeBounds{-1, 0})
|
||||
for s := float64(-2); s <= 12; s += 2 {
|
||||
t, ok := rm(s)
|
||||
if ok {
|
||||
fmt.Printf("s: %5.2f t: %5.2f\n", s, t)
|
||||
} else {
|
||||
fmt.Printf("s: %5.2f out of range\n", s)
|
||||
}
|
||||
}
|
||||
}
|
||||
12
Task/Map-range/Go/map-range.go
Normal file
12
Task/Map-range/Go/map-range.go
Normal file
|
|
@ -0,0 +1,12 @@
|
|||
include "NSLog.incl"
|
||||
|
||||
local fn MapRange( s as double, a1 as double, a2 as double, b1 as double, b2 as double ) as double
|
||||
end fn = b1+(s-a1)*(b2-b1)/(a2-a1)
|
||||
|
||||
NSInteger i
|
||||
|
||||
for i = 0 to 10
|
||||
NSLog( @"%2d maps to %5.1f", i, fn MapRange( i, 0, 10, -1, 0 ) )
|
||||
next
|
||||
|
||||
HandleEvents
|
||||
Loading…
Add table
Add a link
Reference in a new issue