#include <stdio.h>
#include <math.h>
#include <float.h>
#define STEP 0.3
#define STEPS 31
#define FLT_PATTERN "%15.14f"
void print_bin( unsigned long long v )
{
int i;
for ( i = 0; i < 64; i++ ) {
if ( i == 1 || i == 12 || i == 15 ) printf( " " );
printf( "%d", (v >> (63 - i)) & 1 );
}
}
int is_int_A( double t )
{
return (t - floor( t )) == 0;
}
int is_int_B( double t )
{
return (t - (int)t) == 0;
}
int is_int_C( double t )
{
return ceil( t ) == floor( t );
}
int is_int_D( double a )
{
int exp;
unsigned long long mantissa, frac, v = *((unsigned long long *)&a);
exp = ((v >> 52) & 0x7ff) - 1023;
mantissa = (v & 0xfffffffffffff) | 0x10000000000000;
frac = mantissa & (((unsigned long long)1 << (52 - exp)) - 1);
return frac == 0;
}
int is_int_E( double a )
{
int exp;
unsigned long long mantissa, v = *((unsigned long long *)&a);
long long frac;
exp = ((v >> 52) & 0x7ff) - 1023;
mantissa = (v & 0xfffffffffffff) | 0x10000000000000;
frac = mantissa & (((unsigned long long)1 << (52 - exp)) - 1);
frac >>= 20;
return frac <= 0;
}
int is_int_F( double a )
{
double a_frac, a_int;
a_frac = modf( a, &a_int );
return a_frac == 0;
}
int main()
{
// sizeof( double ) == 8
int i;
double t;
printf( "t - floor( t ):\n" );
for ( i = 0; i < STEPS; i++ ) {
t = STEP * i;
printf( FLT_PATTERN" %s celoe\n", t, is_int_A( t ) ? " " : " ne" );
}
printf( "\nt - int( t ):\n" );
for ( i = 0; i < STEPS; i++ ) {
t = STEP * i;
printf( FLT_PATTERN" %s celoe\n", t, is_int_B( t ) ? " " : " ne" );
}
printf( "\nceil( t ) == floor( t ):\n" );
for ( i = 0; i < STEPS; i++ ) {
t = STEP * i;
printf( FLT_PATTERN" %s celoe\n", t, is_int_C( t ) ? " " : " ne" );
}
printf( "\nmodf:\n" );
for ( i = 0; i < STEPS; i++ ) {
t = STEP * i;
printf( FLT_PATTERN" %s celoe\n", t, is_int_F( t ) ? " " : " ne" );
}
printf( "\nfractional extract:\n" );
for ( i = 0; i < STEPS; i++ ) {
t = STEP * i;
printf( FLT_PATTERN" %s celoe\n", t, is_int_D( t ) ? " " : " ne" );
}
printf( "\nfractional extract (hack):\n" );
for ( i = 0; i < STEPS; i++ ) {
t = STEP * i;
printf( FLT_PATTERN" %s celoe ", t, is_int_E( t ) ? " " : "ne" );
print_bin( *((unsigned long long *)&t) );
printf( "\n" );
}
return 0;
}