• 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
..
boost Loading commit data...
HersheyCyrillic.h.unused Loading commit data...
appl_wxstruct.h Loading commit data...
base_struct.h Loading commit data...
base_units.h Loading commit data...
bezier_curves.h Loading commit data...
bitmaps.h Loading commit data...
block_commande.h Loading commit data...
build_version.h Loading commit data...
class_base_screen.h Loading commit data...
class_bitmap_base.h Loading commit data...
class_board_design_settings.h Loading commit data...
class_board_item.h Loading commit data...
class_collector.h Loading commit data...
class_colors_design_settings.h Loading commit data...
class_drawpanel.h Loading commit data...
class_drc_item.h Loading commit data...
class_layer_box_selector.h Loading commit data...
class_macros_record.h Loading commit data...
class_marker_base.h Loading commit data...
class_pcb_screen.h Loading commit data...
class_sch_screen.h Loading commit data...
class_title_block.h Loading commit data...
class_undoredo_container.h Loading commit data...
colors.h Loading commit data...
colors_selection.h Loading commit data...
common.h Loading commit data...
confirm.h Loading commit data...
convert_to_biu.h Loading commit data...
dcsvg.h Loading commit data...
dialog_get_component.h Loading commit data...
dialog_helpers.h Loading commit data...
dialog_hotkeys_editor.h Loading commit data...
dialog_shim.h Loading commit data...
dlist.h Loading commit data...
drawtxt.h Loading commit data...
dsnlexer.h Loading commit data...
eda_dde.h Loading commit data...
eda_doc.h Loading commit data...
eda_text.h Loading commit data...
fctsys.h Loading commit data...
filter_reader.h Loading commit data...
footprint_info.h Loading commit data...
gestfich.h Loading commit data...
gr_basic.h Loading commit data...
hashtables.h Loading commit data...
hotkey_grid_table.h Loading commit data...
hotkeys_basic.h Loading commit data...
html_messagebox.h Loading commit data...
id.h Loading commit data...
kicad_device_context.h Loading commit data...
kicad_msvc.h Loading commit data...
kicad_string.h Loading commit data...
layers_id_colors_and_visibility.h Loading commit data...
length.h Loading commit data...
macros.h Loading commit data...
menus_helpers.h Loading commit data...
newstroke_font.h Loading commit data...
online_help.h Loading commit data...
pad_shapes.h Loading commit data...
param_config.h Loading commit data...
pcbcommon.h Loading commit data...
pcbstruct.h Loading commit data...
plot_common.h Loading commit data...
richio.h Loading commit data...
sch_base_frame.h Loading commit data...
sch_item_struct.h Loading commit data...
trigo.h Loading commit data...
wildcards_and_files_ext.h Loading commit data...
worksheet.h Loading commit data...
wxBasePcbFrame.h Loading commit data...
wxEeschemaStruct.h Loading commit data...
wxPcbStruct.h Loading commit data...
wxstruct.h Loading commit data...
xnode.h Loading commit data...