Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //! These are in std_functions_prototypes.zh for std.zh (ZC 2.5)
- //! Utilities for manipulating values in ZCs floating point fixed madness.
- //Utilities to set bits (flags) using all decimal places.
- //These allows using 30 bits.
- //Sets a flag in the decimal portion of a value.
- //The maximum is 10011100001111, but the REALISTIC maximum is 1111111111111 (8191).
- int SetDecimalFlag(int var, int flag, bool state){
- int v = GetLowFloat(var);
- if ( state ) v |= flag;
- else v &= ~flag;
- return SetLowFloat(var,v);
- }
- //Extracts the binary value of one bit of a decimal portion of a value.
- bool GetDecimalFlag(int var, int flag){
- int v = GetLowFloat(var);
- return ( v & flag ) != 0;
- }
- //Sets a flag int he integer portion of a value.
- //The maximum is 110100011011011011, but the REALISTIC maximum is 11111111111111111 (131,071).
- int SetIntegerFlag(int var, int flag, bool state){
- int v = GetHighFloat(var);
- if ( state ) v |= flag;
- else v &= ~flag;
- return SetHighFloat(var,v);
- }
- //Extracts the binary value of one bit of the integer portion of a value.
- bool GetIntegerFlag(int var, int flag){
- int v = GetHighFloat(var);
- return ( v & flag ) != 0;
- }
- //! Utilities to set and get base-10 values to the integer, or decimal portions of a value; or a range therein.
- //Accepts two inputs, and returns a float where the integer portion
- //is the first inout, and the decimal portion is the second.
- //Inputs will be automatically truncated, and scaled, as needed.
- int SetHighLowFloat(int integer_side, int decimal_side){
- if ( decimal_side >= 0 )
- decimal_side = (decimal_side << 0)/10000;
- if ( integer_side < 0 ) integer_side = integer_side*10000;
- else integer_side = integer_side << 0;
- return integer_side+decimal_side;
- }
- //Accepts a variable, and a value as input, and returns the variable
- //with the decimal portion replaced with 'value'.
- //If value is an integer, it will be divided by 10000.
- //If it is a decimal, it will be translated directly.
- int SetLowFloat(int variable, int value){
- int n = variable << 0;
- if ( value >= 0 )
- value = (value << 0)/10000;
- return n+value;
- }
- //Accepts a variable and a value and returns the variable with its integer
- //portion replaced with 'value'.
- //If value is an integer, it will be truncated automatically.
- //If 'value' is a decimal, it will be multipied by 10000.
- int SetHighFloat(int variable, int value){
- int n = variable - ( variable << 0 );
- if ( value < 0 ) value = value*10000;
- else value = value << 0;
- return n+value;
- }
- //Takes a float as input 'n', and returns the integer portion as int.
- int GetHighFloat(int n) {
- return n >> 0;
- }
- //Takes a float as input 'n', and returns the decimal portion as int.
- int GetLowFloat(int n) {
- return (n - (n >> 0)) * 10000;
- }
- //Converts floating point value 'v', after the decimal, to an integer.
- int DecimalToInt(int v) {
- int r = (v - (v << 0)) * 10000;
- return r;
- }
- //Truncates decimal off floating point value 'v'.
- int Truncate(int v) {
- return ( v << 0 );
- }
- //Returns the number of valid integer places in a value.
- int NumPlacesInt(int var){
- int q; int num; int numzero; int n;
- for ( q = 0; q <= 4; q++ ) {
- n = GetDigit(var, q);
- if ( n == 0 ) numzero++;
- if ( n != 0 ) {
- num++;
- num+=numzero;
- numzero=0;
- }
- }
- return num;
- }
- //Returns the number of valid decimal places in a value.
- int NumPlacesDec(int var){
- int q; int num; int numzero; int n;
- for ( q = -1; q >= -4; q-- ) {
- n = GetDigit(var, q);
- if ( n == 0 ) numzero++;
- if ( n != 0 ) {
- num++;
- num+=numzero;
- numzero=0;
- }
- }
- return num;
- }
- int GetDigit(int n, int place){ return GetDigitValue(n,place);}
- //Stores a single-sigit value into a specified place in a variable.
- //n is the variable input, place is the place, from a range of 4 to -4
- //Storing a value that is larger than can fit in the specified number of places will return an error and -1.
- int SetDigit(int v, int place, int value){
- int err[]="Error: The value specified to SetDigit() may only be one place wide.";
- value = value>>0;
- if ( NumPlacesInt(value) > 1 ) {
- TraceS(err);
- return -1;
- }
- place = Clamp(place, -4, 4);
- int n; int q;
- for ( q = -4; q <= 4; q++ ) {
- if ( q != place ) {
- n += GetDigit(v, q); //Store the individual values, except what we want to replace.
- }
- if ( q == place ) {
- n += (value * Pow(10,place)); //Store what we want to replace.
- }
- }
- return n;
- }
- int SetDigitValue(int v, int place, int value){ return SetDigit(v, place, value); }
- //Stores a value into a specified number of places in a variable.
- //n is the variable input, place is the initial place, from a range of 4 to -4
- //num is the number of places, and 'value' is the value to store in those places.
- //The maximum value is 214747' and decimals will be truncated.
- //Passing 99999.9999 will be truncated to 99999.
- //Storing a value that is larger than can fit in the specified number of places will return an error and -1.
- int SetPartialValue(int n, int place, int num, int value){
- place = Clamp(place, -4, 4);
- int r; int r2 = value << 0;
- int err[]="Error: The value specified to SetPartialValue() is wider than the specified value of 'num_places'.";
- //int err2[]="Error: The maximum input value for SetPartialValue(n, place,
- if ( NumPlacesInt(r2) > num ) {
- TraceS(err);
- return -1;
- }
- int err2[]="Error: The value specified to SetPartialValue() is narrower than the specified value of 'num_places'.";
- //int err2[]="Error: The maximum input value for SetPartialValue(n, place,
- if ( NumPlacesInt(r2) > num ) {
- TraceS(err2);
- num = NumPlacesInt(r2);
- }
- int err3[]="Error: You specified a floating point input 'value' to SetPartialValue() that was truncated.";
- if ( NumPlacesDec(value) > 0 ) {
- TraceS(err3);
- }
- int p = 0; int q; int minplace = place-num;
- minplace = Clamp(minplace, -4, 4);
- int maxplace; int temp;
- //populate r with the values that we want
- for ( q = place; q >= minplace; q-- ) {
- temp = GetDigit(r2, p);
- SetDigit(r, q, temp);
- p++;
- }
- //then setDigit the rest of its places using n
- for ( q = minplace; q >= -4; q++ ) {
- SetDigit(r, q, n);
- }
- for ( q = place+1; q <= 4; q++ ) {
- SetDigit(r, q, n);
- }
- return r;
- }
- //Extracts a single digit from n at the place specified.
- //-4 is the ten-thousandTHs place, 0 is the ones spot, and 4 is the ten-thousanDs spot.
- int GetDigitValue(int n, int place){
- place = Clamp(place, -4, 4);
- if( place < 0 ){
- n = DecimalToInt(n);
- place += 4;
- }
- int r = ((n / Pow(10, place)) % 10) << 0;
- return r;
- }
- //Extracts an integer using specific places of any value 'n', from position 'place' plus a number of places 'num'.
- int GetPartialValue(int n, int place, int num){
- place = Clamp(place, -4, 4);
- int r;
- int adj = 1;
- for(int i = num-1; i > -1; i--){
- if(place - i < -4) continue;
- r += GetDigitValue(n, place - i) * adj;
- adj *= 10;
- }
- return r;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement