// Dick Hollenbeck's KiROUND R&D

// This provides better project control over rounding to int from double
// than wxRound() did.  This scheme provides better logging in Debug builds
// and it provides for compile time calculation of constants.


#include <stdio.h>
#include <assert.h>
#include <limits.h>

//-----<KiROUND KIT>------------------------------------------------------------

/**
 * KiROUND
 * rounds a floating point number to an int using
 * "round halfway cases away from zero".
 * In Debug build an assert fires if will not fit into an int.
 */

#if defined( DEBUG )

// DEBUG: a macro to capture line and file, then calls this inline

static inline int KiRound( double v, int line, const char* filename )
{
    v = v < 0 ? v - 0.5 : v + 0.5;
    if( v > INT_MAX + 0.5 )
    {
        printf( "%s: in file %s on line %d, val: %.16g too ' > 0 ' for int\n", __FUNCTION__, filename, line, v );
    }
    else if( v < INT_MIN - 0.5 )
    {
        printf( "%s: in file %s on line %d, val: %.16g too ' < 0 ' for int\n", __FUNCTION__, filename, line, v );
    }
    return int( v );
}

#define KiROUND( v )    KiRound( v, __LINE__, __FILE__ )

#else

// RELEASE: a macro so compile can pre-compute constants.

#define KiROUND( v )  int( (v) < 0 ? (v) - 0.5 : (v) + 0.5 )

#endif


//-----</KiROUND KIT>-----------------------------------------------------------

// Only a macro is compile time calculated, an inline function causes a static constructor
// in a situation like this.
// Therefore the Release build is best done with a MACRO not an inline function.
int Computed = KiROUND( 14.3 * 8 );


int main( int argc, char** argv )
{
    for( double d = double(INT_MAX)-1;  d < double(INT_MAX)+8;  d += 2.0 )
    {
        int i = KiROUND( d );

        printf( "t: %d  %.16g\n", i, d );
    }

    return 0;
}
This commit is contained in:
Dick Hollenbeck
2012-04-19 01:55:45 -05:00
parent 487ac04a1b
commit c53c5b47ea
60 changed files with 314 additions and 272 deletions
+8 -8
View File
@@ -112,8 +112,8 @@ void HPGL_PLOTTER::PlotImage( wxImage & aImage, wxPoint aPos, double aScaleFacto
size.x = aImage.GetWidth();
size.y = aImage.GetHeight();
size.x = wxRound( size.x * aScaleFactor );
size.y = wxRound( size.y * aScaleFactor );
size.x = KiROUND( size.x * aScaleFactor );
size.y = KiROUND( size.y * aScaleFactor );
wxPoint start = aPos;
start.x -= size.x / 2;
@@ -285,7 +285,7 @@ void HPGL_PLOTTER::flash_pad_oval( wxPoint pos, wxSize size, int orient,
if( trace_mode == FILLED )
{
flash_pad_rect( pos, wxSize( size.x, deltaxy + wxRound( pen_diameter ) ),
flash_pad_rect( pos, wxSize( size.x, deltaxy + KiROUND( pen_diameter ) ),
orient, trace_mode );
cx = 0; cy = deltaxy / 2;
RotatePoint( &cx, &cy, orient );
@@ -298,7 +298,7 @@ void HPGL_PLOTTER::flash_pad_oval( wxPoint pos, wxSize size, int orient,
}
else // Plot in SKETCH mode.
{
sketch_oval( pos, size, orient, wxRound( pen_diameter ) );
sketch_oval( pos, size, orient, KiROUND( pen_diameter ) );
}
}
@@ -313,12 +313,12 @@ void HPGL_PLOTTER::flash_pad_circle( wxPoint pos, int diametre,
user_to_device_coordinates( pos );
delta = wxRound( pen_diameter - pen_overlap );
delta = KiROUND( pen_diameter - pen_overlap );
rayon = diametre / 2;
if( trace_mode != LINE )
{
rayon = ( diametre - wxRound( pen_diameter ) ) / 2;
rayon = ( diametre - KiROUND( pen_diameter ) ) / 2;
}
if( rayon < 0 )
@@ -483,7 +483,7 @@ void HPGL_PLOTTER::flash_pad_trapez( wxPoint aPadPos, wxPoint aCorners[4],
wxPoint coord[4]; // absolute coordinates of corners (coordinates in plotter space)
int move;
move = wxRound( pen_diameter );
move = KiROUND( pen_diameter );
for( int ii = 0; ii < 4; ii++ )
polygone[ii] = aCorners[ii];
@@ -512,7 +512,7 @@ void HPGL_PLOTTER::flash_pad_trapez( wxPoint aPadPos, wxPoint aCorners[4],
// TODO: replace this par the HPGL plot polygon.
int jj;
// Fill the shape
move = wxRound( pen_diameter - pen_overlap );
move = KiROUND( pen_diameter - pen_overlap );
// Calculate fill height.
if( polygone[0].y == polygone[3].y ) // Horizontal