• 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
..
dialog_about Loading commit data...
dialogs Loading commit data...
CMakeLists.txt Loading commit data...
base_screen.cpp Loading commit data...
base_struct.cpp Loading commit data...
base_units.cpp Loading commit data...
basicframe.cpp Loading commit data...
bezier_curves.cpp Loading commit data...
bitmap.cpp Loading commit data...
block_commande.cpp Loading commit data...
build_version.cpp Loading commit data...
class_bitmap_base.cpp Loading commit data...
class_colors_design_settings.cpp Loading commit data...
class_layer_box_selector.cpp Loading commit data...
class_marker_base.cpp Loading commit data...
class_page_info.cpp Loading commit data...
class_plotter.cpp Loading commit data...
class_undoredo_container.cpp Loading commit data...
common.cpp Loading commit data...
common_plotDXF_functions.cpp Loading commit data...
common_plotGERBER_functions.cpp Loading commit data...
common_plotHPGL_functions.cpp Loading commit data...
common_plotPS_functions.cpp Loading commit data...
common_plot_functions.cpp Loading commit data...
confirm.cpp Loading commit data...
copy_to_clipboard.cpp Loading commit data...
dcsvg.cpp Loading commit data...
dialog_shim.cpp Loading commit data...
displlst.cpp Loading commit data...
dlist.cpp Loading commit data...
drawframe.cpp Loading commit data...
drawpanel.cpp Loading commit data...
drawtxt.cpp Loading commit data...
dsnlexer.cpp Loading commit data...
eda_dde.cpp Loading commit data...
eda_doc.cpp Loading commit data...
eda_text.cpp Loading commit data...
edaappl.cpp Loading commit data...
filter_reader.cpp Loading commit data...
footprint_info.cpp Loading commit data...
gestfich.cpp Loading commit data...
getrunningmicrosecs.cpp Loading commit data...
gr_basic.cpp Loading commit data...
hotkey_grid_table.cpp Loading commit data...
hotkeys_basic.cpp Loading commit data...
html_messagebox.cpp Loading commit data...
msgpanel.cpp Loading commit data...
netlist.keywords Loading commit data...
newstroke_font.cpp Loading commit data...
pcb_plot_params.keywords Loading commit data...
pcbcommon.cpp Loading commit data...
projet_config.cpp Loading commit data...
richio.cpp Loading commit data...
sch_item_struct.cpp Loading commit data...
selcolor.cpp Loading commit data...
string.cpp Loading commit data...
trigo.cpp Loading commit data...
wildcards_and_files_ext.cpp Loading commit data...
worksheet.cpp Loading commit data...
wxwineda.cpp Loading commit data...
xnode.cpp Loading commit data...
zoom.cpp Loading commit data...