mirror of
https://github.com/gonum/gonum.git
synced 2025-10-09 00:50:16 +08:00
Changed NearestInSpan to NearestWithinSpan and added panic for out of range
This commit is contained in:
11
AUTHORS
Normal file
11
AUTHORS
Normal file
@@ -0,0 +1,11 @@
|
||||
# This is the official list of gonum authors for copyright purposes.
|
||||
# This file is distinct from the CONTRIBUTORS files.
|
||||
# See the latter for an explanation.
|
||||
|
||||
# Names should be added to this file as
|
||||
# Name or Organization <email address>
|
||||
# The email address is not required for organizations.
|
||||
|
||||
# Please keep the list sorted.
|
||||
|
||||
Brendan Tracey <tracey.brendan@gmail.com>
|
23
LICENSE
Normal file
23
LICENSE
Normal file
@@ -0,0 +1,23 @@
|
||||
Copyright ©2013 The gonum Authors. All rights reserved.
|
||||
|
||||
Redistribution and use in source and binary forms, with or without
|
||||
modification, are permitted provided that the following conditions are met:
|
||||
* Redistributions of source code must retain the above copyright
|
||||
notice, this list of conditions and the following disclaimer.
|
||||
* Redistributions in binary form must reproduce the above copyright
|
||||
notice, this list of conditions and the following disclaimer in the
|
||||
documentation and/or other materials provided with the distribution.
|
||||
* Neither the name of the gonum project nor the names of its authors and
|
||||
contributors may be used to endorse or promote products derived from this
|
||||
software without specific prior written permission.
|
||||
|
||||
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
|
||||
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
||||
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
|
||||
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
||||
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
||||
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
||||
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
||||
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
13
floats.go
13
floats.go
@@ -251,14 +251,13 @@ func Nearest(s []float64, v float64) (ind int) {
|
||||
|
||||
// NearestInSpan return the index of a hypothetical vector created
|
||||
// by Span with length n and bounds l and u whose value is closest
|
||||
// to v. Assumes u > l
|
||||
func NearestInSpan(n int, l, u float64, v float64) int {
|
||||
if v < l {
|
||||
return 0
|
||||
}
|
||||
if v > u {
|
||||
return n - 1
|
||||
// to v. Assumes u > l. If the value is greater than u or less than
|
||||
// l, the function will panic.
|
||||
func NearestWithinSpan(n int, l, u float64, v float64) int {
|
||||
if v < l || v > u {
|
||||
panic("floats: value outside span bounds")
|
||||
}
|
||||
|
||||
// Can't guarantee anything about exactly halfway between
|
||||
// because of floating point weirdness
|
||||
return int((float64(n)-1)/(u-l)*(v-l) + 0.5)
|
||||
|
@@ -318,32 +318,31 @@ func TestNearest(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestNearestInSpan(t *testing.T) {
|
||||
ind := NearestInSpan(13, 7, 8.2, -1)
|
||||
if ind != 0 {
|
||||
t.Errorf("Wrong value when below the lower bound. %i found, %i expected", ind, 0)
|
||||
func TestNearestWithinSpan(t *testing.T) {
|
||||
|
||||
if !Panics(func() { NearestWithinSpan(13, 7, 8.2, 10) }) {
|
||||
t.Errorf("Did not panic below lower bound")
|
||||
}
|
||||
ind = NearestInSpan(13, 7, 8.2, 9)
|
||||
if ind != 12 {
|
||||
t.Errorf("Wrong value when above the upper bound. %i found, %i expected", ind, 12)
|
||||
if !Panics(func() { NearestWithinSpan(13, 7, 8.2, 10) }) {
|
||||
t.Errorf("Did not panic above upper bound")
|
||||
}
|
||||
ind = NearestInSpan(13, 7, 8.2, 7.19)
|
||||
ind := NearestWithinSpan(13, 7, 8.2, 7.19)
|
||||
if ind != 2 {
|
||||
t.Errorf("Wrong value when just below the bucket. %i found, %i expected", ind, 2)
|
||||
}
|
||||
ind = NearestInSpan(13, 7, 8.2, 7.21)
|
||||
ind = NearestWithinSpan(13, 7, 8.2, 7.21)
|
||||
if ind != 2 {
|
||||
t.Errorf("Wrong value when just above the bucket. %i found, %i expected", ind, 2)
|
||||
}
|
||||
ind = NearestInSpan(13, 7, 8.2, 7.2)
|
||||
ind = NearestWithinSpan(13, 7, 8.2, 7.2)
|
||||
if ind != 2 {
|
||||
t.Errorf("Wrong value when equal to bucket. %i found, %i expected", ind, 2)
|
||||
}
|
||||
ind = NearestInSpan(13, 7, 8.2, 7.151)
|
||||
ind = NearestWithinSpan(13, 7, 8.2, 7.151)
|
||||
if ind != 2 {
|
||||
t.Errorf("Wrong value when just above halfway point. %i found, %i expected", ind, 2)
|
||||
}
|
||||
ind = NearestInSpan(13, 7, 8.2, 7.249)
|
||||
ind = NearestWithinSpan(13, 7, 8.2, 7.249)
|
||||
if ind != 2 {
|
||||
t.Errorf("Wrong value when just below halfway point. %i found, %i expected", ind, 2)
|
||||
}
|
||||
|
Reference in New Issue
Block a user