Skip to content

Instantly share code, notes, and snippets.

@astkaasa
Created April 16, 2013 02:03
Show Gist options
  • Save astkaasa/5392785 to your computer and use it in GitHub Desktop.
Save astkaasa/5392785 to your computer and use it in GitHub Desktop.
problem 5.2
public class TestFloat {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
//System.out.println( "Please input a real number between 0 and 1, 0.72 for example:" );
double v = 1.0 / 3.0;
int[] a = new int[32];
if ( testFloat( v, a ) ) {
System.out.print( "0." );
for ( int i = 0; i < 32; i++ )
System.out.print( a[ i ] );
System.out.println();
}
else System.out.println( "ERROR" );
System.out.print( "0." );
for ( int i = 0; i < 32; i++ )
System.out.print( a[ i ] );
System.out.println();
}
public static boolean testFloat( double v, int[] a ) {
for ( int i = 0; i < 32; i++ ) {
v = v * 2;
a[ i ] = ( int ) v;
v = v - a[ i ];
}
if ( v == 0 )
return true;
else return false;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment