From 6231ce5959d76cf52067f0d870f60322c3b4185d Mon Sep 17 00:00:00 2001 From: Dick Hollenbeck Date: Thu, 30 May 2013 16:46:14 -0500 Subject: [PATCH] fix buggy double2str() --- pcbnew/class_board_item.cpp | 11 +++---- pcbnew/kicad_plugin.cpp | 59 +++++++++++++++++++++++++++++-------- 2 files changed, 53 insertions(+), 17 deletions(-) diff --git a/pcbnew/class_board_item.cpp b/pcbnew/class_board_item.cpp index 9b2bb8a972..dbb78a217d 100644 --- a/pcbnew/class_board_item.cpp +++ b/pcbnew/class_board_item.cpp @@ -88,11 +88,9 @@ wxString BOARD_ITEM::GetLayerName() const std::string BOARD_ITEM::FormatInternalUnits( int aValue ) { - char buf[50]; - - double mm = aValue / IU_PER_MM; - + char buf[50]; int len; + double mm = aValue / IU_PER_MM; if( mm != 0.0 && fabs( mm ) <= 0.0001 ) { @@ -101,7 +99,10 @@ std::string BOARD_ITEM::FormatInternalUnits( int aValue ) while( --len > 0 && buf[len] == '0' ) buf[len] = '\0'; - ++len; + if( buf[len] == '.' ) + buf[len] = '\0'; + else + ++len; } else { diff --git a/pcbnew/kicad_plugin.cpp b/pcbnew/kicad_plugin.cpp index f8d8d561d8..7fec74299a 100644 --- a/pcbnew/kicad_plugin.cpp +++ b/pcbnew/kicad_plugin.cpp @@ -64,27 +64,62 @@ static const wxString traceFootprintLibrary( wxT( "KicadFootprintLib" ) ); // Helper function to print a float number without using scientific notation // and no trailing 0 -// For many reasons, S-expr does not accept scientific notation -// for floating numbers, so we cannot use the %g format to print a fp number -// and %f leaves trailing 0. -// this helper function uses the %f format, and then removes trailing 0 + +#if 0 +// Does not work for aValue < 0.0001 and > 0. +// Will need to support exponents in DSNLEXER if we get exponents > 16, i.e. the "precision". std::string double2str( double aValue ) { - char buf[50]; - int len = sprintf( buf, "%.16f", aValue ); + char buf[50]; + int len; - while( --len > 0 && buf[len] == '0' ) - buf[len] = '\0'; + if( aValue != 0.0 && fabs( aValue ) <= 0.0001 ) + { + len = sprintf( buf, "%.10f", aValue ); + + while( --len > 0 && buf[len] == '0' ) + buf[len] = '\0'; + + if( buf[len] == '.' ) + buf[len--] = '\0'; - // Remove useless separator: - if( buf[len] == '.' ) - buf[len] = '\0'; - else ++len; + } + else + { + len = sprintf( buf, "%.10g", mm ); + } return std::string( buf, len ); } +#else +// this one handles 0.00001 ok, and 1.222222222222222 ok, previous did not. +std::string double2str( double aValue ) +{ + char buf[50]; + int len; + + if( aValue != 0.0 && fabs( aValue ) <= 0.0001 ) + { + len = sprintf( buf, "%.16f", aValue ); + + while( --len > 0 && buf[len] == '0' ) + buf[len] = '\0'; + + if( buf[len] == '.' ) + buf[len] = '\0'; + else + ++len; + } + else + { + len = sprintf( buf, "%.16g", aValue ); + } + + return std::string( buf, len );; +} +#endif /**