import { ERRORTYPES as error } from "./error";
Copyright 2015-2021 JC Fisher
import { ERRORTYPES as error } from "./error";
OCT2DEC converts an octal value into a decimal value.
export default function oct2dec(octalNumber) {
Credits: Based on implementation found in https://gist.github.com/ghalimi/4525876#file-oct2dec-js Return error.when number passed in is not octal or has more than 10 digits
if (!/^[0-7]{1,10}$/.test(octalNumber)) return error.num;
Convert octal number to decimal number
var nonNegativeDecimalNumber = parseInt(octalNumber, 8);
Returns the corresponding decimal number Two’s Complement Decimal Range: -(2^N-1) to (2^N-1 - 1) where N=30 (N = number of bits) and ^ means raised to the power of 2^N-1 = 2^(30 - 1) = 2^29 = 536870912 2^N-1 - 1 = 536870912 - 1 = 536870911 2^N = 2^30 = 1073741824 Two’s Complement Decimal Range: [-536870912,536870911] Largest octal number allowed: 7777777777 which in decimal is 1073741823 = 2^N - 1 Case 1: Negative Range if nonNegativeDecimalNumber >= 2^N-1, then return (nonNegativeNumber - 2^N) Smallest Number: 2^N-1 - 2^N = 2^N-1 - 22^N-1 = 2^N-1 (1 - 2) = 2^N-1 * (-1) = -2^N-1 Largest Number: (2^N - 1) - (2^N) = (2^N - 2^N) - 1 = -1 Range: [-2^N-1, -1] = [-536870912, -1]
Smallest octal number allowed: 0 which in decimal is 0 Case 2: Non-Negative Range Range: [0, 2^N-1 - 1] = [0, 536870911]
return nonNegativeDecimalNumber >= 536870912
? nonNegativeDecimalNumber - 1073741824
: nonNegativeDecimalNumber;
}