View Single Post
Author Message
addons_zz
Veteran Member
Join Date: Aug 2015
Location: Dreams, zz
Old 12-27-2016 , 04:06   General Numeric Base Conversion - Any base to any base
Reply With Quote #1

General Numeric Base Conversion - Any base to any base

References:
  1. Wikipedia - Radix
  2. Wikipedia - Positional notation
  3. Wikipedia - Positional notation
  4. List of numeral systems
  5. Phanderson - Base Conversion
  6. StackExchange Computer Science - The math behind converting from any base to any base without going through base 10?

The stock:
Code:
/**  * Given a number on a certain base until 10, calculates and return the equivalent number on another  * base until 10.  *  * @param origin_number    the number to be converted.  * @param origin_base      the base where `origin_number` is on.  * @param destiny_base     the base where `origin_number` is to be converted to.  */ stock convert_numeric_base( origin_number, origin_base, destiny_base ) {     new integer;     new Array:digits;     digits  = toDigitsRepresentation( origin_number, 10 );     integer = fromDigitsRepresentation( digits, origin_base );     ArrayDestroy( digits );     digits  = toDigitsRepresentation( integer, destiny_base );     integer = fromDigitsRepresentation( digits, 10 );     ArrayDestroy( digits );     return integer; } /**  * Read an digits Dynamic Array at its given base and return an integer representation.  *  * @param digits           a Dynamic Array within the digits of inputed number on the specified base.  * @param origin_base      the base where `digits` are represented.  *  * return an integer representation inputed number on the specified base.  */ stock fromDigitsRepresentation( Array:digits, origin_base ) {     new integer;     new arraySize = ArraySize( digits );     for( new index = 0; index < arraySize; index ++ )     {         integer = integer * origin_base + ArrayGetCell( digits, index );     }     return integer; } /**  * Create an Dynamic Array within of the decimal number at its given base.  *  * @param origin_number    a decimal number to be converted.  * @param origin_base      the base where `origin_number` is to be converted to.  *  * return a Dynamic Array within the digits of inputed number on the specified base.  */ stock Array:toDigitsRepresentation( origin_number, origin_base ) {     new Array:digits = ArrayCreate();     ArrayPushCell( digits, origin_number % origin_base );     origin_number = origin_number / origin_base;     while( origin_number > 0 )     {         ArrayInsertCellBefore( digits, 0, origin_number % origin_base );         origin_number = origin_number / origin_base;     }     return digits; }

