// 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
+16 -16
View File
@@ -515,8 +515,8 @@ void EDA_DRAW_FRAME::AdjustScrollBars( const wxPoint& aCenterPosition )
clientSize = m_canvas->GetClientSize();
// The logical size of the client window.
logicalClientSize.x = wxRound( (double) clientSize.x / scalar );
logicalClientSize.y = wxRound( (double) clientSize.y / scalar );
logicalClientSize.x = KiROUND( (double) clientSize.x / scalar );
logicalClientSize.y = KiROUND( (double) clientSize.y / scalar );
// A corner of the drawing in internal units.
wxSize corner = GetPageSizeIU();
@@ -531,14 +531,14 @@ void EDA_DRAW_FRAME::AdjustScrollBars( const wxPoint& aCenterPosition )
drawingRect.GetTop(), drawingRect.GetBottom() );
// The size of the client rectangle in logical units.
int x = wxRound( (double) aCenterPosition.x - ( (double) logicalClientSize.x / 2.0 ) );
int y = wxRound( (double) aCenterPosition.y - ( (double) logicalClientSize.y / 2.0 ) );
int x = KiROUND( (double) aCenterPosition.x - ( (double) logicalClientSize.x / 2.0 ) );
int y = KiROUND( (double) aCenterPosition.y - ( (double) logicalClientSize.y / 2.0 ) );
// If drawn around the center, adjust the client rectangle accordingly.
if( screen->m_Center )
{
x += wxRound( (double) drawingRect.width / 2.0 );
y += wxRound( (double) drawingRect.height / 2.0 );
x += KiROUND( (double) drawingRect.width / 2.0 );
y += KiROUND( (double) drawingRect.height / 2.0 );
}
wxRect logicalClientRect( wxPoint( x, y ), logicalClientSize );
@@ -622,13 +622,13 @@ void EDA_DRAW_FRAME::AdjustScrollBars( const wxPoint& aCenterPosition )
if( screen->m_Center )
{
screen->m_DrawOrg.x = -( wxRound( (double) virtualSize.x / 2.0 ) );
screen->m_DrawOrg.y = -( wxRound( (double) virtualSize.y / 2.0 ) );
screen->m_DrawOrg.x = -( KiROUND( (double) virtualSize.x / 2.0 ) );
screen->m_DrawOrg.y = -( KiROUND( (double) virtualSize.y / 2.0 ) );
}
else
{
screen->m_DrawOrg.x = -( wxRound( (double) (virtualSize.x - drawingRect.width) / 2.0 ) );
screen->m_DrawOrg.y = -( wxRound( (double) (virtualSize.y - drawingRect.height) / 2.0 ) );
screen->m_DrawOrg.x = -( KiROUND( (double) (virtualSize.x - drawingRect.width) / 2.0 ) );
screen->m_DrawOrg.y = -( KiROUND( (double) (virtualSize.y - drawingRect.height) / 2.0 ) );
}
/* Always set scrollbar pixels per unit to 1 unless you want the zoom
@@ -640,20 +640,20 @@ void EDA_DRAW_FRAME::AdjustScrollBars( const wxPoint& aCenterPosition )
screen->m_ScrollPixelsPerUnitX = screen->m_ScrollPixelsPerUnitY = 1;
// Calculate the number of scroll bar units for the given zoom level in device units.
unitsX = wxRound( (double) virtualSize.x * scalar );
unitsY = wxRound( (double) virtualSize.y * scalar );
unitsX = KiROUND( (double) virtualSize.x * scalar );
unitsY = KiROUND( (double) virtualSize.y * scalar );
// Calculate the scroll bar position in logical units to place the center position at
// the center of client rectangle.
screen->SetScrollCenterPosition( aCenterPosition );
posX = aCenterPosition.x - wxRound( (double) logicalClientRect.width / 2.0 ) -
posX = aCenterPosition.x - KiROUND( (double) logicalClientRect.width / 2.0 ) -
screen->m_DrawOrg.x;
posY = aCenterPosition.y - wxRound( (double) logicalClientRect.height / 2.0 ) -
posY = aCenterPosition.y - KiROUND( (double) logicalClientRect.height / 2.0 ) -
screen->m_DrawOrg.y;
// Convert scroll bar position to device units.
posX = wxRound( (double) posX * scalar );
posY = wxRound( (double) posY * scalar );
posX = KiROUND( (double) posX * scalar );
posY = KiROUND( (double) posY * scalar );
if( posX < 0 )
{