![]() |
Java
1.0
|
Class for converting between different numerical bases. More...
Static Public Member Functions | |
static void | main (String[] args) |
main function for testing. More... | |
static int | Product (int a, int b) |
Multiplies two integers by using only the + operation. More... | |
static void | printAsciiTable () |
Prints the ASCII table from ' ' (32) to '~' (126). More... | |
static long | reverseBits (long x) |
Reverses the bits of a long. More... | |
static double | pow (long x, long p) |
Efficiently calculate integer powers by repeated squaring. More... | |
static long | ipow (long x, long p) |
Efficiently calculate integer positive powers by repeated squaring. More... | |
static void | printBits (long n) |
Prints the binary representation of a long. More... | |
static int | getMagnitude (long n) |
Gets the number of decimal digits of a long. More... | |
static int | getNumberOfBits (long n) |
Gets the number of binary digits of a long. More... | |
static int | getLongSize () |
Gets the size of a long. More... | |
static String | itoa (int num, int base) |
Converts an integer value to a string using the specified base and returns it. More... | |
static String | fftoa (double num, int base) |
Similar to function itoa, but accepts a double as argument for being able do deal with huge numbers. More... | |
static String | trimTrailingZeros (String str) |
Trim trailing zeros at the end of a string. More... | |
static String | normalize (String str) |
Converts a string representing a float point number to a scientifc notation of the form: x.xxxxExx. More... | |
static String | ftoa (double num, int base) |
Converts a float value to a string using the specified base and returns it. More... | |
static String | zero (int len) |
Returns a string, of a given size, filled with zeroes. More... | |
static String | ftoIEEE754 (double num) |
Converts a float value to a string representing the number in the IEEE 754 standard: More... | |
static double | IEEE754tof (String num) |
Converts a string representing a number in the IEEE 754 standard to float: More... | |
static int | atoi (String num, int base) |
Converts a String, representing a number in a given base, to decimal. More... | |
static double | atof (String num, int base) |
Converts a String, representing a number in a given base, to a float point decimal. More... | |
Static Private Member Functions | |
static double | frac (double d) |
Returns the fractional part of a double. More... | |
static double | inte (double d) |
Returns the integer part of a double. More... | |
static double[] | modf (double d) |
Extracts the integer and fractional parts of a double. More... | |
Static Private Attributes | |
static final char | decimalPoint = DecimalFormatSymbols.getInstance().getDecimalSeparator() |
Decimal point separator. More... | |
static final int | WORD_LEN = Integer.SIZE |
Number of bits for the IEEE 754 word. More... | |
static final int | EXPO_LEN = 8 |
Number of bits for the IEEE 754 expoent. More... | |
static final int | FRAC_LEN = 23 |
Number of bits for the IEEE 754 fraction. More... | |
static final int | EXPO_MAG = 127 |
Magnitude for the IEEE 754 expoent. More... | |
Class for converting between different numerical bases.
In a positional number system, the value of each digit is determined by which place it appears in the full number. The lowest place value is the rightmost position, and each successive position to the left has a higher place value.
For example, the value of the binary number 10011 is determined by computing the place value of each of the digits of the number:
1 0 0 1 1 the binary number 2⁴ 2³ 2² 2¹ 2⁰ place values (1 * 2⁴) + (0 * 2³) + (0 * 2²) + (1 * 2¹) + (1 * 2⁰) = 16 + 0 + 0 + 2 + 1 = 19
-1 0 0 1. 0 0 1 -(1 * 2³) + (0 * 2²) + (0 * 2¹) + (1 * 2⁰) + (0 * 2⁻¹) + (0 * 2⁻²) + (1 * 2⁻³) = -8 + 0 + 0 + 1 + 0 + 0 + 0.125 = -9.125
IEEE 754 observations:
Zero.
Zero is not directly representable in the straight format, due to the assumption of a leading 1 (we'd need to specify a true zero mantissa to yield a value of zero). Zero is a special value denoted with an exponent field of zero and a fraction field of zero. Note that -0 and +0 are distinct values, though they both compare as equal.
Denormalized
If the exponent is all 0s, but the fraction is non-zero (else it would be interpreted as zero), then the value is a denormalized number, which does not have an assumed leading 1 before the binary point. Thus, this represents a number (-1)s × 0.f × 2⁻¹²⁶, where s is the sign bit and f is the fraction. For double precision, denormalized numbers are of the form (-1)s × 0.f × 2⁻¹⁰²². From this you can interpret zero as a special type of denormalized number.
1.00000000000000000000001E-127 = 5.87747245476067E-39
should be written as
0.00000000000000000000001E-126 = 1.0E-149 = 1.40129846432482E-45
To compile:
To run:
Definition at line 64 of file baseConverter.java.
|
static |
Converts a String, representing a number in a given base, to a float point decimal.
num | a string representing a number in a given base. |
base | numerical base. |
Definition at line 560 of file baseConverter.java.
References atoi(), and decimalPoint.
Referenced by IEEE754tof(), and main().
|
static |
Converts a String, representing a number in a given base, to decimal.
num | a string representing a number in a given base. |
base | numerical base. |
Definition at line 528 of file baseConverter.java.
Referenced by atof(), ftoIEEE754(), IEEE754tof(), and main().
|
static |
Similar to function itoa, but accepts a double as argument for being able do deal with huge numbers.
num | Value to be converted to a string. |
base | Numerical base used to represent the value as a string, between 2 and 36, where 10 means decimal base, 16 hexadecimal, 8 octal, and 2 binary. |
Definition at line 329 of file baseConverter.java.
References modf().
Referenced by ftoa().
|
staticprivate |
Returns the fractional part of a double.
d | a number. |
Definition at line 607 of file baseConverter.java.
References inte().
|
static |
Converts a float value to a string using the specified base and returns it.
If base is 10 and value is negative, the resulting string is preceded with a minus sign (-). With any other base, value is always considered unsigned.
num | value to be converted to a string. |
base | numerical base used to represent the value as a string, between 2 and 36, where 10 means decimal base, 16 hexadecimal, 8 octal, and 2 binary. |
Definition at line 416 of file baseConverter.java.
References decimalPoint, EXPO_MAG, fftoa(), FRAC_LEN, modf(), and normalize().
Referenced by ftoIEEE754(), and main().
|
static |
Converts a float value to a string representing the number in the IEEE 754 standard:
num | value to be converted to the IEEE 754 float point standard. |
Definition at line 464 of file baseConverter.java.
References atoi(), decimalPoint, EXPO_LEN, EXPO_MAG, FRAC_LEN, ftoa(), itoa(), and zero().
Referenced by main().
|
static |
Gets the size of a long.
Definition at line 277 of file baseConverter.java.
Referenced by main().
|
static |
Gets the number of decimal digits of a long.
n | given long. |
Definition at line 248 of file baseConverter.java.
Referenced by main().
|
static |
Gets the number of binary digits of a long.
n | given long. |
Definition at line 263 of file baseConverter.java.
Referenced by main().
|
static |
Converts a string representing a number in the IEEE 754 standard to float:
num | IEEE 754 string representing a float point number. |
Definition at line 507 of file baseConverter.java.
References atof(), atoi(), decimalPoint, EXPO_LEN, EXPO_MAG, FRAC_LEN, and zero().
Referenced by main().
|
staticprivate |
Returns the integer part of a double.
d | a number. |
Definition at line 617 of file baseConverter.java.
|
static |
Efficiently calculate integer positive powers by repeated squaring.
x | given long. |
p | given positive power. |
Definition at line 210 of file baseConverter.java.
Referenced by pow().
|
static |
Converts an integer value to a string using the specified base and returns it.
If base is 10 and value is negative, the resulting string is preceded with a minus sign (-). With any other base, value is always considered unsigned.
num | value to be converted to a string. |
base | numerical base used to represent the value as a string, between 2 and 36, where 10 means decimal base, 16 hexadecimal, 8 octal, and 2 binary. |
Definition at line 299 of file baseConverter.java.
Referenced by ftoIEEE754(), roman.int2roman(), main(), normalize(), and palindromes.printPalindromeNumbers().
|
static |
main function for testing.
args | an integer, a float and a numerical base. |
Definition at line 98 of file baseConverter.java.
References atof(), atoi(), frac(), ftoa(), ftoIEEE754(), getLongSize(), getMagnitude(), getNumberOfBits(), IEEE754tof(), inte(), itoa(), modf(), pow(), printAsciiTable(), printBits(), Product(), and reverseBits().
|
staticprivate |
Extracts the integer and fractional parts of a double.
d | a number. |
Definition at line 629 of file baseConverter.java.
References decimalPoint, frac(), and zero().
|
static |
Converts a string representing a float point number to a scientifc notation of the form: x.xxxxExx.
str | float point number. |
Definition at line 372 of file baseConverter.java.
References decimalPoint, EXPO_MAG, itoa(), and trimTrailingZeros().
Referenced by ftoa().
|
static |
Efficiently calculate integer powers by repeated squaring.
x | given long. |
p | given power. |
Definition at line 198 of file baseConverter.java.
References ipow().
Referenced by main().
|
static |
Prints the ASCII table from ' ' (32) to '~' (126).
Definition at line 166 of file baseConverter.java.
Referenced by main().
|
static |
Prints the binary representation of a long.
n | given long. |
Definition at line 229 of file baseConverter.java.
Referenced by main().
|
static |
Multiplies two integers by using only the + operation.
a | fist factor. |
b | second factor. |
Definition at line 151 of file baseConverter.java.
Referenced by main().
|
static |
Reverses the bits of a long.
x | given long. |
Definition at line 180 of file baseConverter.java.
Referenced by main(), and rsa.power_mod_n().
|
static |
Trim trailing zeros at the end of a string.
str | float point number. |
Definition at line 355 of file baseConverter.java.
References decimalPoint.
Referenced by normalize().
|
static |
Returns a string, of a given size, filled with zeroes.
len | size of the returned string, |
Definition at line 450 of file baseConverter.java.
Referenced by ftoIEEE754(), IEEE754tof(), and modf().
|
staticprivate |
Decimal point separator.
Definition at line 66 of file baseConverter.java.
Referenced by atof(), ftoa(), ftoIEEE754(), IEEE754tof(), modf(), normalize(), and trimTrailingZeros().
|
staticprivate |
Number of bits for the IEEE 754 expoent.
Definition at line 72 of file baseConverter.java.
Referenced by ftoIEEE754(), and IEEE754tof().
|
staticprivate |
Magnitude for the IEEE 754 expoent.
Definition at line 78 of file baseConverter.java.
Referenced by ftoa(), ftoIEEE754(), IEEE754tof(), and normalize().
|
staticprivate |
Number of bits for the IEEE 754 fraction.
Definition at line 75 of file baseConverter.java.
Referenced by ftoa(), ftoIEEE754(), and IEEE754tof().
|
staticprivate |
Number of bits for the IEEE 754 word.
Definition at line 69 of file baseConverter.java.