• 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...
CMakeLists.txt Loading commit data...
Info.plist Loading commit data...
ar_protos.h Loading commit data...
attribut.cpp Loading commit data...
automove.cpp Loading commit data...
autoplac.cpp Loading commit data...
autorout.cpp Loading commit data...
autorout.h Loading commit data...
basepcbframe.cpp Loading commit data...
block.cpp Loading commit data...
block_module_editor.cpp Loading commit data...
board.cpp Loading commit data...
board_items_to_polygon_shape_transform.cpp Loading commit data...
board_undo_redo.cpp Loading commit data...
build_BOM_from_board.cpp Loading commit data...
cell.h Loading commit data...
class_board.cpp Loading commit data...
class_board.h Loading commit data...
class_board_connected_item.cpp Loading commit data...
class_board_connected_item.h Loading commit data...
class_board_design_settings.cpp Loading commit data...
class_board_item.cpp Loading commit data...
class_dimension.cpp Loading commit data...
class_dimension.h Loading commit data...
class_drawsegment.cpp Loading commit data...
class_drawsegment.h Loading commit data...
class_drc_item.cpp Loading commit data...
class_edge_mod.cpp Loading commit data...
class_edge_mod.h Loading commit data...
class_footprint_library.cpp.notused Loading commit data...
class_footprint_library.h.notused Loading commit data...
class_marker_pcb.cpp Loading commit data...
class_marker_pcb.h Loading commit data...
class_mire.cpp Loading commit data...
class_mire.h Loading commit data...
class_module.cpp Loading commit data...
class_module.h Loading commit data...
class_module_transform_functions.cpp Loading commit data...
class_netclass.cpp Loading commit data...
class_netclass.h Loading commit data...
class_netinfo.h Loading commit data...
class_netinfo_item.cpp Loading commit data...
class_netinfolist.cpp Loading commit data...
class_pad.cpp Loading commit data...
class_pad.h Loading commit data...
class_pad_draw_functions.cpp Loading commit data...
class_pcb_layer_widget.cpp Loading commit data...
class_pcb_layer_widget.h Loading commit data...
class_pcb_text.cpp Loading commit data...
class_pcb_text.h Loading commit data...
class_text_mod.cpp Loading commit data...
class_text_mod.h Loading commit data...
class_track.cpp Loading commit data...
class_track.h Loading commit data...
class_zone.cpp Loading commit data...
class_zone.h Loading commit data...
class_zone_settings.cpp Loading commit data...
class_zone_settings.h Loading commit data...
classpcb.cpp Loading commit data...
clean.cpp Loading commit data...
collectors.cpp Loading commit data...
collectors.h Loading commit data...
connect.cpp Loading commit data...
controle.cpp Loading commit data...
cross-probing.cpp Loading commit data...
deltrack.cpp Loading commit data...
dimension.cpp Loading commit data...
dist.cpp Loading commit data...
drag.h Loading commit data...
dragsegm.cpp Loading commit data...
drc.cpp Loading commit data...
drc_clearance_test_functions.cpp Loading commit data...
drc_marker_functions.cpp Loading commit data...
drc_stuff.h Loading commit data...
edgemod.cpp Loading commit data...
edit.cpp Loading commit data...
edit_pcb_text.cpp Loading commit data...
edit_track_width.cpp Loading commit data...
editedge.cpp Loading commit data...
editmod.cpp Loading commit data...
editrack-part2.cpp Loading commit data...
editrack.cpp Loading commit data...
edtxtmod.cpp Loading commit data...
event_handlers_tracks_vias_sizes.cpp Loading commit data...
export_gencad.cpp Loading commit data...
export_vrml.cpp Loading commit data...
files.cpp Loading commit data...
find.cpp Loading commit data...
find.h Loading commit data...
find.pjd Loading commit data...
gen_drill_report_files.cpp Loading commit data...
gen_holes_and_tools_lists_for_drill.cpp Loading commit data...
gen_modules_placefile.cpp Loading commit data...
gendrill.cpp Loading commit data...
gendrill.h Loading commit data...
globaleditpad.cpp Loading commit data...
gpcb_exchange.cpp Loading commit data...
graphpcb.cpp Loading commit data...
help_common_strings.h Loading commit data...
highlight.cpp Loading commit data...
hotkeys.cpp Loading commit data...
hotkeys.h Loading commit data...
hotkeys_board_editor.cpp Loading commit data...
hotkeys_module_editor.cpp Loading commit data...
initpcb.cpp Loading commit data...
io_mgr.cpp Loading commit data...
io_mgr.h Loading commit data...
ioascii.cpp.notused Loading commit data...
item_io.cpp.notused Loading commit data...
kicad_plugin.cpp Loading commit data...
kicad_plugin.h Loading commit data...
layer_widget.cpp Loading commit data...
layer_widget.h Loading commit data...
legacy_plugin.cpp Loading commit data...
legacy_plugin.h Loading commit data...
librairi.cpp Loading commit data...
loadcmp.cpp Loading commit data...
magnetic_tracks_functions.cpp Loading commit data...
menubar_modedit.cpp Loading commit data...
menubar_pcbframe.cpp Loading commit data...
minimun_spanning_tree.cpp Loading commit data...
minimun_spanning_tree.h Loading commit data...
mirepcb.cpp Loading commit data...
modedit.cpp Loading commit data...
modedit_onclick.cpp Loading commit data...
modedit_undo_redo.cpp Loading commit data...
modeditoptions.cpp Loading commit data...
module_editor_frame.h Loading commit data...
moduleframe.cpp Loading commit data...
modules.cpp Loading commit data...
modview.cpp Loading commit data...
modview_frame.cpp Loading commit data...
modview_frame.h Loading commit data...
move-drag_pads.cpp Loading commit data...
move_copy_track.cpp.notused Loading commit data...
move_or_drag_track.cpp Loading commit data...
muonde.cpp Loading commit data...
muwave_command.cpp Loading commit data...
netlist.cpp Loading commit data...
netlist_reader.h Loading commit data...
netlist_reader_common.cpp Loading commit data...
netlist_reader_firstformat.cpp Loading commit data...
netlist_reader_kicad.cpp Loading commit data...
onleftclick.cpp Loading commit data...
onrightclick.cpp Loading commit data...
pcb_plot_params.cpp Loading commit data...
pcb_plot_params.h Loading commit data...
pcbframe.cpp Loading commit data...
pcbnew.cpp Loading commit data...
pcbnew.h Loading commit data...
pcbnew.icns Loading commit data...
pcbnew.rc Loading commit data...
pcbnew_config.cpp Loading commit data...
pcbnew_config.h Loading commit data...
pcbnew_doc.icns Loading commit data...
pcbnew_id.h Loading commit data...
pcbplot.cpp Loading commit data...
pcbplot.h Loading commit data...
plot_rtn.cpp Loading commit data...
plotdxf.cpp Loading commit data...
plotgerb.cpp Loading commit data...
plothpgl.cpp Loading commit data...
plotps.cpp Loading commit data...
polygons_defs.h Loading commit data...
print_board_functions.cpp Loading commit data...
printout_controler.cpp Loading commit data...
printout_controler.h Loading commit data...
protos.h Loading commit data...
queue.cpp Loading commit data...
ratsnest.cpp Loading commit data...
sel_layer.cpp Loading commit data...
set_grid.cpp Loading commit data...
solve.cpp Loading commit data...
specctra.cpp Loading commit data...
specctra.h Loading commit data...
specctra.keywords Loading commit data...
specctra_export.cpp Loading commit data...
specctra_import.cpp Loading commit data...
specctra_test.cpp Loading commit data...
swap_layers.cpp Loading commit data...
tool_modedit.cpp Loading commit data...
tool_modview.cpp Loading commit data...
tool_onrightclick.cpp Loading commit data...
tool_pcb.cpp Loading commit data...
toolbars_update_user_interface.cpp Loading commit data...
tr_modif.cpp Loading commit data...
tracepcb.cpp Loading commit data...
trpiste.cpp Loading commit data...
work.cpp Loading commit data...
xchgmod.cpp Loading commit data...
zone_filling_algorithm.cpp Loading commit data...
zones.h Loading commit data...
zones_by_polygon.cpp Loading commit data...
zones_by_polygon_fill_functions.cpp Loading commit data...
zones_convert_brd_items_to_polygons_with_Boost.cpp Loading commit data...
zones_convert_to_polygons_aux_functions.cpp Loading commit data...
zones_functions_for_undo_redo.cpp Loading commit data...
zones_functions_for_undo_redo.h Loading commit data...
zones_non_copper_type_functions.cpp Loading commit data...
zones_polygons_insulated_copper_islands.cpp Loading commit data...
zones_polygons_test_connections.cpp Loading commit data...
zones_test_and_combine_areas.cpp Loading commit data...