The Unit Tests Results:
HTML Code:
L 12/29/2016 - 16:59:05: {1.000 16292 -1944939 -1944939} I AM ENTERING ON configureTheUnitTests(0)
L 12/29/2016 - 16:59:05: {1.000 16296 -1944936    3}
L 12/29/2016 - 16:59:05: {1.000 16296 -1944919   17}
L 12/29/2016 - 16:59:05: {1.000 16296 -1944914    5}
L 12/29/2016 - 16:59:05: {1.000 16296 -1944910    4}
L 12/29/2016 - 16:59:05: {1.000 16296 -1944904    6}
L 12/29/2016 - 16:59:05: {1.000 14928 -1944899    5}         EXECUTING TEST 1 AFTER 1 SECONDS - test_convertNumericBase.aa_case1
L 12/29/2016 - 16:59:05: {1.000 15196 -1944889   10} I AM ENTERING ON convert_numeric_base(3) | number: 10 (7->10)
L 12/29/2016 - 16:59:05: {1.000 14692 -1944886    3} Array Cells: (0)     1,       (1)     0,
L 12/29/2016 - 16:59:05: {1.000 14692 -1944882    4} Array Cells: (0)     7,
L 12/29/2016 - 16:59:05: {1.000 15196 -1944878    4}     ( convert_numeric_base ) Returning integer: 7
L 12/29/2016 - 16:59:05: {1.000 14904 -1944872    6} ( displaysLastTestOk ) numberOfFailures: 0, lastFailure: 0, lastTestId: 1
L 12/29/2016 - 16:59:05: {1.000 14920 -1944868    4} OK!
L 12/29/2016 - 16:59:05: {1.000 14920 -1944863    5}
L 12/29/2016 - 16:59:05: {1.000 14920 -1944859    4}
L 12/29/2016 - 16:59:05: {1.000 14928 -1944853    6}         EXECUTING TEST 2 AFTER 1 SECONDS - test_convertNumericBase.aa_case2
L 12/29/2016 - 16:59:05: {1.000 15196 -1944849    4} I AM ENTERING ON convert_numeric_base(3) | number: 10 (6->10)
L 12/29/2016 - 16:59:05: {1.000 14692 -1944845    4} Array Cells: (0)     1,       (1)     0,
L 12/29/2016 - 16:59:05: {1.000 14692 -1944841    4} Array Cells: (0)     6,
L 12/29/2016 - 16:59:05: {1.000 15196 -1944836    5}     ( convert_numeric_base ) Returning integer: 6
L 12/29/2016 - 16:59:05: {1.000 14904 -1944832    4} ( displaysLastTestOk ) numberOfFailures: 0, lastFailure: 0, lastTestId: 2
L 12/29/2016 - 16:59:05: {1.000 14920 -1944828    4} OK!
L 12/29/2016 - 16:59:05: {1.000 14920 -1944822    6}
L 12/29/2016 - 16:59:05: {1.000 14920 -1944816    6}
L 12/29/2016 - 16:59:05: {1.000 14928 -1944812    4}         EXECUTING TEST 3 AFTER 1 SECONDS - test_convertNumericBase.aa_case3
L 12/29/2016 - 16:59:05: {1.000 15196 -1944808    4} I AM ENTERING ON convert_numeric_base(3) | number: 10 (5->10)
L 12/29/2016 - 16:59:05: {1.000 14692 -1944803    5} Array Cells: (0)     1,       (1)     0,
L 12/29/2016 - 16:59:06: {1.000 14692 -1944798    5} Array Cells: (0)     5,
L 12/29/2016 - 16:59:06: {1.000 15196 -1944794    4}     ( convert_numeric_base ) Returning integer: 5
L 12/29/2016 - 16:59:06: {1.000 14904 -1944790    4} ( displaysLastTestOk ) numberOfFailures: 0, lastFailure: 0, lastTestId: 3
L 12/29/2016 - 16:59:06: {1.000 14920 -1944784    6} OK!
L 12/29/2016 - 16:59:06: {1.000 14920 -1944779    5}
L 12/29/2016 - 16:59:06: {1.000 14920 -1944776    3}
L 12/29/2016 - 16:59:06: {1.000 14928 -1944769    7}         EXECUTING TEST 4 AFTER 2 SECONDS - test_convertNumericBase.aa_case4
L 12/29/2016 - 16:59:06: {1.000 15196 -1944765    4} I AM ENTERING ON convert_numeric_base(3) | number: 10 (8->10)
L 12/29/2016 - 16:59:06: {1.000 14692 -1944761    4} Array Cells: (0)     1,       (1)     0,
L 12/29/2016 - 16:59:06: {1.000 14692 -1944758    3} Array Cells: (0)     8,
L 12/29/2016 - 16:59:06: {1.000 15196 -1944751    7}     ( convert_numeric_base ) Returning integer: 8
L 12/29/2016 - 16:59:06: {1.000 14904 -1944748    3} ( displaysLastTestOk ) numberOfFailures: 0, lastFailure: 0, lastTestId: 4
L 12/29/2016 - 16:59:06: {1.000 14920 -1944744    4} OK!
L 12/29/2016 - 16:59:06: {1.000 14920 -1944736    8}
L 12/29/2016 - 16:59:06: {1.000 14920 -1944732    4}
L 12/29/2016 - 16:59:06: {1.000 14928 -1944730    2}         EXECUTING TEST 5 AFTER 2 SECONDS - test_convertNumericBase.aa_case5
L 12/29/2016 - 16:59:06: {1.000 15196 -1944726    4} I AM ENTERING ON convert_numeric_base(3) | number: 10 (9->10)
L 12/29/2016 - 16:59:06: {1.000 14692 -1944719    7} Array Cells: (0)     1,       (1)     0,
L 12/29/2016 - 16:59:06: {1.000 14692 -1944716    3} Array Cells: (0)     9,
L 12/29/2016 - 16:59:06: {1.000 15196 -1944712    4}     ( convert_numeric_base ) Returning integer: 9
L 12/29/2016 - 16:59:06: {1.000 14904 -1944708    4} ( displaysLastTestOk ) numberOfFailures: 0, lastFailure: 0, lastTestId: 5
L 12/29/2016 - 16:59:06: {1.000 14920 -1944702    6} OK!
L 12/29/2016 - 16:59:06: {1.000 14920 -1944698    4}
L 12/29/2016 - 16:59:06: {1.000 14920 -1944694    4}
L 12/29/2016 - 16:59:06: {1.000 14928 -1944689    5}         EXECUTING TEST 6 AFTER 2 SECONDS - test_convertNumericBase.aa_case6
L 12/29/2016 - 16:59:06: {1.000 15196 -1944684    5} I AM ENTERING ON convert_numeric_base(3) | number: 10 (10->10)
L 12/29/2016 - 16:59:06: {1.000 14692 -1944681    3} Array Cells: (0)     1,       (1)     0,
L 12/29/2016 - 16:59:06: {1.000 14692 -1944677    4} Array Cells: (0)     1,       (1)     0,
L 12/29/2016 - 16:59:06: {1.000 15196 -1944671    6}     ( convert_numeric_base ) Returning integer: 10
L 12/29/2016 - 16:59:06: {1.000 14904 -1944667    4} ( displaysLastTestOk ) numberOfFailures: 0, lastFailure: 0, lastTestId: 6
L 12/29/2016 - 16:59:06: {1.000 14920 -1944663    4} OK!
L 12/29/2016 - 16:59:06: {1.000 14920 -1944659    4}
L 12/29/2016 - 16:59:06: {1.000 14920 -1944653    6}
L 12/29/2016 - 16:59:06: {1.000 14928 -1944649    4}         EXECUTING TEST 7 AFTER 2 SECONDS - test_convertNumericBase.aa_case7
L 12/29/2016 - 16:59:06: {1.000 15196 -1944645    4} I AM ENTERING ON convert_numeric_base(3) | number: 11 (7->9)
L 12/29/2016 - 16:59:06: {1.000 14692 -1944641    4} Array Cells: (0)     1,       (1)     1,
L 12/29/2016 - 16:59:06: {1.000 14692 -1944636    5} Array Cells: (0)     8,
L 12/29/2016 - 16:59:06: {1.000 15196 -1944632    4}     ( convert_numeric_base ) Returning integer: 8
L 12/29/2016 - 16:59:06: {1.000 14904 -1944628    4} ( displaysLastTestOk ) numberOfFailures: 0, lastFailure: 0, lastTestId: 7
L 12/29/2016 - 16:59:06: {1.000 14920 -1944622    6} OK!
L 12/29/2016 - 16:59:06: {1.000 14920 -1944618    4}
L 12/29/2016 - 16:59:06: {1.000 14920 -1944614    4}
L 12/29/2016 - 16:59:06: {1.000 14928 -1944610    4}         EXECUTING TEST 8 AFTER 2 SECONDS - test_convertNumericBase.aa_case8
L 12/29/2016 - 16:59:06: {1.000 15196 -1944608    2} I AM ENTERING ON convert_numeric_base(3) | number: 2462 (7->9)
L 12/29/2016 - 16:59:06: {1.000 14668 -1944601    7} Array Cells: (0)     2,       (1)     4,       (2)     6,       (3)     2,
L 12/29/2016 - 16:59:06: {1.000 14668 -1944597    4} Array Cells: (0)     1,       (1)     2,       (2)     3,       (3)     8,
L 12/29/2016 - 16:59:06: {1.000 15196 -1944593    4}     ( convert_numeric_base ) Returning integer: 1238
L 12/29/2016 - 16:59:06: {1.000 14904 -1944586    7} ( displaysLastTestOk ) numberOfFailures: 0, lastFailure: 0, lastTestId: 8
L 12/29/2016 - 16:59:06: {1.000 14920 -1944583    3} OK!
L 12/29/2016 - 16:59:06: {1.000 14920 -1944579    4}
L 12/29/2016 - 16:59:06: {1.000 14920 -1944575    4}
L 12/29/2016 - 16:59:06: {1.000 14928 -1944569    6}         EXECUTING TEST 9 AFTER 2 SECONDS - test_convertNumericBase.aa_case9
L 12/29/2016 - 16:59:06: {1.000 15196 -1944565    4} I AM ENTERING ON convert_numeric_base(3) | number: 1238 (9->7)
L 12/29/2016 - 16:59:06: {1.000 14668 -1944561    4} Array Cells: (0)     1,       (1)     2,       (2)     3,       (3)     8,
L 12/29/2016 - 16:59:06: {1.000 14668 -1944556    5} Array Cells: (0)     2,       (1)     4,       (2)     6,       (3)     2,
L 12/29/2016 - 16:59:06: {1.000 15196 -1944551    5}     ( convert_numeric_base ) Returning integer: 2462
L 12/29/2016 - 16:59:06: {1.000 16256 -1944548    3} ( displaysLastTestOk ) numberOfFailures: 0, lastFailure: 0, lastTestId: 9
L 12/29/2016 - 16:59:06: {1.000 16272 -1944544    4} OK!
L 12/29/2016 - 16:59:06: {1.000 16272 -1944538    6}
L 12/29/2016 - 16:59:06: {1.000 16272 -1944534    4}
L 12/29/2016 - 16:59:06: {1.000 16296 -1944530    4}
L 12/29/2016 - 16:59:06: {1.000 16280 -1944526    4} I AM ENTERING ON print_all_tests_executed(0)
L 12/29/2016 - 16:59:06: {1.000 16024 -1944520    6}
L 12/29/2016 - 16:59:06: {1.000 16024 -1944517    3}
L 12/29/2016 - 16:59:06: {1.000 16024 -1944512    5}
L 12/29/2016 - 16:59:06: {1.000 16024 -1944509    3}     The following tests were executed:
L 12/29/2016 - 16:59:06: {1.000 16024 -1944503    6}
L 12/29/2016 - 16:59:06: {1.000 16008 -1944498    5}          1. test_convertNumericBase.aa_case1
L 12/29/2016 - 16:59:06: {1.000 16008 -1944494    4}          2. test_convertNumericBase.aa_case2
L 12/29/2016 - 16:59:06: {1.000 16008 -1944489    5}          3. test_convertNumericBase.aa_case3
L 12/29/2016 - 16:59:06: {1.000 16008 -1944485    4}          4. test_convertNumericBase.aa_case4
L 12/29/2016 - 16:59:06: {1.000 16008 -1944484    1}          5. test_convertNumericBase.aa_case5
L 12/29/2016 - 16:59:06: {1.000 16008 -1944476    8}          6. test_convertNumericBase.aa_case6
L 12/29/2016 - 16:59:06: {1.000 16008 -1944470    6}          7. test_convertNumericBase.aa_case7
L 12/29/2016 - 16:59:06: {1.000 16008 -1944466    4}          8. test_convertNumericBase.aa_case8
L 12/29/2016 - 16:59:06: {1.000 16008 -1944462    4}          9. test_convertNumericBase.aa_case9
L 12/29/2016 - 16:59:06: {1.000 14996 -1944458    4}
L 12/29/2016 - 16:59:06: {1.000 14988 -1944453    5}     9 tests succeed.
L 12/29/2016 - 16:59:06: {1.000 14992 -1944449    4}     0 tests failed.
L 12/29/2016 - 16:59:06: {1.000 16284 -1944445    4}
L 12/29/2016 - 16:59:06: {1.000 16272 -1944439    6}     Finished the General Numeric Base Conversion's Unit Tests execution after '2' seconds.
L 12/29/2016 - 16:59:06: {1.000 16284 -1944435    4}
L 12/29/2016 - 16:59:06: {1.000 16284 -1944431    4}

The Unit Tests Source Code:
Spoiler
Attached Files
File Type: sma Get Plugin or Get Source (full_unit_tests.sma - 484 views - 21.0 KB)
__________________
Plugin: Sublime Text - ITE , Galileo
Multi-Mod: Manager / Plugin / Server

Support me on Patreon, Ko-fi, Liberapay or Open Collective

Last edited by addons_zz; 12-29-2016 at 21:13. Reason: Updated the Unit Tests code
addons_zz is offline