• 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...
plugins Loading commit data...
CMakeLists.txt Loading commit data...
Info.plist Loading commit data...
annotate.cpp Loading commit data...
backanno.cpp Loading commit data...
block.cpp Loading commit data...
block_libedit.cpp Loading commit data...
build_BOM.cpp Loading commit data...
bus-wire-junction.cpp Loading commit data...
busentry.cpp Loading commit data...
class_drc_erc_item.cpp Loading commit data...
class_libentry.cpp Loading commit data...
class_libentry.h Loading commit data...
class_library.cpp Loading commit data...
class_library.h Loading commit data...
class_netlist_object.cpp Loading commit data...
class_netlist_object.h Loading commit data...
cmp_library.keywords Loading commit data...
cmp_library_lexer.cpp Loading commit data...
component_references_lister.cpp Loading commit data...
controle.cpp Loading commit data...
cross-probing.cpp Loading commit data...
dangling_ends.cpp Loading commit data...
database.cpp Loading commit data...
dialog_erc_listbox.h Loading commit data...
edit_bitmap.cpp Loading commit data...
edit_component_in_schematic.cpp Loading commit data...
edit_label.cpp Loading commit data...
eelibs_read_libraryfiles.cpp Loading commit data...
eeredraw.cpp Loading commit data...
eeschema.cpp Loading commit data...
eeschema.icns Loading commit data...
eeschema.rc Loading commit data...
eeschema_config.cpp Loading commit data...
eeschema_config.h Loading commit data...
eeschema_doc.icns Loading commit data...
eeschema_id.h Loading commit data...
erc.cpp Loading commit data...
erc.h Loading commit data...
events_called_functions_for_edit.cpp Loading commit data...
files-io.cpp Loading commit data...
find.cpp Loading commit data...
general.h Loading commit data...
getpart.cpp Loading commit data...
help_common_strings.h Loading commit data...
hierarch.cpp Loading commit data...
hotkeys.cpp Loading commit data...
hotkeys.h Loading commit data...
lib_arc.cpp Loading commit data...
lib_arc.h Loading commit data...
lib_bezier.cpp Loading commit data...
lib_bezier.h Loading commit data...
lib_circle.cpp Loading commit data...
lib_circle.h Loading commit data...
lib_collectors.cpp Loading commit data...
lib_collectors.h Loading commit data...
lib_draw_item.cpp Loading commit data...
lib_draw_item.h Loading commit data...
lib_export.cpp Loading commit data...
lib_field.cpp Loading commit data...
lib_field.h Loading commit data...
lib_pin.cpp Loading commit data...
lib_pin.h Loading commit data...
lib_polyline.cpp Loading commit data...
lib_polyline.h Loading commit data...
lib_rectangle.cpp Loading commit data...
lib_rectangle.h Loading commit data...
lib_text.cpp Loading commit data...
lib_text.h Loading commit data...
libarch.cpp Loading commit data...
libedit.cpp Loading commit data...
libedit_onleftclick.cpp Loading commit data...
libedit_onrightclick.cpp Loading commit data...
libedit_plot_component.cpp Loading commit data...
libedit_undo_redo.cpp Loading commit data...
libeditframe.cpp Loading commit data...
libeditframe.h Loading commit data...
libfield.cpp Loading commit data...
load_one_schematic_file.cpp Loading commit data...
menubar.cpp Loading commit data...
menubar_libedit.cpp Loading commit data...
netform.cpp Loading commit data...
netlist.cpp Loading commit data...
netlist.h Loading commit data...
netlist_control.cpp Loading commit data...
netlist_control.h Loading commit data...
onleftclick.cpp Loading commit data...
onrightclick.cpp Loading commit data...
operations_on_items_lists.cpp Loading commit data...
pinedit.cpp Loading commit data...
protos.h Loading commit data...
sch_base_frame.cpp Loading commit data...
sch_bitmap.cpp Loading commit data...
sch_bitmap.h Loading commit data...
sch_bus_entry.cpp Loading commit data...
sch_bus_entry.h Loading commit data...
sch_collectors.cpp Loading commit data...
sch_collectors.h Loading commit data...
sch_component.cpp Loading commit data...
sch_component.h Loading commit data...
sch_field.cpp Loading commit data...
sch_field.h Loading commit data...
sch_junction.cpp Loading commit data...
sch_junction.h Loading commit data...
sch_line.cpp Loading commit data...
sch_line.h Loading commit data...
sch_marker.cpp Loading commit data...
sch_marker.h Loading commit data...
sch_no_connect.cpp Loading commit data...
sch_no_connect.h Loading commit data...
sch_polyline.cpp Loading commit data...
sch_polyline.h Loading commit data...
sch_screen.cpp Loading commit data...
sch_sheet.cpp Loading commit data...
sch_sheet.h Loading commit data...
sch_sheet_path.cpp Loading commit data...
sch_sheet_path.h Loading commit data...
sch_sheet_pin.cpp Loading commit data...
sch_text.cpp Loading commit data...
sch_text.h Loading commit data...
schedit.cpp Loading commit data...
schematic_undo_redo.cpp Loading commit data...
schframe.cpp Loading commit data...
selpart.cpp Loading commit data...
sheet.cpp Loading commit data...
sheetlab.cpp Loading commit data...
symbdraw.cpp Loading commit data...
symbedit.cpp Loading commit data...
template_fieldnames.cpp Loading commit data...
template_fieldnames.h Loading commit data...
template_fieldnames.keywords Loading commit data...
tool_lib.cpp Loading commit data...
tool_sch.cpp Loading commit data...
tool_viewlib.cpp Loading commit data...
transform.cpp Loading commit data...
transform.h Loading commit data...
viewlib_frame.cpp Loading commit data...
viewlib_frame.h Loading commit data...
viewlibs.cpp Loading commit data...