• 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_SVG_print.cpp Loading commit data...
dialog_SVG_print.h Loading commit data...
dialog_SVG_print_base.cpp Loading commit data...
dialog_SVG_print_base.fbp Loading commit data...
dialog_SVG_print_base.h Loading commit data...
dialog_block_options_base.cpp Loading commit data...
dialog_block_options_base.fbp Loading commit data...
dialog_block_options_base.h Loading commit data...
dialog_cleaning_options.cpp Loading commit data...
dialog_cleaning_options.h Loading commit data...
dialog_cleaning_options_base.cpp Loading commit data...
dialog_cleaning_options_base.fbp Loading commit data...
dialog_cleaning_options_base.h Loading commit data...
dialog_copper_zones.cpp Loading commit data...
dialog_copper_zones_base.cpp Loading commit data...
dialog_copper_zones_base.fbp Loading commit data...
dialog_copper_zones_base.h Loading commit data...
dialog_design_rules.cpp Loading commit data...
dialog_design_rules.h Loading commit data...
dialog_design_rules_aux_helper_class.h Loading commit data...
dialog_design_rules_base.cpp Loading commit data...
dialog_design_rules_base.fbp Loading commit data...
dialog_design_rules_base.h Loading commit data...
dialog_dimension_editor_base.cpp Loading commit data...
dialog_dimension_editor_base.fbp Loading commit data...
dialog_dimension_editor_base.h Loading commit data...
dialog_display_options.cpp Loading commit data...
dialog_display_options.h Loading commit data...
dialog_display_options_base.cpp Loading commit data...
dialog_display_options_base.fbp Loading commit data...
dialog_display_options_base.h Loading commit data...
dialog_drc.cpp Loading commit data...
dialog_drc.h Loading commit data...
dialog_drc_base.cpp Loading commit data...
dialog_drc_base.fbp Loading commit data...
dialog_drc_base.h Loading commit data...
dialog_edit_module_for_BoardEditor.cpp Loading commit data...
dialog_edit_module_for_BoardEditor.h Loading commit data...
dialog_edit_module_for_BoardEditor_base.cpp Loading commit data...
dialog_edit_module_for_BoardEditor_base.fbp Loading commit data...
dialog_edit_module_for_BoardEditor_base.h Loading commit data...
dialog_edit_module_for_Modedit.cpp Loading commit data...
dialog_edit_module_for_Modedit.h Loading commit data...
dialog_edit_module_for_Modedit_base.cpp Loading commit data...
dialog_edit_module_for_Modedit_base.fbp Loading commit data...
dialog_edit_module_for_Modedit_base.h Loading commit data...
dialog_edit_module_text.cpp Loading commit data...
dialog_edit_module_text_base.cpp Loading commit data...
dialog_edit_module_text_base.fbp Loading commit data...
dialog_edit_module_text_base.h Loading commit data...
dialog_exchange_modules_base.cpp Loading commit data...
dialog_exchange_modules_base.fbp Loading commit data...
dialog_exchange_modules_base.h Loading commit data...
dialog_export_3Dfiles_base.cpp Loading commit data...
dialog_export_3Dfiles_base.fbp Loading commit data...
dialog_export_3Dfiles_base.h Loading commit data...
dialog_freeroute_exchange.cpp Loading commit data...
dialog_freeroute_exchange.h Loading commit data...
dialog_freeroute_exchange_base.cpp Loading commit data...
dialog_freeroute_exchange_base.fbp Loading commit data...
dialog_freeroute_exchange_base.h Loading commit data...
dialog_freeroute_exchange_help.html Loading commit data...
dialog_gen_module_position_file_base.cpp Loading commit data...
dialog_gen_module_position_file_base.fbp Loading commit data...
dialog_gen_module_position_file_base.h Loading commit data...
dialog_gendrill.cpp Loading commit data...
dialog_gendrill.h Loading commit data...
dialog_gendrill_base.cpp Loading commit data...
dialog_gendrill_base.fbp Loading commit data...
dialog_gendrill_base.h Loading commit data...
dialog_general_options.cpp Loading commit data...
dialog_general_options.h Loading commit data...
dialog_general_options_BoardEditor_base.cpp Loading commit data...
dialog_general_options_BoardEditor_base.fbp Loading commit data...
dialog_general_options_BoardEditor_base.h Loading commit data...
dialog_global_deletion.cpp Loading commit data...
dialog_global_deletion.h Loading commit data...
dialog_global_deletion_base.cpp Loading commit data...
dialog_global_deletion_base.fbp Loading commit data...
dialog_global_deletion_base.h Loading commit data...
dialog_global_edit_tracks_and_vias.cpp Loading commit data...
dialog_global_edit_tracks_and_vias.h Loading commit data...
dialog_global_edit_tracks_and_vias_base.cpp Loading commit data...
dialog_global_edit_tracks_and_vias_base.fbp Loading commit data...
dialog_global_edit_tracks_and_vias_base.h Loading commit data...
dialog_global_pads_edition_base.cpp Loading commit data...
dialog_global_pads_edition_base.fbp Loading commit data...
dialog_global_pads_edition_base.h Loading commit data...
dialog_graphic_item_properties.cpp Loading commit data...
dialog_graphic_item_properties_base.cpp Loading commit data...
dialog_graphic_item_properties_base.fbp Loading commit data...
dialog_graphic_item_properties_base.h Loading commit data...
dialog_graphic_item_properties_for_Modedit.cpp Loading commit data...
dialog_graphic_items_options.cpp Loading commit data...
dialog_graphic_items_options.h Loading commit data...
dialog_graphic_items_options_base.cpp Loading commit data...
dialog_graphic_items_options_base.fbp Loading commit data...
dialog_graphic_items_options_base.h Loading commit data...
dialog_layers_setup.cpp Loading commit data...
dialog_layers_setup_base.cpp Loading commit data...
dialog_layers_setup_base.fbp Loading commit data...
dialog_layers_setup_base.h Loading commit data...
dialog_mask_clearance.cpp Loading commit data...
dialog_mask_clearance.h Loading commit data...
dialog_mask_clearance_base.cpp Loading commit data...
dialog_mask_clearance_base.fbp Loading commit data...
dialog_mask_clearance_base.h Loading commit data...
dialog_netlist.cpp Loading commit data...
dialog_netlist.h Loading commit data...
dialog_netlist_fbp.cpp Loading commit data...
dialog_netlist_fbp.fbp Loading commit data...
dialog_netlist_fbp.h Loading commit data...
dialog_non_copper_zones_properties_base.cpp Loading commit data...
dialog_non_copper_zones_properties_base.fbp Loading commit data...
dialog_non_copper_zones_properties_base.h Loading commit data...
dialog_orient_footprints.cpp Loading commit data...
dialog_orient_footprints_base.cpp Loading commit data...
dialog_orient_footprints_base.fbp Loading commit data...
dialog_orient_footprints_base.h Loading commit data...
dialog_pad_properties.cpp Loading commit data...
dialog_pad_properties_base.cpp Loading commit data...
dialog_pad_properties_base.fbp Loading commit data...
dialog_pad_properties_base.h Loading commit data...
dialog_pcb_text_properties.cpp Loading commit data...
dialog_pcb_text_properties_base.cpp Loading commit data...
dialog_pcb_text_properties_base.fbp Loading commit data...
dialog_pcb_text_properties_base.h Loading commit data...
dialog_pcbnew_config_libs_and_paths.cpp Loading commit data...
dialog_pcbnew_config_libs_and_paths.h Loading commit data...
dialog_pcbnew_config_libs_and_paths_fbp.cpp Loading commit data...
dialog_pcbnew_config_libs_and_paths_fbp.fbp Loading commit data...
dialog_pcbnew_config_libs_and_paths_fbp.h Loading commit data...
dialog_plot_base.cpp Loading commit data...
dialog_plot_base.fbp Loading commit data...
dialog_plot_base.h Loading commit data...
dialog_print_for_modedit.cpp Loading commit data...
dialog_print_for_modedit_base.cpp Loading commit data...
dialog_print_for_modedit_base.fbp Loading commit data...
dialog_print_for_modedit_base.h Loading commit data...
dialog_print_using_printer.cpp Loading commit data...
dialog_print_using_printer_base.cpp Loading commit data...
dialog_print_using_printer_base.fbp Loading commit data...
dialog_print_using_printer_base.h Loading commit data...
dialog_set_grid.fbp Loading commit data...
dialog_set_grid_base.cpp Loading commit data...
dialog_set_grid_base.h Loading commit data...