findGoldenNumber = function( value, depth )
{
if ( value < 0 )
return null
if ( value > 1 )
return null
const targets = unitGoldenSeries( depth )
const values = targets .map( grfloat )
function checkRange( minIndex, maxIndex ) {
if ( minIndex >= maxIndex )
return targets[ maxIndex ]
else {
const lowDiff = value - values[ minIndex ]
const highDiff = values[ maxIndex ] - value
if ( maxIndex == minIndex + 1 ) {
return ( highDiff < lowDiff )? targets[ maxIndex ] : targets[ minIndex ]
} else {
const midIndex = Math.floor( ( maxIndex + minIndex ) / 2 )
return ( highDiff < lowDiff )? checkRange( midIndex, maxIndex ) : checkRange( minIndex, midIndex )
}
}
}
return checkRange( 0, targets.length -1 )
}