Java  1.0
Static Public Member Functions | Static Private Member Functions | Static Private Attributes | List of all members
baseConverter Class Reference

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...
 

Detailed Description

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:

Author
Paulo Roma
Since
29/12/2012
See also
IEEE 754 single precision
IEEE 754 double precision

Definition at line 64 of file baseConverter.java.

Member Function Documentation

◆ atof()

static double baseConverter.atof ( String  num,
int  base 
)
static

Converts a String, representing a number in a given base, to a float point decimal.

Parameters
numa string representing a number in a given base.
basenumerical base.
Returns
a decimal representation of the given number.

Definition at line 560 of file baseConverter.java.

References atoi(), and decimalPoint.

Referenced by IEEE754tof(), and main().

◆ atoi()

static int baseConverter.atoi ( String  num,
int  base 
)
static

Converts a String, representing a number in a given base, to decimal.

Parameters
numa string representing a number in a given base.
basenumerical base.
Returns
a decimal representation of the given number.

Definition at line 528 of file baseConverter.java.

Referenced by atof(), ftoIEEE754(), IEEE754tof(), and main().

◆ fftoa()

static String baseConverter.fftoa ( double  num,
int  base 
)
static

Similar to function itoa, but accepts a double as argument for being able do deal with huge numbers.

Parameters
numValue to be converted to a string.
baseNumerical 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.
Returns
string representing num in the given base.

Definition at line 329 of file baseConverter.java.

References modf().

Referenced by ftoa().

◆ frac()

static double baseConverter.frac ( double  d)
staticprivate

Returns the fractional part of a double.

Parameters
da number.
Returns
fractional part of d.

Definition at line 607 of file baseConverter.java.

References inte().

Referenced by main(), and modf().

◆ ftoa()

static String baseConverter.ftoa ( double  num,
int  base 
)
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.

Parameters
numvalue to be converted to a string.
basenumerical 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.
Returns
a string representing the number in the given base.

Definition at line 416 of file baseConverter.java.

References decimalPoint, EXPO_MAG, fftoa(), FRAC_LEN, modf(), and normalize().

Referenced by ftoIEEE754(), and main().

◆ ftoIEEE754()

static String baseConverter.ftoIEEE754 ( double  num)
static

Converts a float value to a string representing the number in the IEEE 754 standard:

  • 32 bits: 1 bit for the signal, plus 8 bits for the expoent and 23 bits for the mantissa.
  • 64 bits: 1 bit for the signal, plus 11 bits for the expoent and 52 bits for the mantissa.
Parameters
numvalue to be converted to the IEEE 754 float point standard.
Returns
a string representing the number in the IEEE 754 float point format.

Definition at line 464 of file baseConverter.java.

References atoi(), decimalPoint, EXPO_LEN, EXPO_MAG, FRAC_LEN, ftoa(), itoa(), and zero().

Referenced by main().

◆ getLongSize()

static int baseConverter.getLongSize ( )
static

Gets the size of a long.

Returns
the size of a long integer.

Definition at line 277 of file baseConverter.java.

Referenced by main().

◆ getMagnitude()

static int baseConverter.getMagnitude ( long  n)
static

Gets the number of decimal digits of a long.

Parameters
ngiven long.
Returns
the number of decimal digits of n.

Definition at line 248 of file baseConverter.java.

Referenced by main().

◆ getNumberOfBits()

static int baseConverter.getNumberOfBits ( long  n)
static

Gets the number of binary digits of a long.

Parameters
ngiven long.
Returns
the number of binary digits of n.

Definition at line 263 of file baseConverter.java.

Referenced by main().

◆ IEEE754tof()

static double baseConverter.IEEE754tof ( String  num)
static

Converts a string representing a number in the IEEE 754 standard to float:

  • 32 bits: 1 bit for the signal, plus 8 bits for the expoent and 23 bits for the mantissa.
  • 64 bits: 1 bit for the signal, plus 11 bits for the expoent and 52 bits for the mantissa.
Parameters
numIEEE 754 string representing a float point number.
Returns
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().

◆ inte()

static double baseConverter.inte ( double  d)
staticprivate

Returns the integer part of a double.

Parameters
da number.
Returns
integer part of d.

Definition at line 617 of file baseConverter.java.

Referenced by frac(), and main().

◆ ipow()

static long baseConverter.ipow ( long  x,
long  p 
)
static

Efficiently calculate integer positive powers by repeated squaring.

Parameters
xgiven long.
pgiven positive power.
Returns
\(x^{p}\).

Definition at line 210 of file baseConverter.java.

Referenced by pow().

