• Dick Hollenbeck's avatar
    // Dick Hollenbeck's KiROUND R&D · c24863c0
    Dick Hollenbeck authored
    // 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;
    }
    c24863c0
Name
Last commit
Last update
..
dialogs Loading commit data...
gerber_test_files Loading commit data...
CMakeLists.txt Loading commit data...
Info.plist Loading commit data...
block.cpp Loading commit data...
class_DCodeSelectionbox.cpp Loading commit data...
class_DCodeSelectionbox.h Loading commit data...
class_GERBER.cpp Loading commit data...
class_GERBER.h Loading commit data...
class_am_param.cpp Loading commit data...
class_am_param.h Loading commit data...
class_aperture_macro.cpp Loading commit data...
class_aperture_macro.h Loading commit data...
class_excellon.h Loading commit data...
class_gerber_draw_item.cpp Loading commit data...
class_gerber_draw_item.h Loading commit data...
class_gerbview_layer_widget.cpp Loading commit data...
class_gerbview_layer_widget.h Loading commit data...
controle.cpp Loading commit data...
dcode.cpp Loading commit data...
dcode.h Loading commit data...
draw_gerber_screen.cpp Loading commit data...
events_called_functions.cpp Loading commit data...
excellon_read_drill_file.cpp Loading commit data...
export_to_pcbnew.cpp Loading commit data...
files.cpp Loading commit data...
gerbview.cpp Loading commit data...
gerbview.h Loading commit data...
gerbview.icns Loading commit data...
gerbview.rc Loading commit data...
gerbview_config.cpp Loading commit data...
gerbview_doc.icns Loading commit data...
gerbview_frame.cpp Loading commit data...
gerbview_frame.h Loading commit data...
gerbview_id.h Loading commit data...
hotkeys.cpp Loading commit data...
hotkeys.h Loading commit data...
initpcb.cpp Loading commit data...
locate.cpp Loading commit data...
menubar.cpp Loading commit data...
onleftclick.cpp Loading commit data...
onrightclick.cpp Loading commit data...
options.cpp Loading commit data...
pcbplot.cpp Loading commit data...
pcbplot.h Loading commit data...
readgerb.cpp Loading commit data...
rs274_read_XY_and_IJ_coordinates.cpp Loading commit data...
rs274d.cpp Loading commit data...
rs274x.cpp Loading commit data...
select_layers_to_pcb.cpp Loading commit data...
select_layers_to_pcb.h Loading commit data...
toolbars_gerber.cpp Loading commit data...