nl_tokens: Calculate the scale / offset for an array of numbers . <pre > If signed : then max value unpacked = 2^ ( n - 1 ) - 1 packed min value unpacked = - ( 2^ ( n - 1 ) - 1 ) packed note that - 2^ ( n - 1 ) is unused and a good place to map missing values by solving 2 eq in 2 unknowns we get : scale = ( max - min ) / ( 2^n - 2 ) offset = ( max + min ) / 2 If unsigned then max value unpacked = 2^n - 1 packed min value unpacked = 0 packed and : scale = ( max - min ) / ( 2^n - 1 ) offset = min One could modify this to allow a holder for missing values . < / pre >
pl_tokens: public static MAMath . ScaleOffset calcScaleOffsetSkipMissingData ( Array a , double missingValue , int nbits ) { MAMath . MinMax minmax = getMinMaxSkipMissingData ( a , missingValue ) ; if ( a . isUnsigned ( ) ) { long size = ( 1L << nbits ) - 1 ; double offset = minmax . <mask> ; double scale = ( minmax . max - minmax . min ) / size ; return new ScaleOffset ( scale , offset ) ; } else { long size = ( 1L << nbits ) - 2 ; double offset = ( minmax . max + minmax . min ) / 2 ; double scale = ( minmax . max - minmax . min ) / size ; return new ScaleOffset ( scale , offset ) ; } }