◆ itoa()

static String baseConverter.itoa ( int  num,
int  base 
)
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.

Parameters
numvalue to be converted to a string.
basenumerical 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.
Returns
a string representing the number in the given base.

Definition at line 299 of file baseConverter.java.

Referenced by ftoIEEE754(), roman.int2roman(), main(), normalize(), and palindromes.printPalindromeNumbers().

◆ main()

static void baseConverter.main ( String[]  args)
static

main function for testing.

Parameters
argsan 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().

◆ modf()

static double [] baseConverter.modf ( double  d)
staticprivate

Extracts the integer and fractional parts of a double.

Parameters
da number.
Returns
a double array:
  • position 0 is the integer part and
  • position 1 the fractional part of the given number.

Definition at line 629 of file baseConverter.java.

References decimalPoint, frac(), and zero().

Referenced by fftoa(), ftoa(), and main().

◆ normalize()

static String baseConverter.normalize ( String  str)
static

Converts a string representing a float point number to a scientifc notation of the form: x.xxxxExx.

Parameters
strfloat point number.
Returns
converted string.

Definition at line 372 of file baseConverter.java.

References decimalPoint, EXPO_MAG, itoa(), and trimTrailingZeros().

Referenced by ftoa().

◆ pow()

static double baseConverter.pow ( long  x,
long  p 
)
static

Efficiently calculate integer powers by repeated squaring.

Parameters
xgiven long.
pgiven power.
Returns
\(x^{p}\).

Definition at line 198 of file baseConverter.java.

References ipow().

Referenced by main().

◆ printAsciiTable()

static void baseConverter.printAsciiTable ( )
static

Prints the ASCII table from ' ' (32) to '~' (126).

Definition at line 166 of file baseConverter.java.

Referenced by main().

◆ printBits()

static void baseConverter.printBits ( long  n)
static

Prints the binary representation of a long.

Parameters
ngiven long.
See also
http://mindprod.com/jgloss/shift.html

Definition at line 229 of file baseConverter.java.

Referenced by main().

◆ Product()

static int baseConverter.Product ( int  a,
int  b 
)
static

Multiplies two integers by using only the + operation.

Parameters
afist factor.
bsecond factor.

Definition at line 151 of file baseConverter.java.

Referenced by main().

◆ reverseBits()

static long baseConverter.reverseBits ( long  x)
static

Reverses the bits of a long.

Parameters
xgiven long.
Returns
another long with the reversed bits of x.

Definition at line 180 of file baseConverter.java.

Referenced by main(), and rsa.power_mod_n().

◆ trimTrailingZeros()

static String baseConverter.trimTrailingZeros ( String  str)
static

Trim trailing zeros at the end of a string.

Parameters
strfloat point number.
Returns
cleaned string.

Definition at line 355 of file baseConverter.java.

References decimalPoint.

Referenced by normalize().

◆ zero()

static String baseConverter.zero ( int  len)
static

Returns a string, of a given size, filled with zeroes.

Parameters
lensize of the returned string,
Returns
zero string.

Definition at line 450 of file baseConverter.java.

Referenced by ftoIEEE754(), IEEE754tof(), and modf().

Member Data Documentation

◆ decimalPoint

final char baseConverter.decimalPoint = DecimalFormatSymbols.getInstance().getDecimalSeparator()
staticprivate

Decimal point separator.

Definition at line 66 of file baseConverter.java.

Referenced by atof(), ftoa(), ftoIEEE754(), IEEE754tof(), modf(), normalize(), and trimTrailingZeros().

◆ EXPO_LEN

final int baseConverter.EXPO_LEN = 8
staticprivate

Number of bits for the IEEE 754 expoent.

Definition at line 72 of file baseConverter.java.

Referenced by ftoIEEE754(), and IEEE754tof().

◆ EXPO_MAG

final int baseConverter.EXPO_MAG = 127
staticprivate

Magnitude for the IEEE 754 expoent.

Definition at line 78 of file baseConverter.java.

Referenced by ftoa(), ftoIEEE754(), IEEE754tof(), and normalize().

◆ FRAC_LEN

final int baseConverter.FRAC_LEN = 23
staticprivate

Number of bits for the IEEE 754 fraction.

Definition at line 75 of file baseConverter.java.

Referenced by ftoa(), ftoIEEE754(), and IEEE754tof().

◆ WORD_LEN

final int baseConverter.WORD_LEN = Integer.SIZE
staticprivate

Number of bits for the IEEE 754 word.

Definition at line 69 of file baseConverter.java.


The documentation for this class was generated from the following file: