[Issue 2906] New: writef problem with formatting floating point

d-bugmail at puremagic.com d-bugmail at puremagic.com
Mon Apr 27 14:49:09 PDT 2009


http://d.puremagic.com/issues/show_bug.cgi?id=2906

           Summary: writef problem with formatting floating point
           Product: D
           Version: 2.030
          Platform: PC
        OS/Version: Windows
            Status: NEW
          Severity: normal
          Priority: P2
         Component: Phobos
        AssignedTo: bugzilla at digitalmars.com
        ReportedBy: bugzilla at digitalmars.com


The following code:

        import std.stdio ;
        import std.date ;

        int main ( string[] args ) {
          invariant n = 1000000000L ;
          invariant delta = 1.0 / n ;
          invariant startTime = getUTCtime ( ) ;
          real sum = 0.0 ;
          for ( auto i = 1L ; i <= n ; ++i ) {
            invariant x = ( i - 0.5 ) * delta ;
            sum += 1.0 / ( 1.0 + x * x ) ;
          }
          invariant pi = 4.0 * sum * delta ;
          invariant elapseTime = ( cast (real) ( getUTCtime ( ) - startTime ) )
/ ticksPerSecond ;
          writefln ( "==== D Sequential pi = %f" , pi ) ;
          writefln ( "==== D Sequential iteration count = %d" , n ) ;
          writefln ( "==== D Sequential elapse = %f" , elapseTime ) ;
          writefln ( "==== D Sequential elapse = %s" , elapseTime ) ;
          printf ( "==== D Sequential elapse = %Lg\n" , elapseTime ) ;
          return 0 ;
        }

Produces the output:

==== D Sequential pi = -88796093704866379077000000000000000000000000.000000
==== D Sequential iteration count = 1000000000
==== D Sequential elapse =
-1204799831137578238800000000000000000000000000000000
00000000000000000000000000000000000000000000000000000000000000000000000000000000
00000000000.000000
==== D Sequential elapse = -1.2048e+142
==== D Sequential elapse = 110.718

writefln() is incorrectly formatting reals with the %f and %s formats.


-- 



More information about the Digitalmars-d-bugs mailing list